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.forecast;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.ofbiz.base.util.UtilMisc;
024: import org.ofbiz.entity.GenericDelegator;
025: import org.ofbiz.entity.GenericEntityException;
026: import org.ofbiz.entity.GenericValue;
027: import org.ofbiz.entity.condition.EntityCondition;
028: import org.ofbiz.entity.condition.EntityConditionList;
029: import org.ofbiz.entity.condition.EntityExpr;
030: import org.ofbiz.entity.condition.EntityOperator;
031: import org.ofbiz.entity.model.DynamicViewEntity;
032: import org.ofbiz.entity.model.ModelField;
033: import org.ofbiz.entity.model.ModelKeyMap;
034:
035: import com.sourcetap.sfa.util.EntityHelper;
036:
037: /**
038: *
039: * @author Chris Maurer
040: * @version 1.0
041: */
042: public class ForecastHelper {
043: public static final String module = ForecastHelper.class.getName();
044:
045: /**
046: * DOCUMENT ME!
047: *
048: * @param partyId
049: * @param beginDate
050: * @param endDate
051: * @param delegator
052: *
053: * @return
054: *
055: * @throws GenericEntityException
056: */
057: public static List getForecastForParty(String partyId,
058: java.sql.Date beginDate, java.sql.Date endDate,
059: GenericDelegator delegator) throws GenericEntityException {
060: ArrayList list = new ArrayList();
061: List returnList = null;
062:
063: // select X from Deal d, entity_access ea, team_member tm where ea.entity = "Deal" and ea.partyEntityType = "Team"
064: // and ea.party_id = tm.team_id and tm.party_id = <partyId> and ea.entity_id = d.deal_id
065: DynamicViewEntity dve = EntityHelper.createDynamicViewEntity(
066: delegator, "Deal");
067: dve.addMemberEntity("EntityAccess", "EntityAccess");
068: dve.addMemberEntity("TeamMember", "TeamMember");
069: dve.addViewLink("Deal", "EntityAccess", Boolean.FALSE, UtilMisc
070: .toList(new ModelKeyMap("dealId", "entityId")));
071: dve.addViewLink("EntityAccess", "TeamMember", Boolean.FALSE,
072: UtilMisc.toList(new ModelKeyMap("partyId", "teamId")));
073: dve.addAlias("EntityAccess", "partyEntityType", null, null,
074: null, null, null);
075: dve.addAlias("EntityAccess", "entity", null, null, null, null,
076: null);
077: dve.addAlias("TeamMember", "tmPartyId", "partyId", null, null,
078: null, null);
079:
080: EntityCondition condition = new EntityConditionList(UtilMisc
081: .toList(new EntityExpr("entity", EntityOperator.EQUALS,
082: "Deal"), new EntityExpr("partyEntityType",
083: EntityOperator.EQUALS, "Team"), new EntityExpr(
084: "tmPartyId", EntityOperator.EQUALS, partyId),
085: new EntityExpr("projectedCloseDate",
086: EntityOperator.LESS_THAN_EQUAL_TO,
087: endDate), new EntityExpr(
088: "projectedCloseDate",
089: EntityOperator.GREATER_THAN_EQUAL_TO,
090: beginDate), new EntityExpr(
091: "isInForecast", EntityOperator.EQUALS,
092: "1")), EntityOperator.AND);
093:
094: return EntityHelper.findByCondition(delegator, dve, condition,
095: UtilMisc.toList("projectedCloseDate"));
096: }
097:
098: /**
099: * DOCUMENT ME!
100: *
101: * @param partyId
102: * @param beginDate
103: * @param endDate
104: * @param delegator
105: *
106: * @return
107: *
108: * @throws GenericEntityException
109: */
110: public static String getForecastForPartyXML(String partyId,
111: java.sql.Date beginDate, java.sql.Date endDate,
112: GenericDelegator delegator) throws GenericEntityException {
113: return convertListToXML(getForecastForParty(partyId, beginDate,
114: endDate, delegator));
115: }
116:
117: /**
118: * DOCUMENT ME!
119: *
120: * @param list
121: *
122: * @return
123: *
124: * @throws GenericEntityException
125: */
126: public static String convertListToXML(List list)
127: throws GenericEntityException {
128: Iterator iter = list.iterator();
129: StringBuffer returnString = new StringBuffer();
130: returnString.append("<?xml version=\"1.0\"?>\n\r");
131:
132: GenericValue value = null;
133: String entity = "";
134: List fields = null;
135:
136: while (iter.hasNext()) {
137: value = (GenericValue) iter.next();
138: entity = value.getModelEntity().getEntityName();
139: returnString.append("<" + entity + ">\n\r");
140: fields = value.getModelEntity().getFieldsCopy();
141:
142: for (int i = 0; i < fields.size(); i++) {
143: ModelField field = (ModelField) fields.get(i);
144: returnString.append("< ");
145: returnString.append(field.getName());
146: returnString.append(" type=");
147: returnString.append(field.getType());
148: returnString.append(" colName=");
149: returnString.append(field.getColName());
150: returnString.append(" isPk=");
151: returnString.append(field.getIsPk());
152:
153: returnString.append(" >");
154:
155: returnString.append(String.valueOf(value.get(field
156: .getName())));
157:
158: returnString.append("</");
159: returnString.append(field.getName());
160: returnString.append(">");
161: }
162:
163: returnString.append("</" + entity + ">\n\r");
164: }
165:
166: return "";
167: }
168: }
|