001: /**********************************************************************
002: Copyright (c) 2006 Erik Bengtson 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.util.Collection;
020: import java.util.Map;
021:
022: import org.jpox.StateManager;
023: import org.jpox.api.ApiAdapter;
024:
025: /**
026: * Manager that nullifies any Collection/Map/PC fields of the object.
027: *
028: * @version $Revision: 1.5 $
029: */
030: public class NullifyRelationFieldManager extends AbstractFieldManager {
031: /** State Manager for the object. */
032: private final StateManager sm;
033:
034: /**
035: * Constructor.
036: * @param sm the StateManager
037: */
038: public NullifyRelationFieldManager(StateManager sm) {
039: this .sm = sm;
040: }
041:
042: /**
043: * Accessor for object field.
044: * @param fieldNumber Number of field
045: * @return Object value
046: */
047: public Object fetchObjectField(int fieldNumber) {
048: Object value = sm.provideField(fieldNumber);
049: ApiAdapter api = sm.getObjectManager().getApiAdapter();
050: if (value == null) {
051: return null;
052: } else if (api.isPersistable(value)) {
053: // Process PC fields
054: sm.makeDirty(fieldNumber);
055: return null;
056: } else if (value instanceof Collection) {
057: // Process Collection fields
058: sm.makeDirty(fieldNumber);
059: ((Collection) value).clear();
060: return value;
061: } else if (value instanceof Map) {
062: // Process Map fields
063: sm.makeDirty(fieldNumber);
064: ((Map) value).clear();
065: return value;
066: } else if (value.getClass().isArray()
067: && Object.class.isAssignableFrom(value.getClass()
068: .getComponentType())) {
069: // Process object array fields
070: // TODO Check if the array element is PC and nullify
071: }
072:
073: //do not need to nullify fields that are not references and resides embedded in this object
074: return value;
075: }
076:
077: /**
078: * Accessor for boolean field.
079: * @param fieldNumber Number of field
080: * @return Object value
081: */
082: public boolean fetchBooleanField(int fieldNumber) {
083: return true;
084: }
085:
086: /**
087: * Accessor for char field.
088: * @param fieldNumber Number of field
089: * @return Object value
090: */
091: public char fetchCharField(int fieldNumber) {
092: return '0';
093: }
094:
095: /**
096: * Accessor for byte field.
097: * @param fieldNumber Number of field
098: * @return Object value
099: */
100: public byte fetchByteField(int fieldNumber) {
101: return (byte) 0;
102: }
103:
104: /**
105: * Accessor for double field.
106: * @param fieldNumber Number of field
107: * @return Object value
108: */
109: public double fetchDoubleField(int fieldNumber) {
110: return 0;
111: }
112:
113: /**
114: * Accessor for float field.
115: * @param fieldNumber Number of field
116: * @return Object value
117: */
118: public float fetchFloatField(int fieldNumber) {
119: return 0;
120: }
121:
122: /**
123: * Accessor for int field.
124: * @param fieldNumber Number of field
125: * @return Object value
126: */
127: public int fetchIntField(int fieldNumber) {
128: return 0;
129: }
130:
131: /**
132: * Accessor for long field.
133: * @param fieldNumber Number of field
134: * @return Object value
135: */
136: public long fetchLongField(int fieldNumber) {
137: return 0;
138: }
139:
140: /**
141: * Accessor for short field.
142: * @param fieldNumber Number of field
143: * @return Object value
144: */
145: public short fetchShortField(int fieldNumber) {
146: return 0;
147: }
148:
149: /**
150: * Accessor for String field.
151: * @param fieldNumber Number of field
152: * @return Object value
153: */
154: public String fetchStringField(int fieldNumber) {
155: return "";
156: }
157: }
|