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.sql.Timestamp;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.entity.GenericDelegator;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericPK;
028: import org.ofbiz.entity.GenericValue;
029:
030: import com.sourcetap.sfa.replication.GenericReplicator;
031:
032: /**
033: * DOCUMENT ME!
034: *
035: */
036: public class UIHistoryManager {
037: public static final String module = UIHistoryManager.class
038: .getName();
039:
040: private static final int MAX_HISTORY_SIZE = 10;
041: protected GenericDelegator delegator = null;
042:
043: public UIHistoryManager(GenericDelegator delegator_) {
044: this .delegator = delegator_;
045: }
046:
047: /**
048: * DOCUMENT ME!
049: *
050: * @param partyId
051: * @param historyUrl
052: * @param historyDescription
053: *
054: * @return
055: *
056: * @throws GenericEntityException
057: */
058: public String saveHistoryEvent(String partyId, String historyUrl,
059: String historyDescription) throws GenericEntityException {
060: Debug.logVerbose("[saveEventHistory] Start", module);
061:
062: // Create the value map
063: HashMap valueMap = new HashMap();
064: valueMap.put("partyId", partyId);
065: valueMap.put("url", historyUrl);
066: valueMap.put("description", historyDescription);
067:
068: // Check for duplicates first
069: try {
070: if (this
071: .duplicateHistoryExists(partyId, historyDescription)) {
072: return "N/A";
073: }
074: } catch (GenericEntityException e) {
075: throw e;
076: }
077:
078: // Need to get the timestamp
079: Timestamp now = new Timestamp(new java.util.Date().getTime());
080: valueMap.put("timestamp", now);
081:
082: // Get the seuence ID
083: String seqId = String.valueOf(GenericReplicator.getNextSeqId(
084: "UiUsageHistory", delegator));
085: valueMap.put("usageHistoryId", seqId);
086:
087: // First, create a generic value for the entity in question
088: GenericValue historyGV = delegator.makeValue("UiUsageHistory",
089: valueMap);
090:
091: Debug.logVerbose("[saveEventHistory] History Object to Store:"
092: + historyGV.toString(), module);
093:
094: try {
095: delegator.create(historyGV);
096: this .pruneUserHistory(partyId);
097: } catch (GenericEntityException e) {
098: throw e;
099: }
100:
101: return seqId;
102: }
103:
104: // Returns a colletion of GenericValues that need to be parsed through by the caller.
105: public List getUserHistoryEvents(String partyId)
106: throws GenericEntityException {
107: ArrayList orderBy = new ArrayList();
108: HashMap expressions = new HashMap();
109: List historyList = null;
110:
111: Debug.logVerbose("[getUserHistoryEvents] Start.", module);
112:
113: // Set up the search parameters. Use the primay key (partyId) and order by the timestamp.
114: expressions.put("partyId", partyId);
115: orderBy.add("timestamp DESC");
116:
117: try {
118: historyList = delegator.findByAnd("UiUsageHistory",
119: expressions, orderBy);
120: } catch (GenericEntityException e) {
121: Debug.logError(
122: "[getUserHistoryEvents] Error getting history.",
123: module);
124:
125: throw e;
126: }
127:
128: if (null != historyList) {
129: Debug.logVerbose("[getUserHistoryEvents] History Found: "
130: + historyList.toString(), module);
131: }
132:
133: Debug.logVerbose("[getUserHistoryEvents] Success.", module);
134:
135: return historyList;
136: }
137:
138: /**
139: * DOCUMENT ME!
140: *
141: * @param partyId
142: * @param description
143: *
144: * @return
145: *
146: * @throws GenericEntityException
147: */
148: private boolean duplicateHistoryExists(String partyId,
149: String description) throws GenericEntityException {
150: ArrayList orderBy = new ArrayList();
151: HashMap expressions = new HashMap();
152: List historyList = null;
153:
154: expressions.put("partyId", partyId);
155: expressions.put("description", description);
156:
157: try {
158: historyList = delegator.findByAnd("UiUsageHistory",
159: expressions, orderBy);
160: } catch (GenericEntityException e) {
161: Debug.logError(
162: "[checkForDuplicate] Error getting history.",
163: module);
164:
165: throw e;
166: }
167:
168: if (historyList.size() > 0) {
169:
170: return true;
171: }
172:
173: return false;
174: }
175:
176: /**
177: * DOCUMENT ME!
178: *
179: * @param partyId
180: *
181: * @throws GenericEntityException
182: */
183: private void pruneUserHistory(String partyId)
184: throws GenericEntityException {
185: ArrayList orderBy = new ArrayList();
186: HashMap expressions = new HashMap();
187: List historyList = null;
188: Object[] historyArray = null;
189:
190: expressions.put("partyId", partyId);
191: orderBy.add("timestamp");
192:
193: // First check to make sure that the max number of history items exists for this party ID.
194: try {
195: historyList = delegator.findByAnd("UiUsageHistory",
196: expressions, orderBy);
197: } catch (GenericEntityException e) {
198: Debug.logError("[pruneUserHistory] Error getting history.",
199: module);
200:
201: throw e;
202: }
203:
204: if (null != historyList) {
205: historyArray = historyList.toArray();
206:
207: } else {
208:
209: return;
210: }
211:
212: if (historyArray.length > MAX_HISTORY_SIZE) {
213: // Prune the oldest history item
214: Debug
215: .logVerbose(
216: "[pruneUserHistory] Party Id has max history size. Pruning.",
217: module);
218:
219: GenericValue pruneGV = (GenericValue) historyArray[0];
220: GenericPK prunePk = pruneGV.getPrimaryKey();
221:
222: try {
223: delegator.removeByPrimaryKey(prunePk);
224: } catch (GenericEntityException e) {
225: Debug
226: .logError(
227: "[pruneUserHistory] Error deleting from history list.",
228: module);
229:
230: throw e;
231: }
232: } else {
233: Debug
234: .logVerbose(
235: "[pruneUserHistory] Max Not Reached. No pruning requred.",
236: module);
237: }
238:
239: }
240: }
|