📄 eventutility.java
字号:
/*
*
* 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('*', '%') +
"%";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -