001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.ui;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.StringTokenizer;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: import org.ofbiz.base.util.Debug;
032: import org.ofbiz.base.util.UtilFormatOut;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.entity.GenericDelegator;
035: import org.ofbiz.entity.GenericEntityException;
036: import org.ofbiz.entity.GenericValue;
037: import org.ofbiz.entity.condition.EntityComparisonOperator;
038: import org.ofbiz.entity.condition.EntityCondition;
039: import org.ofbiz.entity.condition.EntityConditionList;
040: import org.ofbiz.entity.condition.EntityExpr;
041: import org.ofbiz.entity.condition.EntityOperator;
042: import org.ofbiz.entity.model.ModelEntity;
043: import org.ofbiz.entity.util.EntityListIterator;
044:
045: import com.sourcetap.sfa.event.EventUtility;
046: import com.sourcetap.sfa.event.GenericEventProcessor;
047: import com.sourcetap.sfa.replication.GenericReplicator;
048: import com.sourcetap.sfa.security.SecurityLinkInfo;
049: import com.sourcetap.sfa.security.SecurityWrapper;
050: import com.sourcetap.sfa.util.QueryInfo;
051: import com.sourcetap.sfa.util.UserInfo;
052:
053: public class UIList {
054: public static final String module = UIList.class.getName();
055:
056: protected boolean primaryFieldsLoaded = false;
057: protected boolean relatedFieldsLoaded = false;
058: protected List primaryFields = null;
059: protected List relatedFields = null;
060: protected String listId = null;
061: protected String listName = null;
062:
063: /**
064: * @return Returns the detailSectionId.
065: */
066: public String getDetailSectionId() {
067: return detailSectionId;
068: }
069:
070: /**
071: * @param detailSectionId The detailSectionId to set.
072: */
073: public void setDetailSectionId(String detailSectionId) {
074: this .detailSectionId = detailSectionId;
075: }
076:
077: /**
078: * @return Returns the listSectionId.
079: */
080: public String getListSectionId() {
081: return listSectionId;
082: }
083:
084: /**
085: * @param listSectionId The listSectionId to set.
086: */
087: public void setListSectionId(String listSectionId) {
088: this .listSectionId = listSectionId;
089: }
090:
091: protected String listSectionId = null;
092: protected String detailSectionId = null;
093: protected String partyId = null;
094: protected String queryId = null;
095: protected String listType = null;
096:
097: protected List reportFields = null;
098: protected List reportCriteria = null;
099: protected List reportOrderBy = null;
100:
101: private List selectFieldLabels = null;
102: private List selectFieldNames = null;
103: protected GenericDelegator delegator = null;
104: protected QueryInfo queryInfo = null;
105: protected GenericEventProcessor eventProcessor = null;
106:
107: public UIList(GenericDelegator delegator_) {
108: setDelegator(delegator_);
109: }
110:
111: public UIList(String listId_, GenericDelegator delegator_) {
112: setDelegator(delegator_);
113: loadList(listId_);
114: }
115:
116: public void loadList(String listId_) {
117: try {
118: GenericValue listGV = delegator.findByPrimaryKey("UiList",
119: UtilMisc.toMap("listId", listId_));
120: if (listGV == null)
121: throw new IllegalArgumentException(
122: "Unable to find report with ID " + listId_);
123:
124: setListId(listId_);
125: setPartyId(listGV.getString("partyId"));
126: setListName(listGV.getString("listName"));
127:
128: // the query was save with the screen section for the LIST screen section. Get the corresponding detail screen section
129: // which will have a more complete list of fields to choose from
130: String listSectionId = listGV.getString("sectionId");
131: String detailSectionId = listSectionId;
132: setListSectionId(listSectionId);
133:
134: GenericValue sectionGV = delegator.findByPrimaryKey(
135: "UiScreenSection", UtilMisc.toMap("sectionId",
136: listSectionId));
137: GenericValue screenGV = delegator.findByPrimaryKey(
138: "UiScreen", UtilMisc.toMap("screenId", sectionGV
139: .getString("screenId")));
140: detailSectionId = screenGV.getString("detailSectionId");
141:
142: if ((detailSectionId == null)
143: || (detailSectionId.length() < 1))
144: detailSectionId = listSectionId;
145:
146: setDetailSectionId(detailSectionId);
147:
148: } catch (GenericEntityException e) {
149: Debug.logError("Error getting report: " + e.getMessage(),
150: module);
151: e.printStackTrace();
152: }
153: }
154:
155: public void saveReport(String partyId, String listName) {
156: setPartyId(partyId);
157:
158: if (!listName.equals(getListName())) {
159: setListName(listName);
160: setListId(null);
161: }
162:
163: saveList();
164: }
165:
166: public void saveList() {
167: try {
168: String listId = getListId();
169:
170: if (listId == null) {
171: listId = GenericReplicator.getNextSeqId("UiList",
172: delegator);
173: setListId(listId);
174: } else {
175: // TODO need to delete old report info
176: }
177:
178: List valuesToStore = new ArrayList();
179:
180: GenericValue listGV = new GenericValue(delegator
181: .getModelEntity("UiList"));
182: listGV.setDelegator(delegator);
183: listGV.set("listId", listId);
184: listGV.set("sectionId", getListSectionId());
185: listGV.set("partyId", getPartyId());
186: listGV.set("listName", getListName());
187: listGV.set("queryId", getQueryId());
188: listGV.set("listType", getListType());
189:
190: valuesToStore.add(listGV);
191:
192: delegator.storeAll(valuesToStore);
193: } catch (GenericEntityException e) {
194: e.printStackTrace();
195: Debug
196: .logError("error saving list" + e.getMessage(),
197: module);
198: }
199: }
200:
201: public static boolean deleteList(String listId,
202: GenericDelegator delegator) {
203: try {
204: List valuesToBeRemoved = new ArrayList();
205:
206: GenericValue gv = new GenericValue(delegator
207: .getModelEntity("UiListItem"));
208: gv.set("listId", listId);
209: valuesToBeRemoved.add(gv);
210:
211: gv = new GenericValue(delegator.getModelEntity("UiList"));
212: gv.set("listId", listId);
213: valuesToBeRemoved.add(gv);
214:
215: delegator.removeAll(valuesToBeRemoved);
216: } catch (GenericEntityException e) {
217: // TODO Auto-generated catch block
218: e.printStackTrace();
219: Debug.logError("unable to delete list: " + listId
220: + " error:" + e.getMessage(), module);
221: return false;
222: }
223: return true;
224: }
225:
226: public static List getLists(String companyId, String partyId,
227: GenericDelegator delegator) {
228: return getLists(null, companyId, partyId, delegator);
229: }
230:
231: public static List getLists(String sectionId, String companyId,
232: String partyId, GenericDelegator delegator) {
233: try {
234: Map condition = sectionId == null ? UtilMisc.toMap(
235: "partyId", "-1") : UtilMisc.toMap("sectionId",
236: sectionId, "partyId", "-1");
237: List orderBy = UtilMisc.toList("reportName");
238:
239: List reportList = delegator.findByAnd("UiReport",
240: condition, orderBy);
241: if (!partyId.equals("-1")) {
242:
243: EntityCondition c1 = new EntityConditionList(UtilMisc
244: .toList(new EntityExpr("partyId",
245: EntityOperator.EQUALS, partyId),
246: new EntityExpr("partyId",
247: EntityOperator.EQUALS,
248: companyId)), EntityOperator.OR);
249:
250: if (sectionId != null)
251: c1 = new EntityConditionList(UtilMisc.toList(c1,
252: new EntityExpr("sectionId",
253: EntityOperator.EQUALS, sectionId)),
254: EntityOperator.AND);
255:
256: if (reportList == null)
257: reportList = delegator.findByCondition("UiList",
258: c1, null, orderBy);
259: else
260: reportList.addAll(delegator.findByCondition(
261: "UiList", c1, null, orderBy));
262: }
263:
264: return reportList;
265: } catch (GenericEntityException e) {
266: Debug.logError("Error getting reportList: "
267: + e.getMessage(), module);
268: e.printStackTrace();
269: }
270: return null;
271: }
272:
273: public List loadPrimarySectionFields() {
274: try {
275: List primaryFields = getDelegator().findByAnd(
276: "UiScreenSectionFields",
277: UtilMisc.toMap("sectionId", getDetailSectionId()),
278: UtilMisc.toList("displayOrder"));
279:
280: setPrimaryFields(primaryFields);
281: return primaryFields;
282: } catch (GenericEntityException e) {
283: Debug.logError("Unable to get Primary Screen Fields:"
284: + e.getMessage(), module);
285: return null;
286: }
287: }
288:
289: public List loadRelatedSectionQueryFields() {
290: try {
291: List relatedFields = getDelegator()
292: .findByAnd(
293: "UiRelatedSectionQueryFields",
294: UtilMisc.toMap("sectionId",
295: getDetailSectionId(),
296: "useInAdvancedQueries", "Y",
297: "isSearchable", "Y"),
298: UtilMisc.toList("relatedSectionId",
299: "displayOrder"));
300: setRelatedFields(relatedFields);
301: return relatedFields;
302: } catch (GenericEntityException e) {
303: Debug.logError("Unable to get Related Screen Fields:"
304: + e.getMessage(), module);
305: return null;
306: }
307: }
308:
309: public String getSelectListAvailableOptions() {
310: if (detailSectionId == null)
311: throw new IllegalArgumentException(
312: "section ID must be set before calling getConditionHtml");
313:
314: StringBuffer displayHtml = new StringBuffer(5000);
315: List fieldList = getPrimaryFields();
316: int fieldListSize = fieldList.size();
317:
318: StringBuffer fieldNameOptions = new StringBuffer(200);
319: // fieldNameOptions.append("<option value=''></option>");
320: for (int fieldNbr = 0; fieldNbr < fieldListSize; fieldNbr++) {
321: GenericValue fieldInfo = (GenericValue) fieldList
322: .get(fieldNbr);
323: String isVisible = UtilFormatOut.checkNull(fieldInfo
324: .getString("isVisible"));
325: if (isVisible.equals("Y")) {
326: String htmlName = UIWebUtility.getHtmlName(fieldInfo
327: .getString("sectionName"), fieldInfo
328: .getString("entityName"), fieldInfo
329: .getString("attributeName"), 0);
330: String displayObjectId = fieldInfo
331: .getString("displayObjectId");
332: String displayTypeId = fieldInfo
333: .getString("displayTypeId");
334: String attributeId = fieldInfo.getString("attributeId");
335: String displayLabel = fieldInfo
336: .getString("displayLabel");
337: String optValue = htmlName + ";" + attributeId + ";"
338: + displayTypeId + ";" + displayObjectId + ";"
339: + displayLabel;
340: fieldNameOptions.append("<option value='" + optValue
341: + "'>" + displayLabel + "</option>");
342: }
343:
344: }
345:
346: List extendedFields = getRelatedFields();
347: if (extendedFields != null) {
348: int numFields = extendedFields.size();
349: for (int i = 0; i < numFields; i++) {
350: GenericValue fieldInfo = (GenericValue) extendedFields
351: .get(i);
352: String entityName = fieldInfo.getString("entityName");
353: String attributeId = fieldInfo.getString("attributeId");
354: String attributeName = fieldInfo
355: .getString("attributeName");
356: String displayObjectId = fieldInfo
357: .getString("displayObjectId");
358: String displayTypeId = fieldInfo
359: .getString("displayTypeId");
360: String sectionName = fieldInfo.getString("sectionName");
361: String sectionDescription = fieldInfo
362: .getString("sectionDescription");
363: String displayLabel = fieldInfo
364: .getString("displayLabel");
365: String htmlName = UIWebUtility.getHtmlName(sectionName,
366: entityName, attributeName, 0);
367: String optValue = htmlName + ";" + attributeId + ";"
368: + displayTypeId + ";" + displayObjectId + ";"
369: + displayLabel;
370: displayLabel = sectionDescription + "." + displayLabel;
371: fieldNameOptions.append("<option value='" + optValue
372: + "'>" + displayLabel + "</option>");
373: }
374: }
375:
376: return fieldNameOptions.toString();
377: }
378:
379: public boolean loadQueryInfo(HttpServletRequest request,
380: HttpServletResponse response, UICache uiCache) {
381:
382: try {
383: List primaryFields = loadPrimarySectionFields();
384:
385: List uiScreenSectionEntityGVL = delegator.findByAnd(
386: "UiScreenSectionEntity", UtilMisc.toMap(
387: "sectionId", detailSectionId), UtilMisc
388: .toList("retrieveOrder"));
389:
390: UIScreenSectionEntity primaryEntity = new UIScreenSectionEntity(
391: (GenericValue) uiScreenSectionEntityGVL.get(0),
392: delegator, uiCache);
393: GenericValue screenSectionGV = delegator.findByPrimaryKey(
394: "UiScreenSection", UtilMisc.toMap("sectionId",
395: detailSectionId));
396: String eventProcessorClass = screenSectionGV
397: .getString("eventProcessorClass");
398: eventProcessor = UIUtility
399: .getEventProcessor(eventProcessorClass);
400:
401: String primaryEntityName = primaryEntity.getUiEntity()
402: .getEntityName();
403: String searchAttribValue = "";
404: String searchEntityName = "";
405: String searchAttribName = "";
406: EntityComparisonOperator entityOperator = null;
407: List relatedSearchClauses = new LinkedList();
408: List selectFields = new ArrayList();
409: List selectFieldLabels = new ArrayList();
410: List sortFields = new ArrayList();
411:
412: List reportFields = new ArrayList();
413: List reportCriteria = new ArrayList();
414: List reportOrderBy = new ArrayList();
415:
416: ModelEntity primaryME = primaryEntity.getUiEntity()
417: .getModelEntity();
418: List primaryPkFieldNames = primaryEntity.getUiEntity()
419: .getPrimaryKeyFieldNames();
420:
421: queryInfo = new QueryInfo(delegator, primaryEntityName);
422:
423: HashMap joinedEntities = new HashMap();
424:
425: joinedEntities.put(primaryEntityName, "Y");
426:
427: // Find the main entity(ies) by building a WHERE clause using LIKE and AND, with one or more tables in the FROM clause.
428: // First join all secondary screen section entities in the WHERE and FROM clauses in case
429: // we are going to use query values from them.
430: Iterator uiScreenSectionEntityI = uiScreenSectionEntityGVL
431: .iterator();
432:
433: uiScreenSectionEntityI.next(); // Pass up the primary entity.
434:
435: while (uiScreenSectionEntityI.hasNext()) {
436: GenericValue entityGV = (GenericValue) uiScreenSectionEntityI
437: .next();
438: UIScreenSectionEntity uiScreenSectionEntity = new UIScreenSectionEntity(
439: entityGV, delegator, uiCache);
440: String relationTitle = uiScreenSectionEntity
441: .getRelationTitle();
442: String relatedEntityName = uiScreenSectionEntity
443: .getUiEntity().getEntityName();
444: String relatedAndFields = uiScreenSectionEntity
445: .getRelationByAndFields();
446: boolean isOuterJoin = uiScreenSectionEntity
447: .getIsOuterJoined();
448:
449: eventProcessor.addOneRelationClause(delegator,
450: relationTitle, relatedAndFields,
451: relatedEntityName, primaryEntityName,
452: primaryME, isOuterJoin, queryInfo);
453:
454: joinedEntities.put(relatedEntityName, "Y");
455: }
456:
457: String pkFieldName = (String) primaryPkFieldNames.get(0);
458: queryInfo.addJoin(primaryEntityName, "UiListItem",
459: Boolean.FALSE, pkFieldName, "entityId");
460: queryInfo.addCondition("UiListItem", "listId",
461: EntityOperator.EQUALS, getListId());
462:
463: List fieldList = getPrimaryFields();
464: int fieldListSize = fieldList.size();
465:
466: StringBuffer fieldNameOptions = new StringBuffer(200);
467: for (int fieldNbr = 0; fieldNbr < fieldListSize; fieldNbr++) {
468: GenericValue fieldInfo = (GenericValue) fieldList
469: .get(fieldNbr);
470: String entityName = fieldInfo.getString("entityName");
471: String attributeName = fieldInfo
472: .getString("attributeName");
473: String aliasName = entityName + "_" + attributeName;
474:
475: if (!entityName.equals(primaryEntityName))
476: queryInfo.checkAttribute(entityName, attributeName,
477: aliasName);
478: }
479:
480: } catch (NumberFormatException e) {
481: Debug.logError(
482: "Error formatting Number: " + e.getMessage(),
483: module);
484: return false;
485: } catch (GenericEntityException e) {
486: Debug.logError(
487: "Error loading QueryInfo: " + e.getMessage(),
488: module);
489: return false;
490: }
491: return true;
492: }
493:
494: public List getListItems(UserInfo userInfo, int maxRows) {
495: EntityListIterator eli = null;
496:
497: if (eventProcessor == null)
498: eventProcessor = new GenericEventProcessor();
499:
500: if (queryInfo == null)
501: throw new RuntimeException(
502: "You must call loadQueryInfo prior to getListItems");
503:
504: SecurityLinkInfo securityInfo = eventProcessor
505: .getSecurityLinkInfo(userInfo, delegator);
506: if (securityInfo == null) {
507: try {
508: eli = queryInfo.getQueryIterator();
509: } catch (GenericEntityException e) {
510: Debug.logError("An error occurred in the "
511: + "GenericDelegator.findByCondition method: "
512: + e.getLocalizedMessage(), module);
513: }
514: } else {
515: try {
516:
517: eli = SecurityWrapper.findListIteratorByCondition(
518: queryInfo.getPrimaryEntity(), queryInfo,
519: queryInfo.getOrderBy(), userInfo, securityInfo,
520: delegator);
521: } catch (GenericEntityException e) {
522: Debug.logError("An error occurred in the "
523: + "SecurityWrapper.findByCondition method: "
524: + e.getLocalizedMessage(), module);
525: }
526: }
527:
528: if (eli != null) {
529: try {
530: List resultGVL = eli.getPartialList(1, maxRows);
531:
532: boolean hasMoreRows = false;
533:
534: // check next value since we close the cursor anyway.
535: if ((resultGVL.size() == maxRows)
536: && (eli.next() != null))
537: hasMoreRows = true;
538:
539: eli.close();
540:
541: return resultGVL;
542:
543: } catch (GenericEntityException e) {
544: Debug.logError(
545: "An error occurred in the getPartialList method: "
546: + e.getLocalizedMessage(), module);
547: }
548: }
549:
550: return new ArrayList();
551: }
552:
553: public GenericDelegator getDelegator() {
554: return delegator;
555: }
556:
557: public void setDelegator(GenericDelegator delegator_) {
558: delegator = delegator_;
559: }
560:
561: /**
562: * @return
563: */
564: public String getPartyId() {
565: return partyId;
566: }
567:
568: /**
569: * @return
570: */
571: public List getPrimaryFields() {
572: if (!primaryFieldsLoaded)
573: loadPrimarySectionFields();
574: return primaryFields;
575: }
576:
577: /**
578: * @return
579: */
580: public List getRelatedFields() {
581: if (!relatedFieldsLoaded)
582: loadRelatedSectionQueryFields();
583: return relatedFields;
584: }
585:
586: /**
587: * @return
588: */
589: public String getListId() {
590: return listId;
591: }
592:
593: /**
594: * @return
595: */
596: public String getListName() {
597: return listName;
598: }
599:
600: /**
601: * @param string
602: */
603: public void setPartyId(String string) {
604: partyId = string;
605: }
606:
607: /**
608: * @param list
609: */
610: public void setPrimaryFields(List list) {
611: primaryFields = list;
612: primaryFieldsLoaded = true;
613: }
614:
615: /**
616: * @param list
617: */
618: public void setRelatedFields(List list) {
619: relatedFields = list;
620: relatedFieldsLoaded = true;
621: }
622:
623: /**
624: * @param string
625: */
626: public void setListId(String string) {
627: listId = string;
628: }
629:
630: /**
631: * @param string
632: */
633: public void setListName(String string) {
634: listName = string;
635: }
636:
637: /**
638: * @return
639: */
640: public List getReportCriteria() {
641: return reportCriteria;
642: }
643:
644: /**
645: * @return
646: */
647: public List getReportFields() {
648: return reportFields;
649: }
650:
651: /**
652: * @return
653: */
654: public List getReportOrderBy() {
655: return reportOrderBy;
656: }
657:
658: /**
659: * @return
660: */
661: public String getPrimaryEntityName() {
662: if (queryInfo == null)
663: return null;
664: return queryInfo.getPrimaryEntity();
665: }
666:
667: /**
668: * @param list
669: */
670: public void setReportCriteria(List list) {
671: reportCriteria = list;
672: }
673:
674: /**
675: * @param list
676: */
677: public void setReportFields(List list) {
678: reportFields = list;
679: }
680:
681: /**
682: * @param list
683: */
684: public void setReportOrderBy(List list) {
685: reportOrderBy = list;
686: }
687:
688: protected List getStringListfromGVL(List gvl, String fieldName) {
689: List sList = new ArrayList();
690:
691: Iterator iter = gvl.iterator();
692: while (iter.hasNext()) {
693: GenericValue gv = (GenericValue) iter.next();
694: sList.add(gv.getString(fieldName));
695: }
696: return sList;
697: }
698:
699: /**
700: * @return
701: */
702: public List getSelectFieldLabels() {
703: return selectFieldLabels;
704: }
705:
706: /**
707: * @return
708: */
709: public List getSelectFieldNames() {
710: return selectFieldNames;
711: }
712:
713: /**
714: * @param list
715: */
716: public void setSelectFieldLabels(List list) {
717: selectFieldLabels = list;
718: }
719:
720: /**
721: * @param list
722: */
723: public void setSelectFieldNames(List list) {
724: selectFieldNames = list;
725: }
726:
727: /**
728: * @return Returns the listType.
729: */
730: public String getListType() {
731: return listType;
732: }
733:
734: /**
735: * @param listType The listType to set.
736: */
737: public void setListType(String listType) {
738: this .listType = listType;
739: }
740:
741: /**
742: * @return Returns the queryId.
743: */
744: public String getQueryId() {
745: return queryId;
746: }
747:
748: /**
749: * @param queryId The queryId to set.
750: */
751: public void setQueryId(String queryId) {
752: this.queryId = queryId;
753: }
754: }
|