001: /**********************************************************************
002: Copyright (c) 2006 Andy Jefferson and others. All rights reserved.
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014:
015: Contributors:
016: ...
017: **********************************************************************/package org.jpox.store.fieldmanager;
018:
019: import java.lang.reflect.Array;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.jpox.StateManager;
026: import org.jpox.api.ApiAdapter;
027: import org.jpox.metadata.AbstractMemberMetaData;
028:
029: /**
030: * Field manager that deletes all "dependent" PC objects referenced from the source object.
031: * Effectively provides "delete-dependent".
032: *
033: * @version $Revision: 1.9 $
034: **/
035: public class DeleteFieldManager extends AbstractFieldManager {
036: /** StateManager for the owning object. */
037: private final StateManager sm;
038:
039: /**
040: * Constructor.
041: * @param sm The state manager for the object.
042: **/
043: public DeleteFieldManager(StateManager sm) {
044: this .sm = sm;
045: }
046:
047: /**
048: * Utility method to process the passed persistable object.
049: * @param pc The PC object
050: */
051: protected void processPersistable(Object pc) {
052: // Delete it
053: // TODO Are there particular object states that we dont want to do this for?
054: sm.getObjectManager().deleteObjectInternal(pc);
055: }
056:
057: /**
058: * Method to store an object field.
059: * @param fieldNumber Number of the field (absolute)
060: * @param value Value of the field
061: */
062: public void storeObjectField(int fieldNumber, Object value) {
063: if (value != null) {
064: AbstractMemberMetaData fmd = sm.getClassMetaData()
065: .getMetaDataForManagedMemberAtAbsolutePosition(
066: fieldNumber);
067: ApiAdapter api = sm.getObjectManager().getApiAdapter();
068: if (api.isPersistable(value)) {
069: // Process PC fields
070: if (fmd.isDependent()) {
071: processPersistable(value);
072: }
073: } else if (value instanceof Collection) {
074: // Process all elements of the Collection that are PC
075: if (fmd.hasCollection()
076: && fmd.getCollection().isDependentElement()) {
077: // Process any elements that are PersistenceCapable
078: Collection coll = (Collection) value;
079: Iterator iter = coll.iterator();
080: while (iter.hasNext()) {
081: Object element = iter.next();
082: if (api.isPersistable(element)) {
083: processPersistable(element);
084: }
085: }
086: }
087: } else if (value instanceof Map) {
088: // Process all keys, values of the Map that are PC
089: Map map = (Map) value;
090: if (fmd.hasMap() && fmd.getMap().isDependentKey()) {
091: // Process any keys that are PersistenceCapable
092: Set keys = map.keySet();
093: Iterator iter = keys.iterator();
094: while (iter.hasNext()) {
095: Object mapKey = iter.next();
096: if (api.isPersistable(mapKey)) {
097: processPersistable(mapKey);
098: }
099: }
100: }
101: if (fmd.hasMap() && fmd.getMap().isDependentValue()) {
102: // Process any values that are PersistenceCapable
103: Collection values = map.values();
104: Iterator iter = values.iterator();
105: while (iter.hasNext()) {
106: Object mapValue = iter.next();
107: if (api.isPersistable(mapValue)) {
108: processPersistable(mapValue);
109: }
110: }
111: }
112: } else if (value instanceof Object[]) {
113: // Process all array elements that are PC
114: if (fmd.hasArray()
115: && fmd.getArray().isDependentElement()) {
116: // Process any array elements that are PersistenceCapable
117: for (int i = 0; i < Array.getLength(value); i++) {
118: Object element = Array.get(value, i);
119: if (api.isPersistable(element)) {
120: processPersistable(element);
121: }
122: }
123: }
124: } else {
125: // Primitive, or primitive array, or some unsupported container type
126: }
127: }
128: }
129:
130: /**
131: * Method to store a boolean field.
132: * @param fieldNumber Number of the field (absolute)
133: * @param value Value of the field
134: */
135: public void storeBooleanField(int fieldNumber, boolean value) {
136: // Do nothing
137: }
138:
139: /**
140: * Method to store a byte field.
141: * @param fieldNumber Number of the field (absolute)
142: * @param value Value of the field
143: */
144: public void storeByteField(int fieldNumber, byte value) {
145: // Do nothing
146: }
147:
148: /**
149: * Method to store a char field.
150: * @param fieldNumber Number of the field (absolute)
151: * @param value Value of the field
152: */
153: public void storeCharField(int fieldNumber, char value) {
154: // Do nothing
155: }
156:
157: /**
158: * Method to store a double field.
159: * @param fieldNumber Number of the field (absolute)
160: * @param value Value of the field
161: */
162: public void storeDoubleField(int fieldNumber, double value) {
163: // Do nothing
164: }
165:
166: /**
167: * Method to store a float field.
168: * @param fieldNumber Number of the field (absolute)
169: * @param value Value of the field
170: */
171: public void storeFloatField(int fieldNumber, float value) {
172: // Do nothing
173: }
174:
175: /**
176: * Method to store an int field.
177: * @param fieldNumber Number of the field (absolute)
178: * @param value Value of the field
179: */
180: public void storeIntField(int fieldNumber, int value) {
181: // Do nothing
182: }
183:
184: /**
185: * Method to store a long field.
186: * @param fieldNumber Number of the field (absolute)
187: * @param value Value of the field
188: */
189: public void storeLongField(int fieldNumber, long value) {
190: // Do nothing
191: }
192:
193: /**
194: * Method to store a short field.
195: * @param fieldNumber Number of the field (absolute)
196: * @param value Value of the field
197: */
198: public void storeShortField(int fieldNumber, short value) {
199: // Do nothing
200: }
201:
202: /**
203: * Method to store a string field.
204: * @param fieldNumber Number of the field (absolute)
205: * @param value Value of the field
206: */
207: public void storeStringField(int fieldNumber, String value) {
208: // Do nothing
209: }
210: }
|