/*
*
* Copyright (c) 2004 SourceTap - www.sourcetap.com
*
* The contents of this file are subject to the SourceTap Public License
* ("License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
*/
package com.sourcetap.sfa.ui;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericPK;
import org.ofbiz.entity.GenericValue;
import com.sourcetap.sfa.replication.GenericReplicator;
/**
* DOCUMENT ME!
*
*/
public class UIHistoryManager {
public static final String module = UIHistoryManager.class.getName();
private static final int MAX_HISTORY_SIZE = 10;
protected GenericDelegator delegator = null;
public UIHistoryManager(GenericDelegator delegator_) {
this.delegator = delegator_;
}
/**
* DOCUMENT ME!
*
* @param partyId
* @param historyUrl
* @param historyDescription
*
* @return
*
* @throws GenericEntityException
*/
public String saveHistoryEvent(String partyId, String historyUrl,
String historyDescription) throws GenericEntityException {
Debug.logVerbose("[saveEventHistory] Start", module);
// Create the value map
HashMap valueMap = new HashMap();
valueMap.put("partyId", partyId);
valueMap.put("url", historyUrl);
valueMap.put("description", historyDescription);
// Check for duplicates first
try {
if (this.duplicateHistoryExists(partyId, historyDescription)) {
return "N/A";
}
} catch (GenericEntityException e) {
throw e;
}
// Need to get the timestamp
Timestamp now = new Timestamp(new java.util.Date().getTime());
valueMap.put("timestamp", now);
// Get the seuence ID
String seqId = String.valueOf(GenericReplicator.getNextSeqId(
"UiUsageHistory", delegator));
valueMap.put("usageHistoryId", seqId);
// First, create a generic value for the entity in question
GenericValue historyGV = delegator.makeValue("UiUsageHistory", valueMap);
Debug.logVerbose("[saveEventHistory] History Object to Store:" +
historyGV.toString(), module);
try {
delegator.create(historyGV);
this.pruneUserHistory(partyId);
} catch (GenericEntityException e) {
throw e;
}
return seqId;
}
// Returns a colletion of GenericValues that need to be parsed through by the caller.
public List getUserHistoryEvents(String partyId)
throws GenericEntityException {
ArrayList orderBy = new ArrayList();
HashMap expressions = new HashMap();
List historyList = null;
Debug.logVerbose("[getUserHistoryEvents] Start.", module);
// Set up the search parameters. Use the primay key (partyId) and order by the timestamp.
expressions.put("partyId", partyId);
orderBy.add("timestamp DESC");
try {
historyList = delegator.findByAnd("UiUsageHistory", expressions,
orderBy);
} catch (GenericEntityException e) {
Debug.logError("[getUserHistoryEvents] Error getting history.", module);
throw e;
}
if (null != historyList) {
Debug.logVerbose("[getUserHistoryEvents] History Found: " +
historyList.toString(), module);
}
Debug.logVerbose("[getUserHistoryEvents] Success.", module);
return historyList;
}
/**
* DOCUMENT ME!
*
* @param partyId
* @param description
*
* @return
*
* @throws GenericEntityException
*/
private boolean duplicateHistoryExists(String partyId, String description)
throws GenericEntityException {
ArrayList orderBy = new ArrayList();
HashMap expressions = new HashMap();
List historyList = null;
expressions.put("partyId", partyId);
expressions.put("description", description);
try {
historyList = delegator.findByAnd("UiUsageHistory", expressions,
orderBy);
} catch (GenericEntityException e) {
Debug.logError("[checkForDuplicate] Error getting history.", module);
throw e;
}
if (historyList.size() > 0) {
return true;
}
return false;
}
/**
* DOCUMENT ME!
*
* @param partyId
*
* @throws GenericEntityException
*/
private void pruneUserHistory(String partyId) throws GenericEntityException {
ArrayList orderBy = new ArrayList();
HashMap expressions = new HashMap();
List historyList = null;
Object[] historyArray = null;
expressions.put("partyId", partyId);
orderBy.add("timestamp");
// First check to make sure that the max number of history items exists for this party ID.
try {
historyList = delegator.findByAnd("UiUsageHistory", expressions,
orderBy);
} catch (GenericEntityException e) {
Debug.logError("[pruneUserHistory] Error getting history.", module);
throw e;
}
if (null != historyList) {
historyArray = historyList.toArray();
} else {
return;
}
if (historyArray.length > MAX_HISTORY_SIZE) {
// Prune the oldest history item
Debug.logVerbose(
"[pruneUserHistory] Party Id has max history size. Pruning.", module);
GenericValue pruneGV = (GenericValue) historyArray[0];
GenericPK prunePk = pruneGV.getPrimaryKey();
try {
delegator.removeByPrimaryKey(prunePk);
} catch (GenericEntityException e) {
Debug.logError(
"[pruneUserHistory] Error deleting from history list.", module);
throw e;
}
} else {
Debug.logVerbose(
"[pruneUserHistory] Max Not Reached. No pruning requred.", module);
}
}
}