001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.customization;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.entity.GenericDelegator;
025: import org.ofbiz.entity.GenericEntityException;
026: import org.ofbiz.entity.GenericValue;
027: import org.ofbiz.entity.model.ModelEntity;
028:
029: import com.sourcetap.sfa.event.DataMatrix;
030: import com.sourcetap.sfa.event.GenericEventProcessor;
031: import com.sourcetap.sfa.util.UserInfo;
032:
033: /**
034: * DOCUMENT ME!
035: *
036: */
037: public class UIDisplayObjectEventProcessor extends
038: GenericEventProcessor {
039: public static final String module = UIDisplayObjectEventProcessor.class
040: .getName();
041:
042: /**
043: * DOCUMENT ME!
044: *
045: * @param userInfo
046: * @param delegator
047: * @param dataMatrix
048: *
049: * @return
050: */
051: protected int postUpdate(UserInfo userInfo,
052: GenericDelegator delegator, DataMatrix dataMatrix) {
053:
054: return STATUS_CONTINUE;
055: }
056:
057: /**
058: * DOCUMENT ME!
059: *
060: * @param userInfo
061: * @param delegator
062: * @param dataMatrix
063: *
064: * @return
065: */
066: protected int postInsert(UserInfo userInfo,
067: GenericDelegator delegator, DataMatrix dataMatrix) {
068:
069: // Get the current values.
070: GenericValue uiDisplayObjectGV = dataMatrix.getCurrentBuffer()
071: .getGenericValue(0, 0);
072: String displayObjectId = uiDisplayObjectGV
073: .getString("displayObjectId");
074: String displayTypeId = uiDisplayObjectGV
075: .getString("displayTypeId");
076:
077: // Get the original values so we can see if this is a copy or a regular insert.
078: GenericValue uiDisplayObjectgGVOrig = dataMatrix
079: .getOriginalBuffer().getGenericValue(0, 0);
080: String displayObjectIdOrig = uiDisplayObjectgGVOrig
081: .getString("displayObjectId");
082: String displayTypeIdOrig = uiDisplayObjectgGVOrig
083: .getString("displayTypeId");
084:
085: if (displayObjectIdOrig.equals("")
086: || (displayObjectIdOrig == null)) {
087: // The display object ID was not filled in yet when the screen was displayed last. This is
088: // a regular insert.
089:
090: // Insert all the attributes for this display object's display type.
091: HashMap uiDisplayAttribFindMap = new HashMap();
092: uiDisplayAttribFindMap.put("displayTypeId", displayTypeId);
093:
094: try {
095: List uiDisplayAttribGVL = delegator.findByAnd(
096: "UiDisplayAttrib", uiDisplayAttribFindMap);
097: Iterator uiDisplayAttribGVI = uiDisplayAttribGVL
098: .iterator();
099: ModelEntity uiDisplayObjectAttribME = delegator
100: .getModelEntity("UiDisplayObjectAttrib");
101:
102: while (uiDisplayAttribGVI.hasNext()) {
103: GenericValue uiDisplayAttribGV = (GenericValue) uiDisplayAttribGVI
104: .next();
105: String displayAttribId = (uiDisplayAttribGV
106: .getString("displayAttribId") == null) ? ""
107: : uiDisplayAttribGV
108: .getString("displayAttribId");
109: String displayAttribName = (uiDisplayAttribGV
110: .getString("name") == null) ? ""
111: : uiDisplayAttribGV.getString("name");
112: GenericValue uiDisplayObjectAttribGV = new GenericValue(
113: uiDisplayObjectAttribME);
114: uiDisplayObjectAttribGV.setDelegator(delegator);
115: uiDisplayObjectAttribGV.set("displayObjectId",
116: displayObjectId);
117: uiDisplayObjectAttribGV.set("displayTypeId",
118: displayTypeId);
119: uiDisplayObjectAttribGV.set("displayAttribId",
120: displayAttribId);
121: uiDisplayObjectAttribGV.set("attributeValue", "");
122:
123: delegator.create(uiDisplayObjectAttribGV);
124: }
125: } catch (GenericEntityException e) {
126: Debug
127: .logError(
128: "[UIDisplayObjectWebEventProcessor.postInsert] Error looking for display attributes: "
129: + e.getLocalizedMessage(),
130: module);
131: }
132: } else {
133: // This is a copy of an existing display object.
134:
135: // Copy all the attributes from the original display object.
136: HashMap uiDisplayObjectAttribFindMap = new HashMap();
137: uiDisplayObjectAttribFindMap.put("displayTypeId",
138: displayTypeIdOrig);
139: uiDisplayObjectAttribFindMap.put("displayObjectId",
140: displayObjectIdOrig);
141:
142: try {
143: // Get the List of original display object attributes.
144: List uiDisplayObjectAttribGVLOrig = delegator
145: .findByAnd("UiDisplayObjectAttrib",
146: uiDisplayObjectAttribFindMap);
147: Iterator uiDisplayObjectAttribGVIOrig = uiDisplayObjectAttribGVLOrig
148: .iterator();
149: ModelEntity uiDisplayObjectAttribME = delegator
150: .getModelEntity("UiDisplayObjectAttrib");
151:
152: while (uiDisplayObjectAttribGVIOrig.hasNext()) {
153: GenericValue uiDisplayObjectAttribGVOrig = (GenericValue) uiDisplayObjectAttribGVIOrig
154: .next();
155: String displayAttribIdOrig = (uiDisplayObjectAttribGVOrig
156: .getString("displayAttribId") == null) ? ""
157: : uiDisplayObjectAttribGVOrig
158: .getString("displayAttribId");
159:
160: // Get values from the original display object attribute.
161: String displayAttribValueOrig = (uiDisplayObjectAttribGVOrig
162: .getString("attributeValue") == null) ? ""
163: : uiDisplayObjectAttribGVOrig
164: .getString("attributeValue");
165:
166: // Create the new display object attribute using the new display object ID.
167: GenericValue uiDisplayObjectAttribGV = new GenericValue(
168: uiDisplayObjectAttribME);
169: uiDisplayObjectAttribGV.setDelegator(delegator);
170: uiDisplayObjectAttribGV.set("displayObjectId",
171: displayObjectId);
172: uiDisplayObjectAttribGV.set("displayTypeId",
173: displayTypeIdOrig);
174: uiDisplayObjectAttribGV.set("displayAttribId",
175: displayAttribIdOrig);
176: uiDisplayObjectAttribGV.set("attributeValue",
177: displayAttribValueOrig);
178:
179: delegator.create(uiDisplayObjectAttribGV);
180: }
181: } catch (GenericEntityException e) {
182: Debug
183: .logError(
184: "[UIDisplayObjectWebEventProcessor.postInsert] Error looking for original display object attributes: "
185: + e.getLocalizedMessage(),
186: module);
187: }
188: }
189:
190: return STATUS_CONTINUE;
191: }
192:
193: /**
194: * DOCUMENT ME!
195: *
196: * @param userInfo
197: * @param delegator
198: * @param originatingEntityName
199: * @param entityGV
200: *
201: * @return
202: */
203: public int deleteAllRelated(UserInfo userInfo,
204: GenericDelegator delegator, String originatingEntityName,
205: GenericValue entityGV) {
206:
207: int status = STATUS_CONTINUE;
208:
209: // Delete related display object attributes.
210: status = deleteOneRelated(userInfo, delegator, entityGV, "",
211: "UiDisplayObjectAttrib", originatingEntityName,
212: new GenericEventProcessor());
213:
214: return status;
215: }
216: }
|