/*
*
* 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.event;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilTimer;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericPK;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityComparisonOperator;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelField;
import org.ofbiz.entity.model.ModelFieldType;
import org.ofbiz.entity.model.ModelFieldTypeReader;
import org.ofbiz.entity.model.ModelKeyMap;
import org.ofbiz.entity.model.ModelReader;
import org.ofbiz.entity.model.ModelRelation;
import com.sourcetap.sfa.ui.UIScreenSection;
import com.sourcetap.sfa.util.QueryInfo;
/**
* DOCUMENT ME!
*
*/
public class EventUtility {
public static final int STATUS_ERROR = -1;
public static final int STATUS_CANCEL = 0;
public static final int STATUS_CONTINUE = 1;
private static final boolean TIMER = false;
public static final String module = EventUtility.class.getName();
public EventUtility() {
}
/**
* DOCUMENT ME!
*
* @param gV
* @param fieldName
* @param delegator
*
* @return
*/
public static String getDataType(GenericValue gV, String fieldName,
GenericDelegator delegator) {
UtilTimer timer = new UtilTimer();
if (TIMER) {
timer.timerString("[EventUtility.getDataType] Start");
}
ModelEntity modelEntity = delegator.getModelEntity(gV.getEntityName());
ModelField curField = modelEntity.getField(fieldName);
ModelFieldTypeReader modelFieldTypeReader = new ModelFieldTypeReader(
"mysql");
ModelFieldType mft = modelFieldTypeReader.getModelFieldType(curField.getType());
String fieldType = mft.getJavaType();
if (TIMER) {
timer.timerString("[EventUtility.getDataType] End");
}
return fieldType;
}
/**
* DOCUMENT ME!
*
* @param gV
* @param fieldName
* @param value
* @param delegator
*/
public static void storeValue(GenericValue gV, String fieldName,
String value, GenericDelegator delegator) {
String fieldType = getDataType(gV, fieldName, delegator);
storeValue(gV, fieldName, value, delegator, fieldType);
return;
}
/**
* DOCUMENT ME!
*
* @param gV
* @param fieldName
* @param value
* @param delegator
* @param fieldType
*/
public static void storeValue(GenericValue gV, String fieldName,
String value, GenericDelegator delegator, String fieldType) {
UtilTimer timer = new UtilTimer();
if (TIMER) {
timer.timerString("[EventUtility.storeValue] Start");
}
if (fieldType.equals("java.lang.String") || fieldType.equals("String")) {
gV.set(fieldName, value);
} else if (fieldType.equals("java.sql.Timestamp") || fieldType.equals("Timestamp")) {
if ((value == null) || (value.trim().length() == 0)) {
gV.set(fieldName, null);
} else {
String[] parseMask = {
"y-M-d", "M/d/y", "M-d-y", "M d, y", "H:m:s.S",
"y-M-d H:m:s.S", "M/d/y H:m:s.S", "M-d-y H:m:s.S",
"M d, y H:m:s.S", "h:m:s a", "y-M-d h:m:s a",
"M/d/y h:m:s a", "M-d-y h:m:s a", "M d, y h:m:s a", "H:m:s",
"y-M-d H:m:s", "M/d/y H:m:s", "M-d-y H:m:s", "M d, y H:m:s",
"h:m a", "y-M-d h:m a", "M/d/y h:m a", "M-d-y h:m a",
"M d, y h:m a", "H:m", "y-M-d H:m", "M/d/y H:m", "M-d-y H:m",
"M d, y H:m"
};
java.util.Date parsedDate = null;
boolean isParsed = false;
String currentMask = "";
for (int maskNbr = 0; maskNbr < parseMask.length; maskNbr++) {
currentMask = parseMask[maskNbr];
SimpleDateFormat dateFormat = new SimpleDateFormat(currentMask);
try {
parsedDate = dateFormat.parse(value);
isParsed = true;
break;
} catch (ParseException e) {
// Try the next mask.
}
}
if (isParsed) {
Timestamp parsedTimestamp = new Timestamp(parsedDate.getTime());
gV.set(fieldName, parsedTimestamp);
} else {
Debug.logWarning(
"[EventUtility.storeValue()]: Could not parse a Date or Datetime from " +
value, module);
}
}
} else if (fieldType.equals("java.sql.Time") || fieldType.equals("Time")) {
if (( value == null ) || (value.trim().length() == 0)) {
gV.set(fieldName, null);
} else {
String[] parseMask = {
"H:m:s.S", "H:m:s", "H:m", "h:m a", "h:m:s a"
};
java.util.Date parsedDate = null;
boolean isParsed = false;
String currentMask = "";
for (int maskNbr = 0; maskNbr < parseMask.length; maskNbr++) {
currentMask = parseMask[maskNbr];
SimpleDateFormat dateFormat = new SimpleDateFormat(currentMask);
try {
parsedDate = dateFormat.parse(value);
isParsed = true;
break;
} catch (ParseException e) {
// Try the next mask.
}
}
if (isParsed) {
// gV.set(fieldName, new java.sql.Time(parsedDate.getTime()));
gV.set(fieldName,
new java.sql.Timestamp(parsedDate.getTime()));
} else {
Debug.logWarning(
"[EventUtility.storeValue()]: Could not parse a time from " +
value, module);
}
}
} else if (fieldType.equals("java.util.Date") || fieldType.equals("java.sql.Date") || fieldType.equals("Date")) {
if ((value == null) || (value.trim().length() == 0)) {
gV.set(fieldName, null);
} else {
String[] parseMask = { "y-M-d", "M/d/y", "M-d-y", "M d, y" };
java.util.Date parsedDate = null;
boolean isParsed = false;
String currentMask = "";
for (int maskNbr = 0; maskNbr < parseMask.length; maskNbr++) {
currentMask = parseMask[maskNbr];
SimpleDateFormat dateFormat = new SimpleDateFormat(currentMask);
try {
parsedDate = dateFormat.parse(value);
isParsed = true;
break;
} catch (ParseException e) {
// Try the next mask.
}
}
if (isParsed) {
gV.set(fieldName, new java.sql.Date(parsedDate.getTime()));
} else {
Debug.logWarning(
"[EventUtility.storeValue()]: Could not parse a date from " +
value, module);
}
}
} else if (fieldType.equals("java.lang.Integer") ||
fieldType.equals("Integer") ||
fieldType.equals("java.lang.Long") || fieldType.equals("Long") ||
fieldType.equals("java.lang.Float") ||
fieldType.equals("Float") ||
fieldType.equals("java.lang.Double") ||
fieldType.equals("Double")) {
if ((value == null) || (value.trim().length() == 0)) {
value = "0";
}
// gV.set(fieldName, String.valueOf(parseNumber(value)));
gV.set(fieldName, parseNumber(value));
}
if (TIMER) {
timer.timerString("[EventUtility.storeValue] End.");
}
return;
}
//-------------------------------------------------------------------------
// Put values from the screen into the Generic Value Vector List
//-------------------------------------------------------------------------
public static Number parseNumber(String value) {
DecimalFormat decimalFormat = new DecimalFormat();
StringBuffer valueBuffer = new StringBuffer(value);
for (int i = 0; i < valueBuffer.length(); i++) {
if (valueBuffer.toString().charAt(i) == '$') {
valueBuffer.deleteCharAt(i--);
}
}
try {
Number n = decimalFormat.parse(valueBuffer.toString());
return n;
} catch (ParseException e) {
Debug.logWarning(
"[EventUtility.parseNumber()]: Could not parse a number from " +
valueBuffer.toString(), module);
return null;
}
}
/**
* DOCUMENT ME!
*
* @param delegator
* @param uiScreenSection
* @param nameToSearchParam
*
* @return
*
* @throws GenericEntityException
*/
public static HashMap getAlphaSearchValues(GenericDelegator delegator,
UIScreenSection uiScreenSection, String nameToSearchParam)
throws GenericEntityException {
HashMap returnValues = new HashMap();
try {
// Alpha search. Use the alpha search attribute specified in the screen section.
String searchAttributeId = uiScreenSection.getSearchAttributeId();
ModelEntity uiAttributeEntity = delegator.getModelEntity(
"UiAttribute");
HashMap uiAttributeFindMap = new HashMap();
uiAttributeFindMap.put("attributeId", searchAttributeId);
GenericPK uiAttributePk = new GenericPK(uiAttributeEntity,
uiAttributeFindMap);
GenericValue uiAttributeGenericValue = delegator.findByPrimaryKeyCache(uiAttributePk);
if (uiAttributeGenericValue == null) {
throw new GenericEntityException(
"No Ui Attribute was found for ui_attribute.attribute_id=" +
searchAttributeId);
}
String searchAttribName = uiAttributeGenericValue.getString(
"attributeName");
String searchEntityId = uiAttributeGenericValue.getString(
"entityId");
ModelEntity uiEntityEntity = delegator.getModelEntity("UiEntity");
HashMap uiEntityFindMap = new HashMap();
uiEntityFindMap.put("entityId", searchEntityId);
GenericPK uiEntityPk = new GenericPK(uiEntityEntity, uiEntityFindMap);
GenericValue uiEntityGenericValue = delegator.findByPrimaryKeyCache(uiEntityPk);
String searchEntityName = uiEntityGenericValue.getString(
"entityName");
String searchAttribValue = nameToSearchParam.replace('*', '%') +
"%";
returnValues.put("searchEntityName", searchEntityName);
returnValues.put("searchAttribName", searchAttribName);
returnValues.put("searchAttribValue", searchAttribValue);
} catch (Exception e) {
Debug.logError(e.getLocalizedMessage(), module);
e.printStackTrace();
}
return returnValues;
}
/**
* DOCUMENT ME!
*
* @param queryValue
*
* @return
*/
public static EntityComparisonOperator getEntityOperator(StringBuffer queryValue) {
EntityComparisonOperator operator = null;
String queryValueString = queryValue.toString();
long queryDateOffset = 0;
if (queryValueString.indexOf(">=") >= 0) {
operator = EntityOperator.GREATER_THAN_EQUAL_TO;
} else if (queryValueString.indexOf("<=") >= 0) {
operator = EntityOperator.LESS_THAN_EQUAL_TO;
queryDateOffset = 24 * 60 * 60 * 1000; // Use tomorrow's date to say less than or equal to today.
} else if ((queryValueString.indexOf("<>") >= 0) ||
(queryValueString.indexOf("!=") >= 0)) {
operator = EntityOperator.NOT_EQUAL;
} else if (queryValueString.indexOf(">") >= 0) {
operator = EntityOperator.GREATER_THAN;
queryDateOffset = 24 * 60 * 60 * 1000; // Use tomorrow's date to say greater than today.
} else if (queryValueString.indexOf("<") >= 0) {
operator = EntityOperator.LESS_THAN;
} else if (queryValueString.indexOf("=") >= 0) {
operator = EntityOperator.EQUALS;
} else {
operator = EntityOperator.LIKE;
}
// Do date logic if it is embedded in the value string.
convertDateQueryValue(queryValue, queryDateOffset);
// Remove all the special characters
for (int i = 0; i < queryValue.length(); i++) {
if ((queryValue.toString().charAt(i) == '>') ||
(queryValue.toString().charAt(i) == '<') ||
(queryValue.toString().charAt(i) == '=') ||
(queryValue.toString().charAt(i) == '!')) {
queryValue.deleteCharAt(i--);
}
}
return operator;
}
/**
* DOCUMENT ME!
*
* @param queryValue
* @param queryDateOffset
*
* @return
*/
public static StringBuffer convertDateQueryValue(StringBuffer queryValue,
long queryDateOffset) {
int start = queryValue.toString().toUpperCase().indexOf("TODAY");
if (start >= 0) {
// Delete the word "today"
queryValue.delete(start, start + 5);
int plusOperandPos = queryValue.toString().indexOf("+");
int minusOperandPos = queryValue.toString().indexOf("-");
long offsetMilliseconds = 0;
if (plusOperandPos >= 0) {
// Need to subtract days. Get the number of days, which should be after the operand.
String offsetDaysString = queryValue.toString().substring(plusOperandPos +
1);
offsetMilliseconds = Integer.valueOf(offsetDaysString).intValue() * 24 * 60 * 60 * 1000;
queryValue.delete(plusOperandPos,
plusOperandPos + offsetDaysString.length() + 1);
}
if (minusOperandPos >= 0) {
// Need to subtract days. Get the number of days, which should be after the operand.
String offsetDaysString = queryValue.toString().substring(minusOperandPos +
1);
offsetMilliseconds = Integer.valueOf(offsetDaysString).intValue() * -24 * 60 * 60 * 1000;
queryValue.delete(minusOperandPos,
minusOperandPos + offsetDaysString.length() + 1);
}
Date today = new Date(Calendar.getInstance().getTime().getTime() +
queryDateOffset + offsetMilliseconds);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String todayString = dateFormat.format(today);
queryValue.insert(start, todayString);
}
/*
// Remove all the special characters
for (int i = 0; i < queryValue.length(); i++) {
if ( queryValue.toString().charAt(i) == '>' ||
queryValue.toString().charAt(i) == '<' ||
queryValue.toString().charAt(i) == '=' ||
queryValue.toString().charAt(i) == '!') {
queryValue.deleteCharAt(i--);
}
}
*/
return queryValue;
}
/**
* DOCUMENT ME!
*
* @param searchEntityName
* @param searchAttribName
* @param searchAttribValue
* @param queryInfo
*
* @return
*/
public static EntityComparisonOperator appendEntityClause(String searchEntityName,
String searchAttribName, String searchAttribValue, QueryInfo queryInfo) {
EntityComparisonOperator entityOperator = appendEntityClause(searchEntityName,
searchAttribName, searchAttribValue, null, queryInfo);
return entityOperator;
}
/**
* DOCUMENT ME!
*
* @param searchEntityName
* @param searchAttribName
* @param searchAttribValue
* @param entityOperator
* @param queryInfo
*
* @return
*/
public static EntityComparisonOperator appendEntityClause(String searchEntityName,
String searchAttribName, Collection searchAttribValue,
EntityComparisonOperator entityOperator, QueryInfo queryInfo)
{
queryInfo.addCondition(searchEntityName, searchAttribName, entityOperator, searchAttribValue );
return entityOperator;
}
/**
* DOCUMENT ME!
*
* @param searchEntityName
* @param searchAttribName
* @param searchAttribValue
* @param entityOperator
* @param queryInfo
*
* @return
*/
public static EntityComparisonOperator appendEntityClause(String searchEntityName,
String searchAttribName, String searchAttribValue,
EntityComparisonOperator entityOperator, QueryInfo queryInfo) {
// Find out the operator to use, and strip out the special characters.
StringBuffer queryValueBuffer = new StringBuffer(searchAttribValue);
if (entityOperator == null) {
// No entity operator was supplied. Look for operator in the query value string, and use it if found.
// If not found, use LIKE.
entityOperator = EventUtility.getEntityOperator(queryValueBuffer);
}
searchAttribValue = queryValueBuffer.toString();
if ( entityOperator.equals(EntityOperator.LIKE))
searchAttribValue = searchAttribValue + "%";
queryInfo.addCondition(searchEntityName, searchAttribName, entityOperator, searchAttribValue.replace('*', '%') );
return entityOperator;
}
/**
* DOCUMENT ME!
*
* @param searchEntityName
* @param paramList
* @param queryInfo criteria to be used in search
*/
public static void appendEntityClauses(String searchEntityName,
HashMap paramList, QueryInfo queryInfo) {
Iterator params = paramList.entrySet().iterator();
while (params.hasNext()) {
Map.Entry param = (Map.Entry) params.next();
String searchAttribName = (String) param.getKey();
String searchAttribValue = (String) param.getValue();
queryInfo.addCondition(searchEntityName, searchAttribName, EntityOperator.EQUALS, searchAttribValue );
}
return;
}
/**
* This function returns an arraylist containing HashMaps representing the primary keys of records
* in a many-to-many relationship which are to be added to or removed from te data base after
* being selected or unselected on a SELECT screen section.
* @author John Nutting
* version 1
* @param queryParameterValueList A hash map containing the values being used to filter the screen section.\
* Example: key=dealId, value=10010
* @param itemString String representation of the list of keys to be added or removed.
* Example representing 3 entities, each with 2 fields in the key:
* "displayObjectId:10021,displayTypeId:22320,displayAttribId:SIZE;displayObjectId:10021,displayTypeId:22320,displayAttribId:DISABLED"
* @return An ArrayList of HashMaps, each containing the primary key of a generic value to be added to or removed from the data base.
*/
public static ArrayList decodeSelectItem(HashMap queryParameterValueList,
String itemString) {
ArrayList manyToManyKeyMapList = new ArrayList();
if (itemString != null) {
StringTokenizer semicolonTokenizer = new StringTokenizer(itemString,
";");
while (semicolonTokenizer.hasMoreTokens()) {
// User selected or un-selected this item. Need to add it to the many-to-many map list.
HashMap manyToManyKepMap = new HashMap();
// Add the fields that are being used to filter the screen section. These will be
// one component of the primary key of the many-to-many table. Example: dealId:10010.
manyToManyKepMap.putAll(queryParameterValueList);
// Add the key(s) of the item. Example: "displayObjectId:10021,displayTypeId:22320"
String itemKeyDef = semicolonTokenizer.nextToken();
StringTokenizer commaTokenizer = new StringTokenizer(itemKeyDef,
",");
while (commaTokenizer.hasMoreTokens()) {
// One more field in the key of the removed item was found.
String itemKeyField = commaTokenizer.nextToken();
String itemKeyAttribName = "";
String itemKeyAttribValue = "";
StringTokenizer colonTokenizer = new StringTokenizer(itemKeyField,
":");
if (colonTokenizer.hasMoreTokens()) {
// The name of the key attribute was found.
itemKeyAttribName = colonTokenizer.nextToken();
} else {
// Error. The name of the key attribute was not found in the string.
Debug.logWarning(
"[EventUtility.decodeSelectItem] Key attribute name was not found in " +
itemKeyField, module);
return manyToManyKeyMapList;
}
if (colonTokenizer.hasMoreTokens()) {
// The value of the key attribute was found.
itemKeyAttribValue = colonTokenizer.nextToken();
} else {
// Error. The value of the key attribute was not found in the string.
Debug.logWarning(
"[EventUtility.decodeSelectItem] Key attribute value was not found in " +
itemKeyField, module);
return manyToManyKeyMapList;
}
manyToManyKepMap.put(itemKeyAttribName, itemKeyAttribValue);
}
manyToManyKeyMapList.add(manyToManyKepMap);
}
}
return manyToManyKeyMapList;
}
/**
* Add entity clauses for one related entity. This will join related tables to the query
* during a retrieve so query values can be entered that are in related entities.
*
* @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
*
* @param delegator Reference to the OFBIZ delegator being used to connect to the data base
* @param relationTitle Relation title
* @param relatedEntityName Name of related entity
* @param primaryEntityName Name of the primary entity
* @param primaryME ModelEntity object for the primary entity
* @param queryInfo criteria to be used in search
*/
public static void addOneRelationClause(GenericDelegator delegator,
String relationTitle, String relatedAndFields, String relatedEntityName,
String primaryEntityName, ModelEntity primaryME, boolean isOuterJoin, QueryInfo queryInfo)
throws GenericEntityException
{
String relationName = relationTitle + relatedEntityName;
ModelRelation relation = primaryME.getRelation(relationName);
if (relation == null) {
Debug.logWarning("Could not find relation for on entity " +
primaryME.getEntityName() + " for relationName " +
relationName, module);
} else {
// Relation was found. Add it to the where and from clauses.
ModelReader modelReader = delegator.getModelReader();
ModelEntity relatedME = modelReader.getModelEntity(relation.getRelEntityName());
Iterator keyMapsIterator = relation.getKeyMapsIterator();
List keyMaps = new ArrayList();
while (keyMapsIterator.hasNext()) {
ModelKeyMap keyMap = (ModelKeyMap) keyMapsIterator.next();
Debug.logVerbose("Adding to entityClauses: " +
primaryEntityName + ", " + relatedEntityName + ", " +
keyMap.getFieldName() + ", " +
keyMap.getRelFieldName(), module);
keyMaps.add(keyMap);
}
queryInfo.addJoin( primaryEntityName, relatedEntityName, Boolean.valueOf(isOuterJoin), keyMaps);
}
}
}