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.ui;
0018:
0019: import java.util.ArrayList;
0020: import java.util.HashMap;
0021: import java.util.Iterator;
0022: import java.util.List;
0023: import java.util.Map;
0024: import java.util.StringTokenizer;
0025: import java.util.Vector;
0026:
0027: import org.ofbiz.base.util.Debug;
0028: import org.ofbiz.base.util.UtilMisc;
0029: import org.ofbiz.entity.GenericDelegator;
0030: import org.ofbiz.entity.GenericEntityException;
0031: import org.ofbiz.entity.GenericPK;
0032: import org.ofbiz.entity.GenericValue;
0033: import org.ofbiz.entity.condition.EntityComparisonOperator;
0034: import org.ofbiz.entity.condition.EntityCondition;
0035: import org.ofbiz.entity.condition.EntityConditionList;
0036: import org.ofbiz.entity.condition.EntityExpr;
0037: import org.ofbiz.entity.condition.EntityOperator;
0038: import org.ofbiz.entity.model.DynamicViewEntity;
0039: import org.ofbiz.entity.model.ModelEntity;
0040: import org.ofbiz.entity.model.ModelKeyMap;
0041:
0042: import com.sourcetap.sfa.event.GenericEventProcessor;
0043: import com.sourcetap.sfa.util.EntityHelper;
0044: import com.sourcetap.sfa.util.QueryInfo;
0045:
0046: /**
0047: * DOCUMENT ME!
0048: *
0049: */
0050: public class UIUtility {
0051: public static final String module = UIUtility.class.getName();
0052:
0053: /**
0054: * DOCUMENT ME!
0055: *
0056: * @param entityDisplayDef
0057: * @param genericValueVector
0058: * @param currentAttibuteName
0059: *
0060: * @return
0061: */
0062: public static String decodeEntityDisplayDef(
0063: String entityDisplayDef, Vector genericValueVector,
0064: String currentAttibuteName) {
0065: if (entityDisplayDef == null) {
0066: Debug.logError("entityDisplayDef is null", module);
0067:
0068: return "ERROR";
0069: }
0070:
0071: if (genericValueVector.size() == 0) {
0072: Debug.logError("genericValueVector is empty.", module);
0073:
0074: return "ERROR";
0075: }
0076:
0077: if (currentAttibuteName == null) {
0078: Debug.logError("currentAttibuteName is null", module);
0079:
0080: return "ERROR";
0081: }
0082:
0083: Debug.logVerbose("[decodeEntityDisplayDef] entityDisplayDef: "
0084: + entityDisplayDef, module);
0085:
0086: Debug.logVerbose(
0087: "[decodeEntityDisplayDef] genericValueVector: "
0088: + genericValueVector.toString(), module);
0089:
0090: Debug.logVerbose(
0091: "[decodeEntityDisplayDef] currentAttibuteName: "
0092: + currentAttibuteName, module);
0093:
0094: // Dissect the display definition.
0095: // Example:
0096: // * attribEntityDisplayDef = 'sicCodeId;" - ";SicCode.sicCodeDescription'
0097: // * Resulting Display Value = '2074 - Cottonseed Oil Mills'
0098: String displayItem = "";
0099: StringTokenizer tokSemicolon = new StringTokenizer(
0100: entityDisplayDef, ";");
0101: GenericValue genericValue = null;
0102: String attributeName = "";
0103:
0104: while (tokSemicolon.hasMoreTokens()) {
0105: String valueDef = tokSemicolon.nextToken();
0106:
0107: Debug.logVerbose("valueDef = " + valueDef, module);
0108:
0109: if (valueDef.indexOf("\"") != -1) {
0110: // This is a literal string. Get the part between the 2 quotes and append it
0111: // directly onto the display value.
0112: int startPos = valueDef.indexOf("\"");
0113: int endPos = valueDef.lastIndexOf("\"");
0114:
0115: if ((startPos < 0)
0116: || (startPos >= (valueDef.length() - 1))) {
0117: startPos = 0; // Missing first quote
0118: }
0119:
0120: if (endPos < (valueDef.length() - 1)) {
0121: endPos = valueDef.length() - 1; // Missing last quote
0122: }
0123:
0124: Debug.logVerbose("startPos = "
0125: + String.valueOf(startPos), module);
0126:
0127: Debug.logVerbose("endPos = " + String.valueOf(endPos),
0128: module);
0129:
0130: displayItem += valueDef.substring(startPos + 1, endPos);
0131: } else {
0132: if (valueDef.indexOf(".") != -1) {
0133:
0134: Debug.logVerbose("Found a period.", module);
0135:
0136: StringTokenizer tokPeriod = new StringTokenizer(
0137: valueDef, ".");
0138: String entityName = tokPeriod.nextToken();
0139: attributeName = tokPeriod.nextToken();
0140:
0141: Debug.logVerbose("entityName = " + entityName,
0142: module);
0143:
0144: Debug.logVerbose(
0145: "attributeName = " + attributeName, module);
0146:
0147: Iterator genericValueVectorI = genericValueVector
0148: .iterator();
0149:
0150: while (genericValueVectorI.hasNext()) {
0151: GenericValue testGV = (GenericValue) genericValueVectorI
0152: .next();
0153:
0154: if (testGV.getEntityName().equals(entityName)) {
0155:
0156: Debug.logVerbose(
0157: "Found generic value with entity name "
0158: + entityName, module);
0159:
0160: genericValue = testGV;
0161: }
0162: }
0163: } else {
0164: Debug.logVerbose("Did not find a period.", module);
0165:
0166: genericValue = (GenericValue) genericValueVector
0167: .get(0);
0168: attributeName = valueDef;
0169: }
0170:
0171: // This is not a literal. Treat it as an attribute name.
0172: if (attributeName.equals("#currentField")) {
0173: attributeName = (currentAttibuteName == null) ? ""
0174: : currentAttibuteName;
0175: }
0176:
0177: try {
0178: displayItem += ((genericValue.get(attributeName) != null) ? String
0179: .valueOf(genericValue.get(attributeName))
0180: : "");
0181: } catch (Exception e) {
0182: // throw new GenericEntityException("");
0183: Debug.logError(e.getMessage(), module);
0184: Debug
0185: .logError(
0186: "Entity display definition \""
0187: + entityDisplayDef
0188: + "\" not valid", module);
0189:
0190: // for genericValue " + genericValue.toString() +
0191: // ". Error message: " + e.toString());
0192: return "ERROR";
0193: }
0194: }
0195: }
0196:
0197: return displayItem;
0198: }
0199:
0200: /**
0201: * DOCUMENT ME!
0202: *
0203: * @param entityFindDef
0204: * @param entityDetailsVector
0205: * @param currentAttibuteName
0206: *
0207: * @return
0208: */
0209: public static HashMap decodeEntityFindDef(String entityFindDef,
0210: Vector entityDetailsVector, String currentAttributeName) {
0211: // Split the entity find definition into pieces. Example:
0212: // * attribEntityFindDef:
0213: // * sectionId:sectionId;partyId:"-1"
0214: // * Resulting find map pairs:
0215: // * sectionId:43
0216: // * partyId:-1
0217: // * Resulting find logic:
0218: // * sectionId = 43 AND partyId = -1
0219: String findAttributeValue = "";
0220: HashMap entityFindMap = new HashMap();
0221: StringTokenizer tokSemicolon = new StringTokenizer(
0222: entityFindDef, ";");
0223: String entityName = "";
0224:
0225: Debug
0226: .logVerbose(
0227: "-->[UIUtility.decodeEntityFindDef] Inside decodeEntityFindDef method. Find def is '"
0228: + entityFindDef + "'.", module);
0229:
0230: while (tokSemicolon.hasMoreTokens()) {
0231: Debug
0232: .logVerbose(
0233: "-->[UIUtility.decodeEntityFindDef] Found a semicolon",
0234: module);
0235:
0236: String pair = tokSemicolon.nextToken();
0237: StringTokenizer tokColon = new StringTokenizer(pair, ":");
0238:
0239: if (tokColon.countTokens() != 2) {
0240: Debug.logWarning(
0241: "-->[UIUtility.decodeEntityFindDef] No colon found in '"
0242: + pair + "'", module);
0243:
0244: Debug.logWarning(
0245: "[UIUtility.decodeEntityFindDef]: Problem with entity find definition \""
0246: + entityFindDef + "\":", module);
0247: Debug
0248: .logWarning(
0249: "There must be 2 items in each pair separated by colons.",
0250: module);
0251:
0252: return entityFindMap;
0253: } else {
0254: Debug.logVerbose(
0255: "-->[UIUtility.decodeEntityFindDef] Colon found in '"
0256: + pair + "'", module);
0257:
0258: String findAttributeName = tokColon.nextToken();
0259: if (findAttributeName.equals("#currentField"))
0260: findAttributeName = currentAttributeName;
0261:
0262: // String screenAttributeString = tokColon.nextToken();
0263: String attributeValueSource = tokColon.nextToken();
0264:
0265: try {
0266: findAttributeValue = decodeAttributeValue(
0267: attributeValueSource, entityDetailsVector,
0268: currentAttributeName);
0269: } catch (GenericEntityException e) {
0270: Debug.logError(
0271: "[UIUtility.decodeFieldValue]: Problem with entity find definition \""
0272: + entityFindDef + "\": "
0273: + e.getLocalizedMessage(), module);
0274: entityFindMap = null;
0275:
0276: return entityFindMap;
0277: }
0278:
0279: //jmn start
0280:
0281: /*
0282: GenericValue screenEntityDetails = null;
0283: if(screenAttributeString.indexOf("\"") == -1) {
0284: // There are no quote marks in the attribute name, so it is really an attribute name. Get the value of this
0285: // attribute from the screen entity.
0286:
0287: // If there is a "." in the attribute name, split off the entity name.
0288: StringTokenizer tokPeriod = new StringTokenizer(screenAttributeString, ".");
0289: if(tokPeriod.countTokens() == 1) {
0290: // There is no "." in the attribute string. Assume we need to use the primary screen entity.
0291: screenEntityName = ((GenericValue)(entityDetailsVector.get(0))).getEntityName();
0292: screenAttributeName = screenAttributeString;
0293: } else {
0294: // Need to get the entity specified before the "." in the attribute name string.
0295: screenEntityName = tokPeriod.nextToken();
0296: screenAttributeName = tokPeriod.nextToken();
0297: }
0298: tokPeriod = null;
0299: if (screenAttributeName.equals("#currentField")) {
0300: screenAttributeName = currentAttibuteName==null ? "" : currentAttibuteName;
0301: }
0302:
0303: // Get the specified screen entity from the vector.
0304: Iterator entityDetailsIterator = entityDetailsVector.iterator();
0305: while (entityDetailsIterator.hasNext()) {
0306: GenericValue testEntity = (GenericValue)entityDetailsIterator.next();
0307: if (testEntity.getEntityName().equals(screenEntityName)) {
0308: screenEntityDetails = testEntity;
0309: }
0310: }
0311: try {
0312: String dummy = String.valueOf(screenEntityDetails.get(screenAttributeName));
0313: }
0314: catch (IllegalArgumentException e) {
0315: Debug.logWarning("[UIUtility.decodeEntityFindDef]: Problem with entity find definition \"" + entityFindDef + "\":");
0316: Debug.logWarning(e.getMessage());
0317: entityFindMap = null;
0318: return entityFindMap;
0319: }
0320: findAttributeValue = screenEntityDetails.getString(screenAttributeName);
0321: } else {
0322: // This is a literal value because it has quote marks around it.
0323: findAttributeValue = screenAttributeString.substring(screenAttributeString.indexOf("\"") + 1, screenAttributeString.lastIndexOf("\""));
0324: }
0325: */
0326:
0327: //jmn end
0328: // Put the attribute name and the screen value to which we are comparing into the find map.
0329: Debug.logVerbose(
0330: "-->[UIUtility.decodeEntityFindDef] Appending onto entityFindMap: "
0331: + findAttributeName + "/"
0332: + findAttributeValue, module);
0333:
0334: entityFindMap
0335: .put(findAttributeName, findAttributeValue);
0336: }
0337:
0338: tokColon = null;
0339: }
0340:
0341: tokSemicolon = null;
0342:
0343: return entityFindMap;
0344: }
0345:
0346: /**
0347: * Instantiates UIDropDown or one of its descendant classes.
0348: *
0349: * @param className The class to be instantiated
0350: *
0351: * @return Object of class specified by className parameter
0352: *
0353: * @see com.sourcetap.sfa.ui.UIDropDown
0354: */
0355: public static UIDropDown getUIDropDown(String className) {
0356: Class uiDropDownClass = null;
0357: UIDropDown uiDropdown = null;
0358:
0359: if ((className.length() > 0) && !className.equals("null")) {
0360: try {
0361: uiDropDownClass = Class.forName(className);
0362: } catch (ClassNotFoundException e) {
0363: Debug
0364: .logError(
0365: "[UIUtility.getUIDropDown] Class \""
0366: + className
0367: + "\" specified in display object could not be found.",
0368: module);
0369: Debug.logError(e, module);
0370:
0371: return null;
0372: }
0373:
0374: try {
0375: uiDropdown = (UIDropDown) uiDropDownClass.newInstance();
0376: } catch (IllegalAccessException e) {
0377: Debug
0378: .logError(
0379: "[UIUtility.getUIDropDown] Drop Down class \""
0380: + className
0381: + "\" could not be instantiated because "
0382: + "the class or initializer is not accessible.",
0383: module);
0384: Debug.logError(e, module);
0385:
0386: return null;
0387: } catch (InstantiationException e) {
0388: Debug
0389: .logError(
0390: "[UIUtility.getUIDropDown] Drop Down class \""
0391: + className
0392: + "\" cannot be instantiated because it is an "
0393: + "abstract class, an interface, an array class, a primitive type, or void.",
0394: module);
0395: Debug.logError(e, module);
0396:
0397: return null;
0398: }
0399: } else {
0400: // Class name was not specified in the display object. Use the default class.
0401: uiDropdown = new UIDropDown();
0402: }
0403:
0404: // Return the new instance of UIDropDown.
0405: return uiDropdown;
0406: }
0407:
0408: /**
0409: * Instantiates GenericEventProcessor or one of its descendant classes.
0410: *
0411: * @param className The class to be instantiated
0412: *
0413: * @return Object of class specified by className parameter
0414: *
0415: * @see com.sourcetap.sfa.ui.UIDropDown
0416: */
0417: public static GenericEventProcessor getEventProcessor(
0418: String className) {
0419: Class eventProcessorClass = null;
0420: GenericEventProcessor eventProcessor = null;
0421:
0422: if ((className != null) && (className.length() > 0)
0423: && !className.equals("null")) {
0424: try {
0425: eventProcessorClass = Class.forName(className);
0426: } catch (ClassNotFoundException e) {
0427: Debug
0428: .logError(
0429: "[UIUtility.getEventProcessor] Class \""
0430: + className
0431: + "\" specified in section.eventProcessorClass could not be found.",
0432: module);
0433: Debug.logError(e, module);
0434:
0435: return null;
0436: }
0437:
0438: try {
0439: eventProcessor = (GenericEventProcessor) eventProcessorClass
0440: .newInstance();
0441: } catch (IllegalAccessException e) {
0442: Debug
0443: .logError(
0444: "[UIUtility.getEventProcessor] EventProcessor class \""
0445: + className
0446: + "\" could not be instantiated because "
0447: + "the class or initializer is not accessible.",
0448: module);
0449: Debug.logError(e, module);
0450:
0451: return null;
0452: } catch (InstantiationException e) {
0453: Debug
0454: .logError(
0455: "[UIUtility.getEventProcessor] EventProcessor class \""
0456: + className
0457: + "\" cannot be instantiated because it is an "
0458: + "abstract class, an interface, an array class, a primitive type, or void.",
0459: module);
0460: Debug.logError(e, module);
0461:
0462: return null;
0463: }
0464: } else {
0465: // Class name was not specified in the display object. Use the default class.
0466: eventProcessor = new GenericEventProcessor();
0467: }
0468:
0469: // Return the new instance of UIDropDown.
0470: return eventProcessor;
0471: }
0472:
0473: /**
0474: * Instantiates UISearchField or one of its descendant classes.
0475: *
0476: * @param className The class to be instantiated
0477: *
0478: * @return Object of class specified by className parameter
0479: *
0480: * @see com.sourcetap.sfa.ui.UISearchField
0481: */
0482: public static UISearchField getUISearchField(String className) {
0483: Class UISearchFieldClass = null;
0484: UISearchField uiSearchField = null;
0485:
0486: if ((className.length() > 0) && !className.equals("null")) {
0487: // Class name was specified in the display object. Need to instantiate that class.
0488: try {
0489: UISearchFieldClass = Class.forName(className);
0490: } catch (ClassNotFoundException e) {
0491: Debug
0492: .logError(
0493: "[UIUtility.getUISearchField] Class \""
0494: + className
0495: + "\" specified in display object could not be found.",
0496: module);
0497: Debug.logError(e, module);
0498:
0499: return null;
0500: }
0501:
0502: try {
0503: uiSearchField = (UISearchField) UISearchFieldClass
0504: .newInstance();
0505: } catch (IllegalAccessException e) {
0506: Debug
0507: .logError(
0508: "[UIUtility.getUISearchField] Search field class \""
0509: + className
0510: + "\" could not be instantiated because "
0511: + "the class or initializer is not accessible.",
0512: module);
0513: Debug.logError(e, module);
0514:
0515: return null;
0516: } catch (InstantiationException e) {
0517: Debug
0518: .logError(
0519: "[UIUtility.getUISearchField] Search field class \""
0520: + className
0521: + "\" cannot be instantiated because it is an "
0522: + "abstract class, an interface, an array class, a primitive type, or void.",
0523: module);
0524: Debug.logError(e, module);
0525:
0526: return null;
0527: }
0528: } else {
0529: // Class name was not specified in the display object. Use the default class.
0530: uiSearchField = new UISearchField();
0531: }
0532:
0533: // Return the new instance of UISearchField.
0534: return uiSearchField;
0535: }
0536:
0537: /**
0538: * DOCUMENT ME!
0539: *
0540: * @param delegator
0541: * @param searchAttribName
0542: * @param searchEntityName
0543: *
0544: * @return
0545: *
0546: * @throws GenericEntityException
0547: */
0548: public static String getAttributeId(GenericDelegator delegator,
0549: String searchAttribName, String searchEntityName)
0550: throws GenericEntityException {
0551: // Figure out the attribute ID for the UiAttribute this parameter corresponds to so we can
0552: // save the query.
0553: // select * from UiAttribute a, UIEntity e where e.entity_id = a.entity_id and a.attribute_name = <searchAttrName> and e.entityName = <searchEntityName>
0554:
0555: DynamicViewEntity dve = EntityHelper.createDynamicViewEntity(
0556: delegator, "UiAttribute");
0557: dve.addMemberEntity("UiEntity", "UiEntity");
0558: dve
0559: .addViewLink("UiAttribute", "UiEntity", Boolean.FALSE,
0560: UtilMisc.toList(new ModelKeyMap("entityId",
0561: "entityId")));
0562: dve.addAlias("UiEntity", "entityName", null, null, null, null,
0563: null);
0564:
0565: EntityCondition condition = new EntityConditionList(
0566: UtilMisc
0567: .toList(
0568: new EntityExpr("attributeName",
0569: EntityOperator.EQUALS,
0570: searchAttribName),
0571: new EntityExpr("entityName",
0572: EntityOperator.EQUALS,
0573: searchEntityName)),
0574: EntityOperator.AND);
0575:
0576: List queryGVL = EntityHelper.findByCondition(delegator, dve,
0577: condition, null);
0578:
0579: if (queryGVL.size() == 0) {
0580: throw new GenericEntityException(
0581: "No UI Attribute found for entity name \""
0582: + searchEntityName
0583: + "\" and attribute name \""
0584: + searchAttribName + "\"");
0585: }
0586:
0587: GenericValue uiAttributeGV = (GenericValue) queryGVL.iterator()
0588: .next();
0589: String attributeId = uiAttributeGV.getString("attributeId");
0590:
0591: return attributeId;
0592: }
0593:
0594: /**
0595: * DOCUMENT ME!
0596: *
0597: * @param entityDetailsVector
0598: * @param entityName
0599: *
0600: * @return
0601: */
0602: public static GenericValue getEntityValue(
0603: Vector entityDetailsVector, String entityName) {
0604: // Get the specified entity from the vector.
0605: Iterator entityDetailsIterator = entityDetailsVector.iterator();
0606:
0607: while (entityDetailsIterator.hasNext()) {
0608: GenericValue testEntity = (GenericValue) entityDetailsIterator
0609: .next();
0610:
0611: if (testEntity.getEntityName().equals(entityName)) {
0612: return testEntity;
0613: }
0614: }
0615:
0616: return null;
0617: }
0618:
0619: /**
0620: * DOCUMENT ME!
0621: *
0622: * @param entityDetailsVector
0623: * @param entityName
0624: * @param attributeName
0625: *
0626: * @return
0627: */
0628: public static String getAttributeValue(Vector entityDetailsVector,
0629: String entityName, String attributeName) {
0630: GenericValue entityGV = UIUtility.getEntityValue(
0631: entityDetailsVector, entityName);
0632:
0633: if (entityGV == null) {
0634: return null;
0635: }
0636:
0637: try {
0638: String attribValue = String.valueOf(entityGV
0639: .get(attributeName));
0640:
0641: return attribValue;
0642: } catch (IllegalArgumentException e) {
0643: Debug.logError(
0644: "[UIUtility.getAttributeValue]: Problem getting entity atribute definition "
0645: + entityName + "." + attributeName, module);
0646: Debug.logError(e.getMessage(), module);
0647: }
0648:
0649: return null;
0650: }
0651:
0652: /**
0653: * DOCUMENT ME!
0654: *
0655: * @param selectedEntityPKL
0656: * @param eligibleEntityL
0657: *
0658: * @return
0659: */
0660: public static List removeSelectedEligibleEntities(
0661: List selectedEntityPKL, List eligibleEntityL) {
0662: Debug.logVerbose("[activityContactSelectAvailable] Start",
0663: module);
0664:
0665: Debug.logVerbose(
0666: "[activityContactSelectAvailable] selectedEntityPKL: "
0667: + selectedEntityPKL, module);
0668:
0669: Iterator eligibleEntityI = eligibleEntityL.iterator();
0670:
0671: while (eligibleEntityI.hasNext()) {
0672: GenericValue eligibleGV = (GenericValue) eligibleEntityI
0673: .next();
0674:
0675: // Look through the assigned keys to see if this key is already selected.
0676: Iterator selectedEntityPKI = selectedEntityPKL.iterator();
0677:
0678: while (selectedEntityPKI.hasNext()) {
0679: GenericPK selectedEntityPK = (GenericPK) selectedEntityPKI
0680: .next();
0681:
0682: if (selectedEntityPK.equals(eligibleGV.getPrimaryKey())) {
0683: Debug.logVerbose(
0684: "[activityContactSelectAvailable] Removing a selected item for PK "
0685: + selectedEntityPK.toString(),
0686: module);
0687:
0688: // This entity has already been selected. Remove it from the eligible list.
0689: eligibleEntityI.remove();
0690:
0691: break;
0692: }
0693: }
0694: }
0695:
0696: Debug
0697: .logVerbose(
0698: "[activityContactSelectAvailable] eligibleEntityL after removing selected items: "
0699: + eligibleEntityL, module);
0700: Debug
0701: .logVerbose("[activityContactSelectAvailable] End",
0702: module);
0703:
0704: return eligibleEntityL;
0705: }
0706:
0707: /**
0708: * DOCUMENT ME!
0709: *
0710: * @param action
0711: * @param fieldInfo
0712: *
0713: * @return
0714: */
0715: public static boolean getIsCopiedPrimaryKey(String action,
0716: UIFieldInfo fieldInfo) {
0717: if (action.equals(UIScreenSection.ACTION_SHOW_COPY)
0718: && fieldInfo.getUiAttribute().getIsPk()) {
0719: return true;
0720: } else {
0721: return false;
0722: }
0723: }
0724:
0725: /**
0726: * DOCUMENT ME!
0727: *
0728: * @param attributeValueSource
0729: * @param entityDetailsVector
0730: * @param currentAttibuteName
0731: *
0732: * @return
0733: *
0734: * @throws GenericEntityException
0735: */
0736: public static String decodeAttributeValue(
0737: String attributeValueSource, Vector entityDetailsVector,
0738: String currentAttibuteName) throws GenericEntityException {
0739: String screenEntityName = "";
0740: String screenAttributeName = "";
0741: GenericValue screenEntityDetails = null;
0742:
0743: Debug.logVerbose(
0744: "-->[UIUtility.decodeFieldValue] attributeValueSource: "
0745: + attributeValueSource, module);
0746:
0747: if (attributeValueSource.indexOf("\"") == -1) {
0748: // There are no quote marks in the attribute name, so it is really an attribute name. Get the value of this
0749: // attribute from the screen entity.
0750: Debug.logVerbose(
0751: "-->[UIUtility.decodeFieldValue] No quotes found in '"
0752: + attributeValueSource + "'", module);
0753:
0754: // If there is a "." in the attribute name, split off the entity name.
0755: StringTokenizer tokPeriod = new StringTokenizer(
0756: attributeValueSource, ".");
0757:
0758: if (tokPeriod.countTokens() == 1) {
0759: // There is no "." in the attribute string. Assume we need to use the primary screen entity.
0760: Debug.logVerbose(
0761: "-->[UIUtility.decodeFieldValue] No period found in '"
0762: + attributeValueSource
0763: + "'. Using the primary entity",
0764: module);
0765:
0766: screenEntityName = ((GenericValue) (entityDetailsVector
0767: .get(0))).getEntityName();
0768: screenAttributeName = attributeValueSource;
0769: } else {
0770: // Need to get the entity specified before the "." in the attribute name string.
0771: Debug
0772: .logVerbose(
0773: "-->[UIUtility.decodeFieldValue] Period found in '"
0774: + attributeValueSource
0775: + "'. Parsing the primary entity out of the find def.",
0776: module);
0777:
0778: screenEntityName = tokPeriod.nextToken();
0779: screenAttributeName = tokPeriod.nextToken();
0780: }
0781:
0782: tokPeriod = null;
0783:
0784: if (screenAttributeName.equals("#currentField")) {
0785: screenAttributeName = (currentAttibuteName == null) ? ""
0786: : currentAttibuteName;
0787: Debug.logVerbose(
0788: "-->[UIUtility.decodeFieldValue] Replacing '#currentField' with '"
0789: + screenAttributeName + "'", module);
0790: }
0791:
0792: Debug.logVerbose(
0793: "-->[UIUtility.decodeFieldValue] Find Def Entity = "
0794: + screenEntityName, module);
0795: Debug.logVerbose(
0796: "-->[UIUtility.decodeFieldValue] Find Def Attrib = "
0797: + screenAttributeName, module);
0798:
0799: // Get the specified screen entity from the vector.
0800: Iterator entityDetailsIterator = entityDetailsVector
0801: .iterator();
0802:
0803: while (entityDetailsIterator.hasNext()) {
0804: GenericValue testEntity = (GenericValue) entityDetailsIterator
0805: .next();
0806:
0807: if (testEntity.getEntityName().equals(screenEntityName)) {
0808: screenEntityDetails = testEntity;
0809: }
0810: }
0811:
0812: try {
0813: String dummy = String.valueOf(screenEntityDetails
0814: .get(screenAttributeName));
0815: } catch (IllegalArgumentException e) {
0816: throw new GenericEntityException(
0817: "[UIUtility.decodeFieldValue]: Problem with attribute value definition \""
0818: + attributeValueSource + "\": "
0819: + e.getLocalizedMessage());
0820:
0821: // Debug.logWarning("[UIUtility.decodeFieldValue]: Problem with entity find definition \"" + entityFindDef + "\":");
0822: // Debug.logWarning(e.getMessage());
0823: // entityFindMap = null;
0824: // return entityFindMap;
0825: }
0826:
0827: return screenEntityDetails.getString(screenAttributeName);
0828: } else {
0829: Debug.logVerbose(
0830: "-->[UIUtility.decodeEntityFindDef] Quotes found in '"
0831: + attributeValueSource
0832: + "'. Using literal value.", module);
0833:
0834: // This is a literal value because it has quote marks around it.
0835: return attributeValueSource.substring(attributeValueSource
0836: .indexOf("\"") + 1, attributeValueSource
0837: .lastIndexOf("\""));
0838: }
0839: }
0840:
0841: /**
0842: * Looks up the read-only value for the field to be shown on the screen instead
0843: * of a drop down select because the mode is read-only.
0844: *
0845: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
0846: *
0847: * @param fieldValue Value stored or to be stored in the data base. Used as the primary key to look up the value to be displayed.
0848: * @param attributeName Name of the attribute being displayed
0849: * @param uiDisplayObject Reference to a display object defined in the data base and attached to the field to be displayed by the UI builder
0850: * @param entityDetailsVector Vector that holds one or more generic values from which the display value can be decoded
0851: * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
0852: *
0853: * @return Array containing the data value and display value.
0854: */
0855: public static GenericValue getReadOnlyValue(String fieldValue,
0856: String attributeName, UIDisplayObject uiDisplayObject,
0857: Vector entityDetailsVector, GenericDelegator delegator) {
0858:
0859: GenericValue gv = null;
0860:
0861: if (fieldValue.trim().equals("")) {
0862: Debug.logVerbose(
0863: "[getReadOnlyValue] Field value is empty.", module);
0864:
0865: // Field value is empty. Just display an empty string, and create a hidden field.
0866: return gv;
0867: } else {
0868: Debug
0869: .logVerbose(
0870: "[getReadOnlyValue] Field value has contents. About to create the findByAnd field map.",
0871: module);
0872:
0873: // Decode the primary key entity find definition into a hash map that can be used
0874: // to create the primary key necessary for the findByPrimaryKey function.
0875: HashMap entityFindMap = UIUtility.decodeEntityFindDef(
0876: uiDisplayObject.getAttribEntityPkFindDef(),
0877: entityDetailsVector, attributeName);
0878:
0879: if (entityFindMap == null) {
0880: Debug.logWarning("[getReadOnlyValue] Skipping field \""
0881: + attributeName
0882: + "\" because of faulty find definition: "
0883: + uiDisplayObject.getAttribEntityPkFindDef(),
0884: module);
0885:
0886: return gv;
0887: }
0888:
0889: Debug.logVerbose(
0890: "[getReadOnlyValue] Finished calling decodeEntityFindDef to decode find def '"
0891: + uiDisplayObject
0892: .getAttribEntityPkFindDef() + "'.",
0893: module);
0894: Debug.logVerbose(
0895: "[getReadOnlyValue] entityFindMap -> "
0896: + entityFindMap.toString(), module);
0897:
0898: // Find the entity by its primary key.
0899: ModelEntity entityEntity = delegator
0900: .getModelEntity(uiDisplayObject.getAttribEntity());
0901: GenericPK entityPk = new GenericPK(entityEntity,
0902: entityFindMap);
0903:
0904: try {
0905: gv = delegator.findByPrimaryKeyCache(entityPk);
0906: } catch (GenericEntityException e) {
0907: Debug.logError(
0908: "[getReadOnlyValue] Error searching for read-only value for drop down: "
0909: + e.getLocalizedMessage(), module);
0910: }
0911:
0912: if (gv == null) {
0913:
0914: gv = new GenericValue(entityPk);
0915:
0916: Debug.logWarning("[getReadOnlyValue] Skipping field \""
0917: + attributeName
0918: + "\" because no entity was found for value \""
0919: + fieldValue + "\" using find definition \""
0920: + uiDisplayObject.getAttribEntityPkFindDef()
0921: + "\"", module);
0922: }
0923:
0924: return gv;
0925: }
0926: }
0927:
0928: /**
0929: * Add criteria to
0930: *
0931: * @param entityFindDef
0932: * @param entityDetailsVector
0933: * @param currentAttibuteName
0934: *
0935: * @return
0936: */
0937: public static boolean addSelectSearch(QueryInfo queryInfo,
0938: String displayObjectId, String entityName,
0939: String attributeName, String aliasName,
0940: EntityComparisonOperator searchOperator, Object searchValue) {
0941: try {
0942: GenericDelegator delegator = queryInfo.getDelegator();
0943: UIDisplayObject displayObject = new UIDisplayObject(
0944: displayObjectId, delegator);
0945: displayObject.loadAttributes();
0946:
0947: // Split the entity find definition into pieces. Example:
0948: // * attribEntityFindDef:
0949: // * sectionId:sectionId;partyId:"-1"
0950: // * Resulting find map pairs:
0951: // * sectionId:43
0952: // * partyId:-1
0953: // * Resulting find logic:
0954: // * sectionId = 43 AND partyId = -1
0955:
0956: String findAttributeValue = "";
0957: List joinList = new ArrayList();
0958: String joinEntity = "";
0959: HashMap conditionMap = new HashMap();
0960:
0961: String entityFindDef = displayObject
0962: .getAttribEntityPkFindDef();
0963:
0964: StringTokenizer tokSemicolon = new StringTokenizer(
0965: entityFindDef, ";");
0966:
0967: while (tokSemicolon.hasMoreTokens()) {
0968:
0969: String pair = tokSemicolon.nextToken();
0970: StringTokenizer tokColon = new StringTokenizer(pair,
0971: ":");
0972:
0973: if (tokColon.countTokens() != 2) {
0974: Debug.logWarning(
0975: "-->[UIUtility.decodeEntityFindDef] No colon found in '"
0976: + pair + "'" + "for entityFindDef"
0977: + entityFindDef, module);
0978:
0979: return false;
0980: } else {
0981:
0982: String findAttributeName = tokColon.nextToken();
0983: if (findAttributeName.equals("#currentField"))
0984: findAttributeName = attributeName;
0985:
0986: // String screenAttributeString = tokColon.nextToken();
0987: String attributeValueSource = tokColon.nextToken();
0988: if (attributeValueSource.equals("#currentField"))
0989: attributeValueSource = attributeName;
0990:
0991: if (attributeValueSource.indexOf("\"") > -1) {
0992: attributeValueSource = attributeValueSource
0993: .substring(attributeValueSource
0994: .indexOf("\"") + 1,
0995: attributeValueSource
0996: .lastIndexOf("\""));
0997: conditionMap.put(findAttributeName,
0998: attributeValueSource);
0999: } else {
1000: StringTokenizer tokPeriod = new StringTokenizer(
1001: attributeValueSource, ".");
1002:
1003: String relatedEntity = "";
1004: if (tokPeriod.countTokens() == 1) {
1005: // There is no "." in the attribute string. Assume we need to use the primary screen entity.
1006: relatedEntity = entityName;
1007: } else {
1008: // Need to get the entity specified before the "." in the attribute name string.
1009: relatedEntity = tokPeriod.nextToken();
1010: attributeValueSource = tokPeriod
1011: .nextToken();
1012: }
1013: if ((joinEntity.length() > 0)
1014: && (!joinEntity.equals(relatedEntity))) {
1015: Debug
1016: .logError(
1017: "Invalid Join Condition, attempting to join lookup table to two different entities",
1018: module);
1019: return false;
1020: }
1021: joinEntity = relatedEntity;
1022:
1023: joinList
1024: .add(new ModelKeyMap(
1025: attributeValueSource,
1026: findAttributeName));
1027: }
1028:
1029: }
1030: }
1031:
1032: if (joinList.size() < 1) {
1033: Debug
1034: .logWarning(
1035: "EntityFindDef has no join with primary tables",
1036: module);
1037: return false;
1038: }
1039:
1040: queryInfo.addJoin(joinEntity, joinEntity, displayObject
1041: .getAttribEntity(), aliasName, Boolean
1042: .valueOf(false), joinList);
1043:
1044: Iterator conditionIter = conditionMap.entrySet().iterator();
1045: while (conditionIter.hasNext()) {
1046: Map.Entry set = (Map.Entry) conditionIter.next();
1047: String fieldName = (String) set.getKey();
1048: String fieldValue = (String) set.getValue();
1049:
1050: queryInfo.addCondition(aliasName, "c" + aliasName
1051: + fieldName, fieldName, EntityOperator.EQUALS,
1052: fieldValue);
1053: }
1054:
1055: String listDisplayField = displayObject
1056: .getAttribEntityDisplayDef();
1057: if (listDisplayField.indexOf(";") > 0)
1058: listDisplayField = listDisplayField
1059: .substring(listDisplayField.lastIndexOf(";") + 1);
1060:
1061: queryInfo.addCondition(aliasName, "c" + aliasName
1062: + listDisplayField, listDisplayField,
1063: searchOperator, searchValue);
1064:
1065: return true;
1066: } catch (GenericEntityException e) {
1067: Debug.logError("Unable to generate search on lookup table"
1068: + e.getMessage(), module);
1069: e.printStackTrace();
1070: return false;
1071: }
1072: }
1073:
1074: }
|