001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.ecm.core.api.impl; 023 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.Collection; 027import java.util.HashMap; 028import java.util.Map; 029 030import org.nuxeo.ecm.core.api.DataModel; 031import org.nuxeo.ecm.core.api.PropertyException; 032import org.nuxeo.ecm.core.api.model.DocumentPart; 033import org.nuxeo.ecm.core.api.model.Property; 034import org.nuxeo.ecm.core.api.model.PropertyNotFoundException; 035import org.nuxeo.ecm.core.api.model.impl.AbstractProperty; 036import org.nuxeo.ecm.core.api.model.impl.DocumentPartImpl; 037import org.nuxeo.ecm.core.schema.SchemaManager; 038import org.nuxeo.ecm.core.schema.types.Schema; 039import org.nuxeo.runtime.api.Framework; 040 041/** 042 * Data model implementation. 043 * 044 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 045 */ 046public class DataModelImpl implements DataModel { 047 048 private static final long serialVersionUID = 1L; 049 050 private final DocumentPart dp; 051 052 /** 053 * Builds an empty data model. 054 * 055 * @param schema a schema name. 056 */ 057 public DataModelImpl(String schema) { 058 this(schema, new HashMap<String, Object>()); 059 } 060 061 /** 062 * Builds a data model using the given data. 063 * 064 * @param schema a schema name. 065 * @param data the data (map String>Object) to put in the DataModel. 066 */ 067 public DataModelImpl(String schemaName, Map<String, Object> data) { 068 assert data != null; 069 SchemaManager schemaManager = Framework.getService(SchemaManager.class); 070 Schema schema = schemaManager.getSchema(schemaName); 071 dp = new DocumentPartImpl(schema); 072 if (!data.isEmpty()) { 073 dp.init((Serializable) data); 074 } 075 } 076 077 public DataModelImpl(DocumentPart part) { 078 assert part != null; 079 dp = part; 080 } 081 082 /** 083 * Gets the underlying document part. 084 */ 085 public DocumentPart getDocumentPart() { 086 return dp; 087 } 088 089 @Override 090 public String getSchema() { 091 return dp.getSchema().getName(); 092 } 093 094 @Override 095 public Object getData(String key) throws PropertyException { 096 return dp.getValue(key); 097 } 098 099 @Override 100 public void setData(String key, Object value) throws PropertyException { 101 dp.setValue(key, value); 102 } 103 104 @Override 105 @SuppressWarnings("unchecked") 106 public Map<String, Object> getMap() throws PropertyException { 107 return (Map<String, Object>) dp.getValue(); 108 } 109 110 @Override 111 public void setMap(Map<String, Object> data) throws PropertyException { 112 dp.setValue(data); 113 } 114 115 @Override 116 public boolean isDirty() { 117 return dp.isDirty(); 118 } 119 120 @Override 121 public boolean isDirty(String name) throws PropertyNotFoundException { 122 return dp.get(name).isDirty(); 123 } 124 125 @Override 126 public Collection<String> getDirtyFields() { 127 Collection<String> dirtyFields = new ArrayList<String>(); 128 for (Property prop : dp.getChildren()) { 129 if (prop.isDirty()) { 130 dirtyFields.add(prop.getName()); 131 } 132 } 133 return dirtyFields; 134 } 135 136 @Override 137 public String toString() { 138 return getClass().getSimpleName() + '(' + getSchema() + (dp.isDirty() ? "*" : "") + ')'; 139 } 140 141 @Override 142 public void setDirty(String name) throws PropertyNotFoundException { 143 ((AbstractProperty) dp.get(name)).setIsModified(); 144 } 145 146 @Override 147 public Object getValue(String path) throws PropertyException { 148 return dp.getValue(path); 149 } 150 151 @Override 152 public Object setValue(String path, Object value) throws PropertyException { 153 Property prop = dp.resolvePath(path); 154 Object oldValue = prop.getValue(); 155 prop.setValue(value); 156 return oldValue; 157 } 158 159}