0001: /*
0002: *
0003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
0004: *
0005: * The contents of this file are subject to the SourceTap Public License
0006: * ("License"); You may not use this file except in compliance with the
0007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
0008: * Software distributed under the License is distributed on an "AS IS" basis,
0009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
0010: * the specific language governing rights and limitations under the License.
0011: *
0012: * The above copyright notice and this permission notice shall be included
0013: * in all copies or substantial portions of the Software.
0014: *
0015: */
0016:
0017: package com.sourcetap.sfa.event;
0018:
0019: import java.util.ArrayList;
0020: import java.util.Collection;
0021: import java.util.HashMap;
0022: import java.util.Iterator;
0023: import java.util.LinkedList;
0024: import java.util.List;
0025: import java.util.Map;
0026: import java.util.Vector;
0027:
0028: import org.ofbiz.base.util.Debug;
0029: import org.ofbiz.base.util.UtilTimer;
0030: import org.ofbiz.entity.GenericDelegator;
0031: import org.ofbiz.entity.GenericEntityException;
0032: import org.ofbiz.entity.GenericModelException;
0033: import org.ofbiz.entity.GenericPK;
0034: import org.ofbiz.entity.GenericValue;
0035: import org.ofbiz.entity.condition.EntityOperator;
0036: import org.ofbiz.entity.model.ModelEntity;
0037: import org.ofbiz.entity.model.ModelField;
0038: import org.ofbiz.entity.model.ModelKeyMap;
0039: import org.ofbiz.entity.model.ModelRelation;
0040: import org.ofbiz.entity.util.EntityListIterator;
0041:
0042: import com.sourcetap.sfa.replication.GenericReplicator;
0043: import com.sourcetap.sfa.security.SecurityLinkInfo;
0044: import com.sourcetap.sfa.security.SecurityWrapper;
0045: import com.sourcetap.sfa.ui.UIScreenSectionEntity;
0046: import com.sourcetap.sfa.util.EntityHelper;
0047: import com.sourcetap.sfa.util.QueryInfo;
0048: import com.sourcetap.sfa.util.UserInfo;
0049:
0050: /**
0051: * DOCUMENT ME!
0052: *
0053: */
0054: public class GenericEventProcessor {
0055: public static final String module = GenericEventProcessor.class
0056: .getName();
0057:
0058: private static final boolean TIMER = false;
0059: public static final int STATUS_ERROR = -1;
0060: public static final int STATUS_CANCEL = 0;
0061: public static final int STATUS_CONTINUE = 1;
0062: public static final int RETRIEVE_METHOD_ALL = 1;
0063: public static final int RETRIEVE_METHOD_AND = 2;
0064: public static final int RETRIEVE_METHOD_CLAUSE = 3;
0065: public static final int RETRIEVE_METHOD_LIKE = 4;
0066: public static final int RETRIEVE_METHOD_PK = 5;
0067: private int rowOffset = 0;
0068: private int fetchSize = 1;
0069: private boolean hasMoreRows = false;
0070: private int totalRows = 0;
0071:
0072: /**
0073: * DOCUMENT ME!
0074: *
0075: * @param iFetchSize
0076: */
0077: public void setFetchSize(int iFetchSize) {
0078: fetchSize = iFetchSize;
0079: }
0080:
0081: /**
0082: * DOCUMENT ME!
0083: *
0084: * @param iRowOffset
0085: */
0086: public void setRowOffset(int iRowOffset) {
0087: rowOffset = iRowOffset;
0088: }
0089:
0090: /**
0091: * DOCUMENT ME!
0092: *
0093: * @return
0094: */
0095: public int getFetchSize() {
0096: return fetchSize;
0097: }
0098:
0099: /**
0100: * DOCUMENT ME!
0101: *
0102: * @return
0103: */
0104: public int getRowOffset() {
0105: return rowOffset;
0106: }
0107:
0108: /**
0109: * DOCUMENT ME!
0110: *
0111: * @return
0112: */
0113: public boolean getHasMoreData() {
0114: return hasMoreRows;
0115: }
0116:
0117: /**
0118: * DOCUMENT ME!
0119: *
0120: * @return
0121: */
0122: public int getTotalRows() {
0123: return totalRows;
0124: }
0125:
0126: /*
0127: protected String entityName = "";
0128: public String getEntityName() {
0129: return entityName;
0130: }
0131: public void setEntityName(String entityName_) {
0132: entityName = entityName_;
0133: }
0134: */
0135:
0136: //-------------------------------------------------------------------------
0137: // RETRIEVE - Retrieves an entity and associated entities from the data base.
0138: //-------------------------------------------------------------------------
0139: public int processRetrieve(UserInfo userInfo,
0140: String mainEntityName, int method, Map fields,
0141: List orderBy, QueryInfo queryInfo,
0142: List relatedSearchClauses, GenericDelegator delegator,
0143: DataMatrix dataMatrix) throws GenericEntityException {
0144:
0145: if ((mainEntityName == null) || mainEntityName.equals("")) {
0146: throw new GenericEntityException(
0147: "Main entity name is required.");
0148: }
0149:
0150: if ((method != RETRIEVE_METHOD_ALL)
0151: && (method != RETRIEVE_METHOD_AND)
0152: && (method != RETRIEVE_METHOD_CLAUSE)
0153: && (method != RETRIEVE_METHOD_LIKE)
0154: && (method != RETRIEVE_METHOD_PK)) {
0155: throw new GenericEntityException("Invalid search method.");
0156: }
0157:
0158: int status = STATUS_CONTINUE;
0159:
0160: // Trigger the pre-retrieve event.
0161: status = this .preRetrieve(userInfo, mainEntityName, method,
0162: fields, orderBy, queryInfo, relatedSearchClauses,
0163: delegator, dataMatrix);
0164:
0165: if (status != STATUS_CONTINUE) {
0166: return status;
0167: }
0168:
0169: // Trigger the do-retrieve event.
0170: status = this .retrieve(userInfo, mainEntityName, method,
0171: fields, orderBy, queryInfo, relatedSearchClauses,
0172: delegator, dataMatrix);
0173:
0174: if (status != STATUS_CONTINUE) {
0175: return status;
0176: }
0177:
0178: // Trigger the post-retrieve event.
0179: status = this .postRetrieve(userInfo, mainEntityName, method,
0180: fields, orderBy, queryInfo, relatedSearchClauses,
0181: delegator, dataMatrix);
0182:
0183: return status;
0184: }
0185:
0186: /**
0187: * DOCUMENT ME!
0188: *
0189: * @param userInfo
0190: * @param mainEntityName
0191: * @param method
0192: * @param fields
0193: * @param orderBy
0194: * @param queryInfo criteria to be used in search
0195: * @param relatedSearchClauses
0196: * @param delegator
0197: * @param dataMatrix
0198: *
0199: * @return
0200: */
0201: protected int preRetrieve(UserInfo userInfo, String mainEntityName,
0202: int method, Map fields, List orderBy, QueryInfo queryInfo,
0203: List relatedSearchClauses, GenericDelegator delegator,
0204: DataMatrix dataMatrix) {
0205:
0206: return STATUS_CONTINUE;
0207: }
0208:
0209: /**
0210: * DOCUMENT ME!
0211: *
0212: * @param userInfo
0213: * @param mainEntityName
0214: * @param method
0215: * @param fields
0216: * @param orderBy
0217: * @param queryInfo criteria to be used in search
0218: * @param relatedSearchClauses
0219: * @param delegator
0220: * @param dataMatrix
0221: *
0222: * @return
0223: */
0224: public int retrieve(UserInfo userInfo, String mainEntityName,
0225: int method, Map fields, List orderBy, QueryInfo queryInfo,
0226: List relatedSearchClauses, GenericDelegator delegator,
0227: DataMatrix dataMatrix) {
0228:
0229: UtilTimer timer = new UtilTimer();
0230:
0231: if (TIMER) {
0232: timer.timerString(2,
0233: "[GenericEventProcessor.retrieve] Start");
0234: }
0235:
0236: Debug.logVerbose(
0237: "Getting entity info to see if it has an accountId field = "
0238: + String.valueOf(method), module);
0239:
0240: SecurityLinkInfo securityInfo = getSecurityLinkInfo(userInfo,
0241: delegator);
0242:
0243: if (securityInfo == null) {
0244: Debug.logVerbose("No securityInfo defined", module);
0245: } else {
0246: Debug.logVerbose("securityInfo = "
0247: + securityInfo.toString(), module);
0248: }
0249:
0250: Debug.logVerbose("About to retrieve main entity. Method = "
0251: + String.valueOf(method), module);
0252: Debug.logVerbose("orderBy: " + orderBy.toString(), module);
0253: Debug.logVerbose("Retrieve Method: " + String.valueOf(method),
0254: module);
0255:
0256: // Retrieve the main entity or entities.
0257: List mainGVL = retrieveMainEntity(securityInfo, userInfo,
0258: mainEntityName, method, fields, orderBy, queryInfo,
0259: delegator);
0260:
0261: totalRows = mainGVL.size();
0262:
0263: Debug.logVerbose("GEP: rowOffset = " + rowOffset
0264: + " fetchSize = " + fetchSize, module);
0265:
0266: Debug.logVerbose("Finished retrieving main entity.", module);
0267:
0268: Iterator mainGVI = mainGVL.iterator();
0269: int rowCount = 0;
0270:
0271: while (mainGVI.hasNext()) {
0272: rowCount++;
0273:
0274: GenericValue mainGV1 = (GenericValue) mainGVI.next();
0275: GenericValue mainGV = EntityHelper
0276: .getPrimaryGVFromDynamicGV(delegator, mainGV1,
0277: mainEntityName);
0278:
0279: if (TIMER) {
0280: timer.timerString(2,
0281: "[GenericEventProcessor.retrieve] Start processing row "
0282: + String.valueOf(rowCount));
0283: }
0284:
0285: // For each instance of the main entity, start a vector of entities, and then
0286: // retrieve the associated entities and add them also.
0287: Vector outGVV = new Vector();
0288: outGVV.add(mainGV);
0289:
0290: Debug.logVerbose("About to retrieve related entities.",
0291: module);
0292:
0293: // Loop through the relations to get related entities.
0294: int entityCount = 0;
0295: Iterator relatedSearchClauseI = relatedSearchClauses
0296: .iterator();
0297:
0298: while (relatedSearchClauseI.hasNext()) {
0299: UIScreenSectionEntity relatedSearchClause = (UIScreenSectionEntity) relatedSearchClauseI
0300: .next();
0301: entityCount++;
0302:
0303: String relationRelEntityName = relatedSearchClause
0304: .getEntityName();
0305:
0306: if (TIMER) {
0307: timer.timerString(2,
0308: "[GenericEventProcessor.retrieve] Start appending related entity "
0309: + String.valueOf(entityCount)
0310: + " (" + relationRelEntityName
0311: + ") for row "
0312: + String.valueOf(rowCount));
0313: }
0314: //TODO copy mainGV to primaryEntityGV if this is a dynamic view
0315: GenericValue relatedGV = retrieveOneRelatedGV(mainGV,
0316: relatedSearchClause, outGVV, userInfo,
0317: delegator);
0318:
0319: if (relatedGV == null) {
0320: // Related generic value was not found. Create a new one.
0321: relatedGV = new GenericValue(delegator
0322: .getModelEntity(relationRelEntityName));
0323: relatedGV.setDelegator(delegator);
0324: }
0325:
0326: // Add the new related generic value to the entity vector with the main entity.
0327: Debug.logVerbose("About to append related entity \""
0328: + relationRelEntityName
0329: + "\" onto generic value map.", module);
0330:
0331: outGVV.add(relatedGV);
0332:
0333: if (TIMER) {
0334: timer.timerString(2,
0335: "[GenericEventProcessor.retrieve] End appending related entity "
0336: + String.valueOf(entityCount)
0337: + " for row "
0338: + String.valueOf(rowCount));
0339: }
0340: }
0341:
0342: Debug.logVerbose("Generic value vector: "
0343: + outGVV.toString(), module);
0344:
0345: // Append the vector of generic entities onto the List.
0346: Debug
0347: .logVerbose(
0348: "About to append generic value vector onto generic value vector List.",
0349: module);
0350:
0351: dataMatrix.getCurrentBuffer().addContentsRow(outGVV);
0352:
0353: if (TIMER) {
0354: timer
0355: .timerString(2,
0356: "[GenericEventProcessor.retrieve] Added row to current buffer.");
0357: }
0358:
0359: Debug.logVerbose("Finished appending row "
0360: + String.valueOf(rowCount), module);
0361: }
0362:
0363: // if (DEBUG) Debug.logVerbose("Data matrix current buffer: " + dataMatrix.getCurrentBuffer().getContents().toString());
0364: Debug.logVerbose("Finished retrieve method.", module);
0365:
0366: if (TIMER) {
0367: timer
0368: .timerString(2,
0369: "[GenericEventProcessor.retrieve] End");
0370: }
0371:
0372: return STATUS_CONTINUE;
0373: }
0374:
0375: /**
0376: * DOCUMENT ME!
0377: *
0378: * @param userInfo
0379: * @param mainEntityName
0380: * @param method
0381: * @param fields
0382: * @param orderBy
0383: * @param queryInfo criteria to be used in search
0384: * @param relatedSearchClauses
0385: * @param delegator
0386: * @param dataMatrix
0387: *
0388: * @return
0389: */
0390: protected int postRetrieve(UserInfo userInfo,
0391: String mainEntityName, int method, Map fields,
0392: List orderBy, QueryInfo queryInfo,
0393: List relatedSearchClauses, GenericDelegator delegator,
0394: DataMatrix dataMatrix) {
0395:
0396: return STATUS_CONTINUE;
0397: }
0398:
0399: /**
0400: * DOCUMENT ME!
0401: *
0402: * @param securityInfo
0403: * @param userInfo
0404: * @param mainEntityName
0405: * @param method
0406: * @param fields
0407: * @param orderBy
0408: * @param queryInfo criteria to be used in search
0409: * @param delegator
0410: *
0411: * @return
0412: */
0413: protected List retrieveMainEntity(SecurityLinkInfo securityInfo,
0414: UserInfo userInfo, String mainEntityName, int method,
0415: Map fields, List orderBy, QueryInfo queryInfo,
0416: GenericDelegator delegator) {
0417: UtilTimer timer = new UtilTimer();
0418:
0419: GenericValue mainGV = null;
0420: List mainGVL = null;
0421: ArrayList queryList = new ArrayList();
0422: EntityListIterator eli = null;
0423:
0424: switch (method) {
0425: case RETRIEVE_METHOD_ALL:
0426:
0427: // do nothing, leave condition = null
0428: break;
0429:
0430: case RETRIEVE_METHOD_AND:
0431:
0432: if (fields != null) {
0433: Iterator entries = fields.entrySet().iterator();
0434: while (entries.hasNext()) {
0435: Map.Entry anEntry = (Map.Entry) entries.next();
0436: queryInfo.addCondition(mainEntityName,
0437: (String) anEntry.getKey(),
0438: EntityOperator.EQUALS, anEntry.getValue());
0439: }
0440: }
0441: break;
0442:
0443: case RETRIEVE_METHOD_CLAUSE:
0444:
0445: // nothing needed, as queryInfo is already setup
0446: break;
0447:
0448: case RETRIEVE_METHOD_LIKE:
0449:
0450: if (fields != null) {
0451: Iterator entries = fields.entrySet().iterator();
0452: while (entries.hasNext()) {
0453: Map.Entry anEntry = (Map.Entry) entries.next();
0454: queryInfo.addCondition(mainEntityName,
0455: (String) anEntry.getKey(),
0456: EntityOperator.EQUALS, ((String) anEntry
0457: .getValue()).replace('*', '%')
0458: + "%");
0459: }
0460: }
0461:
0462: break;
0463:
0464: case RETRIEVE_METHOD_PK:
0465:
0466: // Get one main entity using a primary key built from the field map passed in.
0467:
0468: if (securityInfo == null) {
0469: try {
0470: mainGV = delegator.findByPrimaryKey(mainEntityName,
0471: fields);
0472: } catch (GenericEntityException e) {
0473: Debug
0474: .logError(
0475: "An error occurred in the "
0476: + "GenericDelegator.findByPrimaryKey method: "
0477: + e.getLocalizedMessage(),
0478: module);
0479: }
0480:
0481: if (mainGV != null) {
0482: // The entity was found. Store it in a List
0483: mainGVL = new LinkedList();
0484: mainGVL.add(mainGV);
0485: return mainGVL;
0486: }
0487: } else {
0488: if (fields != null) {
0489: Iterator entries = fields.entrySet().iterator();
0490: while (entries.hasNext()) {
0491: Map.Entry anEntry = (Map.Entry) entries.next();
0492: queryInfo.addCondition(mainEntityName,
0493: (String) anEntry.getKey(),
0494: EntityOperator.EQUALS, anEntry
0495: .getValue());
0496: }
0497: }
0498: }
0499:
0500: break;
0501: }
0502:
0503: Debug.logVerbose("calling (findIteratorByCondition)", module);
0504: Debug.logVerbose("mainEntityName: " + mainEntityName, module);
0505:
0506: if (TIMER) {
0507: timer
0508: .timerString(3,
0509: "[GenericEventProcessor.retrieveMainEntity] Start findIteratorByCondition)");
0510: }
0511:
0512: if (securityInfo == null) {
0513: try {
0514: queryInfo.setOrderBy(orderBy);
0515: eli = queryInfo.getQueryIterator();
0516: } catch (GenericEntityException e) {
0517: Debug.logError("An error occurred in the "
0518: + "GenericDelegator.findByCondition method: "
0519: + e.getLocalizedMessage(), module);
0520: }
0521: } else {
0522: try {
0523:
0524: eli = SecurityWrapper.findListIteratorByCondition(
0525: mainEntityName, queryInfo, orderBy, userInfo,
0526: securityInfo, delegator);
0527: } catch (GenericEntityException e) {
0528: Debug.logError("An error occurred in the "
0529: + "SecurityWrapper.findByCondition method: "
0530: + e.getLocalizedMessage(), module);
0531: }
0532: }
0533:
0534: if (eli != null) {
0535: try {
0536: mainGVL = eli.getPartialList(getRowOffset() + 1,
0537: getFetchSize());
0538:
0539: hasMoreRows = false;
0540:
0541: // check next value since we close the cursor anyway.
0542: if ((mainGVL.size() == getFetchSize())
0543: && (eli.next() != null))
0544: hasMoreRows = true;
0545:
0546: // check to see if we need to save the output to a named list.
0547: if ((queryInfo.getSaveResultListId() != null)
0548: && (queryInfo.getSaveResultListId().length() > 0)) {
0549: eli.beforeFirst();
0550: GenericValue gv = null;
0551: ModelEntity me = delegator
0552: .getModelEntity("UiListItem");
0553: ModelEntity primaryME = delegator
0554: .getModelEntity(mainEntityName);
0555:
0556: if (primaryME.getPksSize() != 1)
0557: throw new IllegalArgumentException(
0558: "Multi-Field primary Keys not allows in Save Result List");
0559:
0560: String keyFieldName = primaryME.getPk(0).getName();
0561:
0562: while ((gv = (GenericValue) eli.next()) != null) {
0563: GenericValue itemGV = new GenericValue(me);
0564: itemGV.setDelegator(delegator);
0565: itemGV.set("listItemId", GenericReplicator
0566: .getNextSeqId("UiListItem", delegator));
0567: itemGV.set("listId", queryInfo
0568: .getSaveResultListId());
0569: itemGV.set("entityName", mainEntityName);
0570: String entityId = gv.getString(keyFieldName);
0571: itemGV.set("entityId", entityId);
0572: itemGV.set("status", "new");
0573:
0574: delegator.create(itemGV);
0575: }
0576: }
0577:
0578: eli.close();
0579:
0580: } catch (GenericEntityException e) {
0581: Debug.logError(
0582: "An error occurred in the getPartialList method: "
0583: + e.getLocalizedMessage(), module);
0584: }
0585: }
0586: if (TIMER) {
0587: timer
0588: .timerString(3,
0589: "[GenericEventProcessor.retrieveMainEntity] End findIteratorByCondition");
0590: }
0591:
0592: return mainGVL;
0593: }
0594:
0595: /**
0596: * DOCUMENT ME!
0597: *
0598: * @param mainGV
0599: * @param relatedSearchClause
0600: * @param outGVV
0601: * @param userInfo
0602: * @param delegator
0603: *
0604: * @return
0605: */
0606: protected GenericValue retrieveOneRelatedGV(GenericValue mainGV,
0607: UIScreenSectionEntity relatedSearchClause, Vector outGVV,
0608: UserInfo userInfo, GenericDelegator delegator) {
0609:
0610: String relationTitle = relatedSearchClause.getRelationTitle();
0611:
0612: String relationRelEntityName = relatedSearchClause
0613: .getEntityName();
0614:
0615: Map byAndFields = relatedSearchClause.getDecodedAndFields();
0616:
0617: List relatedOrderBy = (List) relatedSearchClause
0618: .getDecodedOrderBy();
0619:
0620: Debug
0621: .logVerbose(
0622: "About to retrieve related entity \""
0623: + relationTitle + relationRelEntityName
0624: + "\".", module);
0625:
0626: List relatedGVL = null;
0627: boolean relationFound = false;
0628: String relationName = relationTitle + relationRelEntityName;
0629:
0630: try {
0631: relatedGVL = delegator.getRelated(relationName,
0632: byAndFields, relatedOrderBy, mainGV);
0633:
0634: Debug.logVerbose("Found relation \"" + relationName
0635: + "\" on entity " + mainGV.getEntityName() + ".",
0636: module);
0637:
0638: relationFound = true;
0639: } catch (GenericModelException gme1) {
0640: // The relation was not found on the primary entity. Try the other secondary ones retrieved so far.
0641: Debug.logVerbose("Did not find relation \"" + relationName
0642: + "\" on entity " + relationRelEntityName
0643: + ". Looking on secondary entities.", module);
0644:
0645: for (int entityNbr = 1; entityNbr < outGVV.size(); entityNbr++) {
0646: GenericValue tempGV = (GenericValue) outGVV
0647: .get(entityNbr);
0648:
0649: try {
0650: relatedGVL = delegator.getRelated(relationName,
0651: byAndFields, relatedOrderBy, tempGV);
0652:
0653: Debug.logVerbose("Found relation \"" + relationName
0654: + "\" on entity " + tempGV.getEntityName()
0655: + ".", module);
0656:
0657: relationFound = true;
0658: break;
0659: } catch (GenericModelException gme2) {
0660: // Continue looking on the next entity.
0661: Debug.logVerbose("Did not find relation \""
0662: + relationName + "\" on entity "
0663: + tempGV.getEntityName() + ".", module);
0664: } catch (GenericEntityException gee2) {
0665: Debug
0666: .logError(
0667: "An error occured while looking for "
0668: + "entities related to secondary entity "
0669: + tempGV.getEntityName()
0670: + " using relation "
0671: + relationName
0672: + ": "
0673: + gee2
0674: .getLocalizedMessage(),
0675: module);
0676: }
0677: }
0678: } catch (GenericEntityException gee1) {
0679: Debug.logError("An error occured while looking for "
0680: + "entities related to main entity "
0681: + mainGV.getEntityName() + " using relation "
0682: + relationName + ": " + gee1.getLocalizedMessage(),
0683: module);
0684: }
0685:
0686: if (!relationFound) {
0687:
0688: //see if there is a relation from the related entity back to one of the entities already retrieved
0689: //this will only be entered if there is not a corresponding relation in the other direction, as the previous
0690: //code would have found that link. This is necessary for entities in the SFA data model that are related to
0691: //entities in other ofbiz data models, since the other ofbiz data models are not aware of SFA
0692: ModelEntity relatedME = delegator
0693: .getModelEntity(relationRelEntityName);
0694:
0695: for (int entityNbr = 0; entityNbr < outGVV.size(); entityNbr++) {
0696: GenericValue tempGV = (GenericValue) outGVV
0697: .get(entityNbr);
0698: ModelRelation relation = relatedME.getRelation(tempGV
0699: .getEntityName());
0700: if (relation != null) {
0701: Map fields = byAndFields == null ? new HashMap()
0702: : new HashMap(byAndFields);
0703: for (int i = 0; i < relation.getKeyMapsSize(); i++) {
0704: ModelKeyMap keyMap = relation.getKeyMap(i);
0705: fields.put(keyMap.getFieldName(), tempGV
0706: .get(keyMap.getRelFieldName()));
0707: }
0708:
0709: try {
0710: relatedGVL = delegator.findByAnd(
0711: relationRelEntityName, fields,
0712: relatedOrderBy);
0713: relationFound = true;
0714: break;
0715: } catch (GenericEntityException gee3) {
0716: Debug
0717: .logError(
0718: "An error occured while looking for "
0719: + "entities related to secondary entity "
0720: + tempGV
0721: .getEntityName()
0722: + " using relation "
0723: + relationName
0724: + ": "
0725: + gee3
0726: .getLocalizedMessage(),
0727: module);
0728: }
0729:
0730: }
0731: }
0732: }
0733:
0734: if (!relationFound) {
0735: Debug
0736: .logError(
0737: "Error retrieving entity \""
0738: + relationRelEntityName
0739: + "\". Relation \""
0740: + relationName
0741: + "\" was not found on any entity in this screen section.",
0742: module);
0743: }
0744:
0745: // Get the first related entity so it can be appended to the related generic value vector.
0746: GenericValue relatedGV = null;
0747: if (relatedGVL != null) {
0748: Iterator relatedGVI = relatedGVL.iterator();
0749:
0750: if (relatedGVI.hasNext()) {
0751: // At least one related entity was found. Use the first one.
0752: relatedGV = (GenericValue) relatedGVI.next();
0753: }
0754: }
0755: if (relatedGV == null) {
0756: // No related entity was found. Create an empty one.
0757: ModelEntity relatedME = delegator
0758: .getModelEntity(relationRelEntityName);
0759: relatedGV = new GenericValue(relatedME);
0760: relatedGV.setDelegator(delegator);
0761: }
0762:
0763: return relatedGV;
0764: }
0765:
0766: //-------------------------------------------------------------------------
0767: // CREATE - Creates a blank set of entities to prepare for an INSERT.
0768: //-------------------------------------------------------------------------
0769: public int processCreate(UserInfo userInfo, List entityNameList,
0770: GenericDelegator delegator, DataMatrix dataMatrix)
0771: throws GenericEntityException {
0772:
0773: int status = STATUS_CONTINUE;
0774:
0775: // Trigger the pre-create event.
0776: status = this .preCreate(userInfo, entityNameList, delegator,
0777: dataMatrix);
0778:
0779: if (status != STATUS_CONTINUE) {
0780: return status;
0781: }
0782:
0783: // Trigger the create event.
0784: status = this .create(userInfo, entityNameList, delegator,
0785: dataMatrix);
0786:
0787: if (status != STATUS_CONTINUE) {
0788: return status;
0789: }
0790:
0791: // Trigger the post-create event.
0792: status = this .postCreate(userInfo, entityNameList, delegator,
0793: dataMatrix);
0794:
0795: return status;
0796: }
0797:
0798: /**
0799: * DOCUMENT ME!
0800: *
0801: * @param userInfo
0802: * @param entityNameList
0803: * @param delegator
0804: * @param dataMatrix
0805: *
0806: * @return
0807: */
0808: protected int preCreate(UserInfo userInfo, List entityNameList,
0809: GenericDelegator delegator, DataMatrix dataMatrix) {
0810:
0811: return STATUS_CONTINUE;
0812: }
0813:
0814: /**
0815: * DOCUMENT ME!
0816: *
0817: * @param userInfo
0818: * @param entityNameList
0819: * @param delegator
0820: * @param dataMatrix
0821: *
0822: * @return
0823: *
0824: * @throws GenericEntityException
0825: */
0826: public int create(UserInfo userInfo, List entityNameList,
0827: GenericDelegator delegator, DataMatrix dataMatrix)
0828: throws GenericEntityException {
0829:
0830: // Create an empty entity so the section will show up.
0831: dataMatrix.getCurrentBuffer().addEmptyRow();
0832:
0833: return STATUS_CONTINUE;
0834: }
0835:
0836: /**
0837: * DOCUMENT ME!
0838: *
0839: * @param userInfo
0840: * @param entityNameList
0841: * @param delegator
0842: * @param dataMatrix
0843: *
0844: * @return
0845: */
0846: protected int postCreate(UserInfo userInfo, List entityNameList,
0847: GenericDelegator delegator, DataMatrix dataMatrix) {
0848:
0849: return STATUS_CONTINUE;
0850: }
0851:
0852: //-------------------------------------------------------------------------
0853: // SHOW_COPY - Preparing to show a copy of an existing record so it can be saved as a new record.
0854: //-------------------------------------------------------------------------
0855: public int processShowCopy(UserInfo userInfo,
0856: GenericDelegator delegator, DataMatrix dataMatrix)
0857: throws GenericEntityException {
0858:
0859: int status = STATUS_CONTINUE;
0860:
0861: // Trigger the pre-ShowCopy event.
0862: status = this .preShowCopy(userInfo, delegator, dataMatrix);
0863:
0864: if (status != STATUS_CONTINUE) {
0865: return status;
0866: }
0867:
0868: // Trigger the showCopy event.
0869: status = this .showCopy(userInfo, delegator, dataMatrix);
0870:
0871: if (status != STATUS_CONTINUE) {
0872: return status;
0873: }
0874:
0875: // Trigger the post-ShowCopy event.
0876: status = this .postShowCopy(userInfo, delegator, dataMatrix);
0877:
0878: return status;
0879: }
0880:
0881: /**
0882: * DOCUMENT ME!
0883: *
0884: * @param userInfo
0885: * @param delegator
0886: * @param dataMatrix
0887: *
0888: * @return
0889: */
0890: protected int preShowCopy(UserInfo userInfo,
0891: GenericDelegator delegator, DataMatrix dataMatrix) {
0892:
0893: return STATUS_CONTINUE;
0894: }
0895:
0896: /**
0897: * DOCUMENT ME!
0898: *
0899: * @param userInfo
0900: * @param delegator
0901: * @param dataMatrix
0902: *
0903: * @return
0904: *
0905: * @throws GenericEntityException
0906: */
0907: public int showCopy(UserInfo userInfo, GenericDelegator delegator,
0908: DataMatrix dataMatrix) throws GenericEntityException {
0909:
0910: return STATUS_CONTINUE;
0911: }
0912:
0913: /**
0914: * DOCUMENT ME!
0915: *
0916: * @param userInfo
0917: * @param delegator
0918: * @param dataMatrix
0919: *
0920: * @return
0921: */
0922: protected int postShowCopy(UserInfo userInfo,
0923: GenericDelegator delegator, DataMatrix dataMatrix) {
0924:
0925: return STATUS_CONTINUE;
0926: }
0927:
0928: //-------------------------------------------------------------------------
0929: // SHOWQUERY - Creates a blank set of entities to prepare for query mode.
0930: //-------------------------------------------------------------------------
0931: public int processShowQuery(UserInfo userInfo, List entityNameList,
0932: GenericDelegator delegator, DataMatrix dataMatrix)
0933: throws GenericEntityException {
0934:
0935: int status = STATUS_CONTINUE;
0936:
0937: // Trigger the pre-showQuery event.
0938: status = this .preShowQuery(userInfo, entityNameList, delegator,
0939: dataMatrix);
0940:
0941: if (status != STATUS_CONTINUE) {
0942: return status;
0943: }
0944:
0945: // Trigger the showQuery event.
0946: status = this .showQuery(userInfo, entityNameList, delegator,
0947: dataMatrix);
0948:
0949: if (status != STATUS_CONTINUE) {
0950: return status;
0951: }
0952:
0953: // Trigger the post-showQuery event.
0954: status = this .postShowQuery(userInfo, entityNameList,
0955: delegator, dataMatrix);
0956:
0957: return status;
0958: }
0959:
0960: /**
0961: * DOCUMENT ME!
0962: *
0963: * @param userInfo
0964: * @param entityNameList
0965: * @param delegator
0966: * @param dataMatrix
0967: *
0968: * @return
0969: */
0970: protected int preShowQuery(UserInfo userInfo, List entityNameList,
0971: GenericDelegator delegator, DataMatrix dataMatrix) {
0972:
0973: return STATUS_CONTINUE;
0974: }
0975:
0976: /**
0977: * DOCUMENT ME!
0978: *
0979: * @param userInfo
0980: * @param entityNameList
0981: * @param delegator
0982: * @param dataMatrix
0983: *
0984: * @return
0985: *
0986: * @throws GenericEntityException
0987: */
0988: public int showQuery(UserInfo userInfo, List entityNameList,
0989: GenericDelegator delegator, DataMatrix dataMatrix)
0990: throws GenericEntityException {
0991:
0992: Vector blankGVV = new Vector();
0993:
0994: Debug.logVerbose("Entity name List: "
0995: + entityNameList.toString(), module);
0996:
0997: Iterator entityNameIterator = entityNameList.iterator();
0998:
0999: while (entityNameIterator.hasNext()) {
1000: String entityName = (String) entityNameIterator.next();
1001: ModelEntity entityME = delegator.getModelEntity(entityName);
1002: GenericValue blankGV = new GenericValue(entityME);
1003: blankGV.setDelegator(delegator);
1004:
1005: Debug.logVerbose(
1006: "Adding a blank generic value for entity \""
1007: + entityName + "\".", module);
1008:
1009: blankGVV.add(blankGV);
1010: }
1011:
1012: dataMatrix.getCurrentBuffer().addEmptyRow();
1013:
1014: // if (DEBUG) Debug.logVerbose("genericValueVectorList at end of GenericEventProcessor.create method: " + genericValueVectorList.toString());
1015: return STATUS_CONTINUE;
1016: }
1017:
1018: /**
1019: * DOCUMENT ME!
1020: *
1021: * @param userInfo
1022: * @param entityNameList
1023: * @param delegator
1024: * @param dataMatrix
1025: *
1026: * @return
1027: */
1028: protected int postShowQuery(UserInfo userInfo, List entityNameList,
1029: GenericDelegator delegator, DataMatrix dataMatrix) {
1030:
1031: return STATUS_CONTINUE;
1032: }
1033:
1034: //-------------------------------------------------------------------------
1035: // SHOWREPORT - Re-displays the query criteria in query mode.
1036: //-------------------------------------------------------------------------
1037: public int processShowReport(UserInfo userInfo,
1038: GenericDelegator delegator, DataMatrix dataMatrix)
1039: throws GenericEntityException {
1040:
1041: int status = STATUS_CONTINUE;
1042:
1043: // Trigger the preShowReport event.
1044: status = this .preShowReport(userInfo, delegator, dataMatrix);
1045:
1046: if (status != STATUS_CONTINUE) {
1047: return status;
1048: }
1049:
1050: // Trigger the showReport event.
1051: status = this .showReport(userInfo, delegator, dataMatrix);
1052:
1053: if (status != STATUS_CONTINUE) {
1054: return status;
1055: }
1056:
1057: // Trigger the postShowReport event.
1058: status = this .postShowReport(userInfo, delegator, dataMatrix);
1059:
1060: return status;
1061: }
1062:
1063: /**
1064: * DOCUMENT ME!
1065: *
1066: * @param userInfo
1067: * @param delegator
1068: * @param dataMatrix
1069: *
1070: * @return
1071: */
1072: protected int preShowReport(UserInfo userInfo,
1073: GenericDelegator delegator, DataMatrix dataMatrix) {
1074:
1075: return STATUS_CONTINUE;
1076: }
1077:
1078: /**
1079: * DOCUMENT ME!
1080: *
1081: * @param userInfo
1082: * @param delegator
1083: * @param dataMatrix
1084: *
1085: * @return
1086: *
1087: * @throws GenericEntityException
1088: */
1089: public int showReport(UserInfo userInfo,
1090: GenericDelegator delegator, DataMatrix dataMatrix)
1091: throws GenericEntityException {
1092:
1093: return STATUS_CONTINUE;
1094: }
1095:
1096: /**
1097: * DOCUMENT ME!
1098: *
1099: * @param userInfo
1100: * @param delegator
1101: * @param dataMatrix
1102: *
1103: * @return
1104: */
1105: protected int postShowReport(UserInfo userInfo,
1106: GenericDelegator delegator, DataMatrix dataMatrix) {
1107:
1108: return STATUS_CONTINUE;
1109: }
1110:
1111: //-------------------------------------------------------------------------
1112: // INSERT - Inserts a new set of entities into the data base.
1113: //-------------------------------------------------------------------------
1114: public int processInsert(UserInfo userInfo,
1115: GenericDelegator delegator, DataMatrix dataMatrix)
1116: throws GenericEntityException {
1117:
1118: if (userInfo == null) {
1119: userInfo = new UserInfo();
1120: }
1121:
1122: int status = STATUS_CONTINUE;
1123:
1124: // If necessary, generate keys.
1125: for (int entityNbr = 0; entityNbr < dataMatrix
1126: .getEntityNameVector().size(); entityNbr++) {
1127: if (dataMatrix.getHasSequenceKey(entityNbr)) {
1128: String entityName = dataMatrix.getEntityName(entityNbr);
1129: String seqId = GenericReplicator.getNextSeqId(
1130: entityName, delegator);
1131: GenericValue genericValue = (GenericValue) (dataMatrix
1132: .getCurrentBuffer().getContentsRow(0)
1133: .get(entityNbr));
1134: List pks = delegator.getModelEntity(entityName)
1135: .getPksCopy();
1136: String pkAttributeName = ((ModelField) pks.get(0))
1137: .getName();
1138: genericValue.set(pkAttributeName, seqId);
1139: }
1140: }
1141:
1142: // Trigger the pre-insert event.
1143: status = this .preInsert(userInfo, delegator, dataMatrix);
1144:
1145: if (status != STATUS_CONTINUE) {
1146: return status;
1147: }
1148:
1149: // Trigger the insert event.
1150: status = this .insert(userInfo, delegator, dataMatrix);
1151:
1152: if (status != STATUS_CONTINUE) {
1153: return status;
1154: }
1155:
1156: // Trigger the post-insert event.
1157: status = this .postInsert(userInfo, delegator, dataMatrix);
1158:
1159: return status;
1160: }
1161:
1162: /**
1163: * DOCUMENT ME!
1164: *
1165: * @param userInfo
1166: * @param delegator
1167: * @param dataMatrix
1168: *
1169: * @return
1170: */
1171: protected int preInsert(UserInfo userInfo,
1172: GenericDelegator delegator, DataMatrix dataMatrix) {
1173:
1174: return STATUS_CONTINUE;
1175: }
1176:
1177: /**
1178: * DOCUMENT ME!
1179: *
1180: * @param userInfo
1181: * @param delegator
1182: * @param dataMatrix
1183: *
1184: * @return
1185: *
1186: * @throws GenericEntityException
1187: */
1188: public int insert(UserInfo userInfo, GenericDelegator delegator,
1189: DataMatrix dataMatrix) throws GenericEntityException {
1190:
1191: for (int row = 0; row < dataMatrix.getCurrentBuffer()
1192: .getRowCount(); row++) {
1193: Debug.logVerbose("row: " + String.valueOf(row), module);
1194:
1195: Debug.logVerbose("Values to insert = "
1196: + dataMatrix.getCurrentBuffer().getContentsRow(row)
1197: .toString(), module);
1198:
1199: Vector currentRow = dataMatrix.getCurrentBuffer()
1200: .getContentsRow(row);
1201:
1202: // Vector originalRow = dataMatrix.getOriginalBuffer().getContentsRow(row);
1203: if (currentRow.size() > 0) {
1204: // Create a List of generic values to be stored.
1205: List storeGVL = new LinkedList();
1206:
1207: // Check for updateable entities.
1208: for (int entityNumber = 0; entityNumber < currentRow
1209: .size(); entityNumber++) {
1210: if (dataMatrix.getIsUpdateable(entityNumber)) {
1211: // This entity is updateable. Append it to the List to be stored.
1212: storeGVL.add((GenericValue) currentRow
1213: .get(entityNumber));
1214: }
1215: }
1216:
1217: Debug.logVerbose("Generic value(s) to insert = "
1218: + storeGVL.toString(), module);
1219:
1220: try {
1221: delegator.storeAll(storeGVL);
1222: } catch (GenericEntityException e2) {
1223: Debug.logWarning("Error inserting: "
1224: + e2.getLocalizedMessage(), "insert");
1225: }
1226:
1227: Debug.logVerbose("Inserted row " + String.valueOf(row),
1228: module);
1229: } else {
1230: return STATUS_CANCEL;
1231: }
1232: }
1233:
1234: return STATUS_CONTINUE;
1235: }
1236:
1237: /**
1238: * DOCUMENT ME!
1239: *
1240: * @param userInfo
1241: * @param delegator
1242: * @param dataMatrix
1243: *
1244: * @return
1245: */
1246: protected int postInsert(UserInfo userInfo,
1247: GenericDelegator delegator, DataMatrix dataMatrix) {
1248:
1249: return STATUS_CONTINUE;
1250: }
1251:
1252: //-------------------------------------------------------------------------
1253: // DELETE - Deletes data for an entity from the data base.
1254: //-------------------------------------------------------------------------
1255: public int processDelete(UserInfo userInfo,
1256: GenericDelegator delegator, ModelEntity primaryModelEntity,
1257: HashMap fields) throws GenericEntityException {
1258:
1259: int status = STATUS_CONTINUE;
1260:
1261: // Get the primary entity record.
1262: GenericValue entityGV = null;
1263:
1264: // try {
1265: entityGV = delegator.findByPrimaryKey(primaryModelEntity
1266: .getEntityName(), fields);
1267:
1268: // } catch (GenericEntityException e) {
1269: // Debug.logWarning("Error retrieving entity to be deleted: " + e.getLocalizedMessage());
1270: // return STATUS_ERROR;
1271: // }
1272: // Trigger the pre-delete event.
1273: status = this .preDelete(userInfo, delegator, "", entityGV);
1274:
1275: if (status != STATUS_CONTINUE) {
1276: return status;
1277: }
1278:
1279: // Trigger the delete event.
1280: status = this .delete(userInfo, delegator, "", entityGV);
1281:
1282: if (status != STATUS_CONTINUE) {
1283: return status;
1284: }
1285:
1286: // Trigger the post-delete event.
1287: status = this .postDelete(userInfo, delegator, "", entityGV);
1288:
1289: return status;
1290: }
1291:
1292: /**
1293: * DOCUMENT ME!
1294: *
1295: * @param userInfo
1296: * @param delegator
1297: * @param originatingEntityName
1298: * @param entityGV
1299: *
1300: * @return
1301: */
1302: protected int preDelete(UserInfo userInfo,
1303: GenericDelegator delegator, String originatingEntityName,
1304: GenericValue entityGV) {
1305:
1306: return STATUS_CONTINUE;
1307: }
1308:
1309: /**
1310: * DOCUMENT ME!
1311: *
1312: * @param userInfo
1313: * @param delegator
1314: * @param originatingEntityName
1315: * @param entityGV
1316: *
1317: * @return
1318: */
1319: public int delete(UserInfo userInfo, GenericDelegator delegator,
1320: String originatingEntityName, GenericValue entityGV) {
1321: GenericPK entityPK = null;
1322: String entityName = null;
1323: String entityKeyString = entityGV.getPrimaryKey().toString();
1324:
1325: Debug.logVerbose("Removing entities dependent on "
1326: + entityKeyString, module);
1327:
1328: int status = deleteAllRelated(userInfo, delegator,
1329: originatingEntityName, entityGV);
1330:
1331: if (status != STATUS_CONTINUE) {
1332: return status;
1333: }
1334:
1335: try {
1336:
1337: entityGV.remove();
1338: } catch (GenericEntityException e) {
1339: Debug.logError("Error removing " + entityGV.getEntityName()
1340: + " " + entityKeyString, module);
1341:
1342: return STATUS_ERROR;
1343: }
1344:
1345: return STATUS_CONTINUE;
1346: }
1347:
1348: /**
1349: * DOCUMENT ME!
1350: *
1351: * @param userInfo
1352: * @param delegator
1353: * @param originatingEntityName
1354: * @param entityGV
1355: *
1356: * @return
1357: */
1358: protected int postDelete(UserInfo userInfo,
1359: GenericDelegator delegator, String originatingEntityName,
1360: GenericValue entityGV) {
1361:
1362: return STATUS_CONTINUE;
1363: }
1364:
1365: /**
1366: * DOCUMENT ME!
1367: *
1368: * @param userInfo
1369: * @param delegator
1370: * @param originatingEntityName
1371: * @param entityGV
1372: *
1373: * @return
1374: */
1375: public int deleteAllRelated(UserInfo userInfo,
1376: GenericDelegator delegator, String originatingEntityName,
1377: GenericValue entityGV) {
1378:
1379: // Override this in the descendant to delete all entities that are dependent on the
1380: // specified entity (the one being deleted in the main delete method). This will call the
1381: // deleteOneRelated method once for each entity class that is dependent on the current entity.
1382: return STATUS_CONTINUE;
1383: }
1384:
1385: /**
1386: * DOCUMENT ME!
1387: *
1388: * @param userInfo
1389: * @param delegator
1390: * @param entityGV
1391: * @param relationTitle
1392: * @param relatedEntityName
1393: * @param originatingEntityName
1394: * @param relatedEventProcessor
1395: *
1396: * @return
1397: */
1398: public int deleteOneRelated(UserInfo userInfo,
1399: GenericDelegator delegator, GenericValue entityGV,
1400: String relationTitle, String relatedEntityName,
1401: String originatingEntityName,
1402: GenericEventProcessor relatedEventProcessor) {
1403: return deleteOneRelated(userInfo, delegator, entityGV,
1404: relationTitle, relatedEntityName,
1405: originatingEntityName, relatedEventProcessor, false);
1406: }
1407:
1408: /**
1409: * DOCUMENT ME!
1410: *
1411: * @param userInfo
1412: * @param delegator
1413: * @param entityGV
1414: * @param relationTitle
1415: * @param relatedEntityName
1416: * @param originatingEntityName
1417: * @param relatedEventProcessor
1418: * @param allowRecursive
1419: *
1420: * @return
1421: */
1422: public int deleteOneRelated(UserInfo userInfo,
1423: GenericDelegator delegator, GenericValue entityGV,
1424: String relationTitle, String relatedEntityName,
1425: String originatingEntityName,
1426: GenericEventProcessor relatedEventProcessor,
1427: boolean allowRecursive) {
1428:
1429: // Check to make sure we are not in an infinite loop caused by two entities trying to delete each other.
1430: if (originatingEntityName.equals(relatedEntityName)
1431: && !allowRecursive) {
1432: // The entity being deleted is the one that originated the delete in the first place.
1433: // It has been deleted anyway. Skip it to avoid infinite loop.
1434: Debug.logVerbose("Skipping delete of related "
1435: + relatedEntityName
1436: + " records to avoid infinite loop.", module);
1437:
1438: return STATUS_CONTINUE;
1439: }
1440:
1441: List relatedGVL = findOneRelated(userInfo, delegator, entityGV,
1442: relationTitle, relatedEntityName);
1443:
1444: if (relatedGVL == null) {
1445: return STATUS_CONTINUE;
1446: }
1447:
1448: Iterator relatedGVI = relatedGVL.iterator();
1449:
1450: while (relatedGVI.hasNext()) {
1451: GenericValue relatedGV = (GenericValue) relatedGVI.next();
1452: int status = relatedEventProcessor.delete(userInfo,
1453: delegator, entityGV.getEntityName(), relatedGV);
1454:
1455: if (status != STATUS_CONTINUE) {
1456: return status;
1457: }
1458: }
1459:
1460: return STATUS_CONTINUE;
1461: }
1462:
1463: /**
1464: * DOCUMENT ME!
1465: *
1466: * @param userInfo
1467: * @param delegator
1468: * @param entityGV
1469: * @param relationTitle
1470: * @param relatedEntityName
1471: *
1472: * @return
1473: */
1474: public List findOneRelated(UserInfo userInfo,
1475: GenericDelegator delegator, GenericValue entityGV,
1476: String relationTitle, String relatedEntityName) {
1477: // Find instances of a particular entity related to the current entity. This will normally be called by
1478: // the deleteAllRelated method of a descendant of this class.
1479:
1480: GenericPK entityPK = null;
1481:
1482: // String entityName = entityGV.getEntityName();
1483: String entityKeyString = entityGV.getPrimaryKey().toString();
1484:
1485: List relatedGVL = null;
1486:
1487: try {
1488: Debug.logVerbose("Retrieving all " + relatedEntityName
1489: + " records related to " + entityKeyString, module);
1490:
1491: relatedGVL = delegator.getRelated(relationTitle
1492: + relatedEntityName, entityGV);
1493: } catch (GenericEntityException e) {
1494: Debug.logError("Error getting the related "
1495: + relatedEntityName + " records: "
1496: + e.getLocalizedMessage(), module);
1497:
1498: return relatedGVL;
1499: }
1500:
1501: return relatedGVL;
1502: }
1503:
1504: //-------------------------------------------------------------------------
1505: // UPDATE - Updates a set of entities in the data base.
1506: //-------------------------------------------------------------------------
1507: public int processUpdate(UserInfo userInfo,
1508: GenericDelegator delegator, DataMatrix dataMatrix)
1509: throws GenericEntityException {
1510:
1511: UtilTimer timer = new UtilTimer();
1512:
1513: if (TIMER) {
1514: timer.timerString(2,
1515: "[GenericEventProcessor.processUpdate] Start");
1516: }
1517:
1518: int status = STATUS_CONTINUE;
1519:
1520: // Trigger the pre-update event.
1521: status = this .preUpdate(userInfo, delegator, dataMatrix);
1522:
1523: if (status != STATUS_CONTINUE) {
1524: return status;
1525: }
1526:
1527: if (TIMER) {
1528: timer
1529: .timerString(2,
1530: "[GenericEventProcessor.processUpdate] Preupdate finished");
1531: }
1532:
1533: // Trigger the update event.
1534: status = this .update(userInfo, delegator, dataMatrix);
1535:
1536: if (status != STATUS_CONTINUE) {
1537: return status;
1538: }
1539:
1540: if (TIMER) {
1541: timer
1542: .timerString(2,
1543: "[GenericEventProcessor.processUpdate] Update finished");
1544: }
1545:
1546: // Trigger the post-update event.
1547: status = this .postUpdate(userInfo, delegator, dataMatrix);
1548:
1549: if (TIMER) {
1550: timer
1551: .timerString(2,
1552: "[GenericEventProcessor.processUpdate] Postupdate finished");
1553: }
1554:
1555: if (TIMER) {
1556: timer.timerString(2,
1557: "[GenericEventProcessor.processUpdate] End");
1558: }
1559:
1560: return status;
1561: }
1562:
1563: /**
1564: * DOCUMENT ME!
1565: *
1566: * @param userInfo
1567: * @param delegator
1568: * @param dataMatrix
1569: *
1570: * @return
1571: */
1572: protected int preUpdate(UserInfo userInfo,
1573: GenericDelegator delegator, DataMatrix dataMatrix) {
1574:
1575: return STATUS_CONTINUE;
1576: }
1577:
1578: /**
1579: * DOCUMENT ME!
1580: *
1581: * @param userInfo
1582: * @param delegator
1583: * @param dataMatrix
1584: *
1585: * @return
1586: *
1587: * @throws GenericEntityException
1588: */
1589: public int update(UserInfo userInfo, GenericDelegator delegator,
1590: DataMatrix dataMatrix) throws GenericEntityException {
1591:
1592: UtilTimer timer = new UtilTimer();
1593:
1594: if (TIMER) {
1595: timer
1596: .timerString(2,
1597: "[GenericEventProcessor.update] Start");
1598: }
1599:
1600: for (int row = 0; row < dataMatrix.getOriginalBuffer()
1601: .getRowCount(); row++) {
1602: if (TIMER) {
1603: timer.timerString(2,
1604: "[GenericEventProcessor.update] Saving row");
1605: }
1606:
1607: boolean rowChanged = dataMatrix.getRowChanged(row);
1608:
1609: if (rowChanged) {
1610: Vector currentRow = dataMatrix.getCurrentBuffer()
1611: .getContentsRow(row);
1612: Vector originalRow = dataMatrix.getOriginalBuffer()
1613: .getContentsRow(row);
1614:
1615: if (currentRow.size() > 0) {
1616: // Create a List of generic values to be stored.
1617: List storeGVL = new LinkedList();
1618:
1619: // Check for updateable entities.
1620: for (int entityNumber = 0; entityNumber < currentRow
1621: .size(); entityNumber++) {
1622: if (dataMatrix.getIsUpdateable(entityNumber)) {
1623: // This entity is updateable. Append it to the List to be stored.
1624: storeGVL.add((GenericValue) currentRow
1625: .get(entityNumber));
1626: }
1627: }
1628:
1629: Debug.logVerbose("About to update row "
1630: + String.valueOf(row), module);
1631:
1632: Debug.logVerbose("Generic value(s) to update: "
1633: + storeGVL.toString(), module);
1634:
1635: try {
1636: delegator.storeAll(storeGVL);
1637: } catch (GenericEntityException e2) {
1638: Debug.logError("Error updating: "
1639: + e2.getLocalizedMessage(), module);
1640: }
1641:
1642: } else {
1643: return STATUS_CANCEL;
1644: }
1645: }
1646: }
1647:
1648: return STATUS_CONTINUE;
1649: }
1650:
1651: /**
1652: * DOCUMENT ME!
1653: *
1654: * @param userInfo
1655: * @param delegator
1656: * @param dataMatrix
1657: *
1658: * @return
1659: */
1660: protected int postUpdate(UserInfo userInfo,
1661: GenericDelegator delegator, DataMatrix dataMatrix) {
1662:
1663: return STATUS_CONTINUE;
1664: }
1665:
1666: //-------------------------------------------------------------------------
1667: // UPDATE_SELECT - Adds and/or deletes records in a many-to-many relationship table.
1668: //-------------------------------------------------------------------------
1669: public int processUpdateSelect(UserInfo userInfo,
1670: GenericDelegator delegator, ModelEntity primaryModelEntity,
1671: ArrayList removeKeyMapList, ArrayList createKeyMapList)
1672: throws GenericEntityException {
1673:
1674: // Trigger the pre-update-select event.
1675: int status = this .preUpdateSelect(userInfo, delegator,
1676: primaryModelEntity, removeKeyMapList, createKeyMapList);
1677:
1678: if (status != STATUS_CONTINUE) {
1679: return status;
1680: }
1681:
1682: // Trigger the update-select event.
1683: status = this .updateSelect(userInfo, delegator,
1684: primaryModelEntity, removeKeyMapList, createKeyMapList);
1685:
1686: if (status != STATUS_CONTINUE) {
1687: return status;
1688: }
1689:
1690: // Trigger the post-update-select event.
1691: status = this .postUpdateSelect(userInfo, delegator,
1692: primaryModelEntity, removeKeyMapList, createKeyMapList);
1693:
1694: return status;
1695: }
1696:
1697: /**
1698: * DOCUMENT ME!
1699: *
1700: * @param userInfo
1701: * @param delegator
1702: * @param primaryModelEntity
1703: * @param removeKeyMapList
1704: * @param createKeyMapList
1705: *
1706: * @return
1707: */
1708: protected int preUpdateSelect(UserInfo userInfo,
1709: GenericDelegator delegator, ModelEntity primaryModelEntity,
1710: ArrayList removeKeyMapList, ArrayList createKeyMapList) {
1711:
1712: return STATUS_CONTINUE;
1713: }
1714:
1715: /**
1716: * DOCUMENT ME!
1717: *
1718: * @param userInfo
1719: * @param delegator
1720: * @param primaryModelEntity
1721: * @param removeKeyMapList
1722: * @param createKeyMapList
1723: *
1724: * @return
1725: *
1726: * @throws GenericEntityException
1727: */
1728: public int updateSelect(UserInfo userInfo,
1729: GenericDelegator delegator, ModelEntity primaryModelEntity,
1730: ArrayList removeKeyMapList, ArrayList createKeyMapList)
1731: throws GenericEntityException {
1732:
1733: Debug.logVerbose("removeKeyMapList: " + removeKeyMapList,
1734: module);
1735: Debug.logVerbose("createKeyMapList: " + createKeyMapList,
1736: module);
1737:
1738: // Remove de-selected items from the data base.
1739: Iterator removeKeyMapListI = removeKeyMapList.iterator();
1740:
1741: while (removeKeyMapListI.hasNext()) {
1742: // User un-selected this item. Need to remove it from the many-to-many table.
1743: HashMap removeMap = (HashMap) removeKeyMapListI.next();
1744: deleteOneSelect(primaryModelEntity, removeMap, userInfo,
1745: delegator);
1746: }
1747:
1748: // Add newly selected items to the data base.
1749: Iterator createKeyMapListI = createKeyMapList.iterator();
1750:
1751: while (createKeyMapListI.hasNext()) {
1752: // User selected this item. Need to add it to the many-to-many table.
1753: HashMap createMap = (HashMap) createKeyMapListI.next();
1754: insertOneSelect(primaryModelEntity, createMap, userInfo,
1755: delegator);
1756: }
1757:
1758: return STATUS_CONTINUE;
1759: }
1760:
1761: /**
1762: * DOCUMENT ME!
1763: *
1764: * @param userInfo
1765: * @param delegator
1766: * @param primaryModelEntity
1767: * @param removeKeyMapList
1768: * @param createKeyMapList
1769: *
1770: * @return
1771: */
1772: protected int postUpdateSelect(UserInfo userInfo,
1773: GenericDelegator delegator, ModelEntity primaryModelEntity,
1774: ArrayList removeKeyMapList, ArrayList createKeyMapList) {
1775:
1776: return STATUS_CONTINUE;
1777: }
1778:
1779: /**
1780: * DOCUMENT ME!
1781: *
1782: * @param primaryModelEntity
1783: * @param removeMap
1784: * @param userInfo
1785: * @param delegator
1786: *
1787: * @return
1788: */
1789: protected int deleteOneSelect(ModelEntity primaryModelEntity,
1790: HashMap removeMap, UserInfo userInfo,
1791: GenericDelegator delegator) {
1792:
1793: try {
1794: GenericPK removePK = new GenericPK(primaryModelEntity,
1795: removeMap);
1796:
1797: try {
1798: delegator.removeByPrimaryKey(removePK);
1799: } catch (Exception e2) {
1800: Debug.logError(
1801: "Error removing entity using primary key map "
1802: + removeMap + ": "
1803: + e2.getLocalizedMessage(), module);
1804:
1805: return STATUS_ERROR;
1806: }
1807: } catch (Exception e1) {
1808: Debug.logError("Cannot create a primary key for entity "
1809: + primaryModelEntity.getEntityName() + " from map "
1810: + removeMap + ": " + e1.getLocalizedMessage(),
1811: module);
1812:
1813: return STATUS_ERROR;
1814: }
1815:
1816: return STATUS_CONTINUE;
1817: }
1818:
1819: /**
1820: * DOCUMENT ME!
1821: *
1822: * @param primaryModelEntity
1823: * @param createMap
1824: * @param userInfo
1825: * @param delegator
1826: *
1827: * @return
1828: */
1829: protected int insertOneSelect(ModelEntity primaryModelEntity,
1830: HashMap createMap, UserInfo userInfo,
1831: GenericDelegator delegator) {
1832:
1833: try {
1834: Debug.logVerbose("Creating generic value from "
1835: + "create map " + createMap.toString(), module);
1836:
1837: GenericValue createGV = new GenericValue(
1838: primaryModelEntity, createMap);
1839: createGV.setDelegator(delegator);
1840:
1841: try {
1842: Debug.logVerbose("Inserting record "
1843: + "using generic value " + createGV.toString(),
1844: module);
1845:
1846: delegator.create(createGV);
1847: } catch (Exception e2) {
1848: Debug.logError("Error inserting a new "
1849: + primaryModelEntity.getEntityName()
1850: + " from generic value " + createGV, module);
1851: Debug.logError(e2.getLocalizedMessage(), module);
1852:
1853: return STATUS_ERROR;
1854: }
1855: } catch (Exception e1) {
1856: Debug.logError("Cannot create a generic value for entity "
1857: + primaryModelEntity.getEntityName() + " from map "
1858: + createMap, module);
1859: Debug.logError(e1.getLocalizedMessage(), module);
1860:
1861: return STATUS_ERROR;
1862: }
1863:
1864: return STATUS_CONTINUE;
1865: }
1866:
1867: //-------------------------------------------------------------------------
1868: // utility function to handle linkage between EntityAccess and the entity that is being retrieved/stored
1869: // the default is null, meaning that the entity is unrestricted
1870: // this should be overridden for any entity that needs controlled access
1871: //-------------------------------------------------------------------------
1872: public SecurityLinkInfo getSecurityLinkInfo(UserInfo userInfo,
1873: GenericDelegator delegator) {
1874:
1875: return null;
1876: }
1877:
1878: //-------------------------------------------------------------------------
1879: // CUSTOM - Performs one or more custom events.
1880: //-------------------------------------------------------------------------
1881: public int processCustom(UserInfo userInfo,
1882: GenericDelegator delegator, DataMatrix dataMatrix,
1883: String action) throws GenericEntityException {
1884:
1885: return STATUS_CONTINUE;
1886: }
1887:
1888: /**
1889: * Add entity clauses for one related entity. This will join related tables to the query
1890: * during a retrieve so query values can be entered that are in related entities.
1891: *
1892: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
1893: *
1894: * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
1895: * @param relationTitle Relation title
1896: * @param relatedEntityName Name of related entity
1897: * @param primaryEntityName Name of the primary entity
1898: * @param primaryME ModelEntity object for the primary entity
1899: * @param queryInfo criteria to be used in search
1900: */
1901: public void addOneRelationClause(GenericDelegator delegator,
1902: String relationTitle, String relatedAndFields,
1903: String relatedEntityName, String primaryEntityName,
1904: ModelEntity primaryME, boolean isOuterJoin,
1905: QueryInfo queryInfo) throws GenericEntityException {
1906: EventUtility.addOneRelationClause(delegator, relationTitle,
1907: relatedAndFields, relatedEntityName, primaryEntityName,
1908: primaryME, isOuterJoin, queryInfo);
1909: }
1910:
1911: }
|