/*
*
* Copyright (c) 2004 SourceTap - www.sourcetap.com
*
* The contents of this file are subject to the SourceTap Public License
* ("License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
*/
package com.sourcetap.sfa.customization;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.model.ModelEntity;
import com.sourcetap.sfa.event.DataMatrix;
import com.sourcetap.sfa.event.GenericEventProcessor;
import com.sourcetap.sfa.util.UserInfo;
/**
* DOCUMENT ME!
*
*/
public class UIDisplayObjectEventProcessor extends GenericEventProcessor {
public static final String module = UIDisplayObjectEventProcessor.class.getName();
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param dataMatrix
*
* @return
*/
protected int postUpdate(UserInfo userInfo, GenericDelegator delegator,
DataMatrix dataMatrix) {
return STATUS_CONTINUE;
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param dataMatrix
*
* @return
*/
protected int postInsert(UserInfo userInfo, GenericDelegator delegator,
DataMatrix dataMatrix) {
// Get the current values.
GenericValue uiDisplayObjectGV = dataMatrix.getCurrentBuffer()
.getGenericValue(0, 0);
String displayObjectId = uiDisplayObjectGV.getString("displayObjectId");
String displayTypeId = uiDisplayObjectGV.getString("displayTypeId");
// Get the original values so we can see if this is a copy or a regular insert.
GenericValue uiDisplayObjectgGVOrig = dataMatrix.getOriginalBuffer()
.getGenericValue(0, 0);
String displayObjectIdOrig = uiDisplayObjectgGVOrig.getString(
"displayObjectId");
String displayTypeIdOrig = uiDisplayObjectgGVOrig.getString(
"displayTypeId");
if (displayObjectIdOrig.equals("") || (displayObjectIdOrig == null)) {
// The display object ID was not filled in yet when the screen was displayed last. This is
// a regular insert.
// Insert all the attributes for this display object's display type.
HashMap uiDisplayAttribFindMap = new HashMap();
uiDisplayAttribFindMap.put("displayTypeId", displayTypeId);
try {
List uiDisplayAttribGVL = delegator.findByAnd("UiDisplayAttrib",
uiDisplayAttribFindMap);
Iterator uiDisplayAttribGVI = uiDisplayAttribGVL.iterator();
ModelEntity uiDisplayObjectAttribME = delegator.getModelEntity(
"UiDisplayObjectAttrib");
while (uiDisplayAttribGVI.hasNext()) {
GenericValue uiDisplayAttribGV = (GenericValue) uiDisplayAttribGVI.next();
String displayAttribId = (uiDisplayAttribGV.getString(
"displayAttribId") == null) ? ""
: uiDisplayAttribGV.getString(
"displayAttribId");
String displayAttribName = (uiDisplayAttribGV.getString(
"name") == null) ? ""
: uiDisplayAttribGV.getString(
"name");
GenericValue uiDisplayObjectAttribGV = new GenericValue(uiDisplayObjectAttribME);
uiDisplayObjectAttribGV.setDelegator(delegator);
uiDisplayObjectAttribGV.set("displayObjectId",
displayObjectId);
uiDisplayObjectAttribGV.set("displayTypeId", displayTypeId);
uiDisplayObjectAttribGV.set("displayAttribId",
displayAttribId);
uiDisplayObjectAttribGV.set("attributeValue", "");
delegator.create(uiDisplayObjectAttribGV);
}
} catch (GenericEntityException e) {
Debug.logError(
"[UIDisplayObjectWebEventProcessor.postInsert] Error looking for display attributes: " +
e.getLocalizedMessage(), module);
}
} else {
// This is a copy of an existing display object.
// Copy all the attributes from the original display object.
HashMap uiDisplayObjectAttribFindMap = new HashMap();
uiDisplayObjectAttribFindMap.put("displayTypeId", displayTypeIdOrig);
uiDisplayObjectAttribFindMap.put("displayObjectId",
displayObjectIdOrig);
try {
// Get the List of original display object attributes.
List uiDisplayObjectAttribGVLOrig = delegator.findByAnd("UiDisplayObjectAttrib",
uiDisplayObjectAttribFindMap);
Iterator uiDisplayObjectAttribGVIOrig = uiDisplayObjectAttribGVLOrig.iterator();
ModelEntity uiDisplayObjectAttribME = delegator.getModelEntity(
"UiDisplayObjectAttrib");
while (uiDisplayObjectAttribGVIOrig.hasNext()) {
GenericValue uiDisplayObjectAttribGVOrig = (GenericValue) uiDisplayObjectAttribGVIOrig.next();
String displayAttribIdOrig = (uiDisplayObjectAttribGVOrig.getString(
"displayAttribId") == null) ? ""
: uiDisplayObjectAttribGVOrig.getString(
"displayAttribId");
// Get values from the original display object attribute.
String displayAttribValueOrig = (uiDisplayObjectAttribGVOrig.getString(
"attributeValue") == null) ? ""
: uiDisplayObjectAttribGVOrig.getString(
"attributeValue");
// Create the new display object attribute using the new display object ID.
GenericValue uiDisplayObjectAttribGV = new GenericValue(uiDisplayObjectAttribME);
uiDisplayObjectAttribGV.setDelegator(delegator);
uiDisplayObjectAttribGV.set("displayObjectId",
displayObjectId);
uiDisplayObjectAttribGV.set("displayTypeId",
displayTypeIdOrig);
uiDisplayObjectAttribGV.set("displayAttribId",
displayAttribIdOrig);
uiDisplayObjectAttribGV.set("attributeValue",
displayAttribValueOrig);
delegator.create(uiDisplayObjectAttribGV);
}
} catch (GenericEntityException e) {
Debug.logError(
"[UIDisplayObjectWebEventProcessor.postInsert] Error looking for original display object attributes: " +
e.getLocalizedMessage(), module);
}
}
return STATUS_CONTINUE;
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param originatingEntityName
* @param entityGV
*
* @return
*/
public int deleteAllRelated(UserInfo userInfo, GenericDelegator delegator,
String originatingEntityName, GenericValue entityGV) {
int status = STATUS_CONTINUE;
// Delete related display object attributes.
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"UiDisplayObjectAttrib", originatingEntityName,
new GenericEventProcessor());
return status;
}
}