001: /*
002: * Copyright 2006 Davide Deidda
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: package it.biobytes.ammentos;
017:
018: import java.util.*;
019: import it.biobytes.ammentos.query.*;
020: import it.biobytes.ammentos.util.*;
021: import java.util.logging.*;
022:
023: /**
024: *
025: * @author davide
026: */
027: public abstract class CollectionField {
028:
029: private String m_sqlQuery;
030: private Class m_mappedClass; // Mapped class
031: private java.lang.reflect.Field m_classField; // Class field
032: private String m_sourceDomain;
033: private boolean m_cascadeOnSave;
034: private boolean m_cascadeOnDelete;
035: private boolean m_deleteOnRemove;
036: private Logger m_logger = Logger.getLogger("ammentos");
037:
038: /**
039: * Creates a new instance of MapField
040: */
041: public CollectionField(Class mappedClass,
042: java.lang.reflect.Field classMember, String strQuery) {
043: m_mappedClass = mappedClass;
044: m_classField = classMember;
045: m_sqlQuery = strQuery;
046: m_classField.setAccessible(true);
047: }
048:
049: private EntityCollection getEntityCollection(Object obj) {
050: Object res = null;
051: try {
052: res = m_classField.get(obj);
053: // If the map is not an EntityMap it's replaced silently
054: if ((res == null) || (!(res instanceof EntityCollection))) {
055: replaceCollection(obj);
056: }
057: res = (EntityCollection) m_classField.get(obj);
058: } catch (Exception e) {
059: m_logger.severe("Error getting Map: " + e.getMessage());
060: e.printStackTrace();
061: }
062: return (EntityCollection) res;
063: }
064:
065: /**
066: * Saves this map for the containing object
067: */
068: public void save(Object parent) throws PersistenceException {
069: if (m_cascadeOnSave) {
070: EntityCollection ec = getEntityCollection(parent);
071: ec.save();
072: }
073: }
074:
075: public void delete(Object parent) throws PersistenceException {
076: if (m_cascadeOnDelete) {
077: EntityCollection map = getEntityCollection(parent);
078: if ((map != null)) {
079: map.delete();
080: }
081: }
082: }
083:
084: /**
085: * Loading this field means unloading the map in order to refresh it at the
086: * next external method call
087: */
088: public void load(Object parent) throws PersistenceException {
089: Object parentCollection = null;
090:
091: try {
092: parentCollection = m_classField.get(parent);
093: } catch (Exception e) {
094: e.printStackTrace();
095: throw new PersistenceException(
096: "Error accessing PersistentMap "
097: + m_classField.getName() + ": "
098: + e.getMessage());
099: }
100: if (parentCollection != null) {
101: if (((parentCollection instanceof Map) && !((Map) parentCollection)
102: .isEmpty())
103: || ((parentCollection instanceof Collection) && !((Collection) parentCollection)
104: .isEmpty())) {
105: throw new PersistenceException("PersistentMap "
106: + m_classField.getName()
107: + " was not empty before loading");
108: }
109: }
110: replaceCollection(parent);
111: }
112:
113: /**
114: * Replaces parent's map with an instance of EntityMap
115: */
116: /**
117: * Replaces parent's map with an instance of EntityMap
118: */
119: protected void replaceCollection(Object parent) {
120: // Generating query for the EntityMap
121: SqlQueryFilter sql = new SqlQueryFilter(getSqlQuery());
122: sql.setObject(parent, FieldTypeEnum.ENTITY.getFieldType());
123: Query qry = new Query(sql);
124: if (!"".equals(getSourceDomain())) {
125: qry.setDomain(getSourceDomain());
126: }
127:
128: try {
129: // Getting old map
130: Object parentCollection = m_classField.get(parent);
131: // Copying elements to the new EntityMap
132:
133: EntityCollection eColl = createEntityCollection(
134: m_mappedClass, qry, parentCollection,
135: m_deleteOnRemove);
136: // Replacing the map with the new one EntityMap
137: m_classField.set(parent, eColl);
138: } catch (Exception e) {
139: m_logger.severe("Error creating EntityMap: "
140: + e.getMessage());
141: e.printStackTrace();
142: }
143: }
144:
145: protected abstract EntityCollection createEntityCollection(
146: Class mappedClass, Query query, Object parentCollection,
147: boolean deleteOnRemove);
148:
149: public void setSourceDomain(String sourceDomain) {
150: m_sourceDomain = sourceDomain;
151: }
152:
153: public String getSourceDomain() {
154: return m_sourceDomain;
155: }
156:
157: public void setCascadeOnSave(boolean cascadeOnSave) {
158: m_cascadeOnSave = cascadeOnSave;
159: }
160:
161: public boolean isCascadeOnSave() {
162: return m_cascadeOnSave;
163: }
164:
165: public void setCascadeOnDelete(boolean cascadeOnDelete) {
166: m_cascadeOnDelete = cascadeOnDelete;
167: }
168:
169: public boolean isCascadeOnDelete() {
170: return m_cascadeOnDelete;
171: }
172:
173: public void setDeleteOnRemove(boolean deleteOnRemove) {
174: m_deleteOnRemove = deleteOnRemove;
175: }
176:
177: public boolean isDeleteOnRemove() {
178: return m_deleteOnRemove;
179: }
180:
181: public String getSqlQuery() {
182: return m_sqlQuery;
183: }
184:
185: public Class getMappedClass() {
186: return m_mappedClass;
187: }
188:
189: public java.lang.reflect.Field getClassField() {
190: return m_classField;
191: }
192: }
|