Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
xworker.dataObject.utils.DataObjectUtil Maven / Gradle / Ivy
/*******************************************************************************
* Copyright 2007-2013 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package xworker.dataObject.utils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import ognl.Ognl;
import org.xmeta.ActionContext;
import org.xmeta.ActionException;
import org.xmeta.Thing;
import org.xmeta.World;
import org.xmeta.util.UtilData;
import org.xmeta.util.UtilMap;
import xworker.dataObject.DataObject;
import xworker.dataObject.PageInfo;
import xworker.dataObject.query.UtilCondition;
public class DataObjectUtil {
/**
* 把Map转化为Condition事物,其中map中的childs属性是子节点列表,子节点列表类型是List<Map<String, Object>>。
*
* @param amap 集合
* @return 事物
*/
public static Thing mapToConditionThing(Map amap){
Thing condition = new Thing("xworker.dataObject.query.Condition");
condition.initDefaultValue();
condition.getAttributes().putAll(amap);
@SuppressWarnings("unchecked")
List> childs = (List>) amap.get("childs");
if(childs != null){
for(Map child : childs){
condition.addChild(mapToConditionThing(child));
}
}
return condition;
}
/**
* 根据关键字查找记录,如果存在不插入返回null,否则插入并返回插入的数据对象。
*
* @param dataObject 数据对象
* @param keyFields 关键字列表
* @param actionContext 变上下文
* @return 数据对象
*/
public static DataObject createIfNotExists(DataObject dataObject, String[] keyFields, ActionContext actionContext){
Thing condition = createConditionThing("", "");
for(String key : keyFields){
condition.addChild(createConditionThing(key, key));
}
Thing desc = dataObject.getMetadata().getDescriptor();
List datas = query(desc, condition, dataObject, actionContext);
if(datas != null && datas.size() > 0){
return null;
}else{
return dataObject.create(actionContext);
}
}
/**
* 创建一个查询配置事物。
*
* @param attrName 属性名
* @param dataName 数据名
* @param operator 操作符
* @param join 是否是join
* @return 查询配置
*/
public static Thing createConditionThing(String attrName, String dataName, byte operator, String join){
Thing condition = new Thing("xworker.dataObject.query.Condition");
condition.initDefaultValue();
condition.put("attributeName", attrName);
condition.put("dataName", dataName);
condition.put("operator", String.valueOf(operator));
condition.put("join", join);
return condition;
}
/**
* 创建一个查询配置事物,连接方式是and。
*
* @param attrName
* @param dataName
* @param operator
* @return
*/
public static Thing createConditionThing(String attrName, String dataName, byte operator){
return createConditionThing(attrName, dataName, operator, UtilCondition.AND);
}
/**
* 创建一个查询配置事物,连接方式是and,匹配方式是eq。
* @param attrName
* @param dataName
* @return
*/
public static Thing createConditionThing(String attrName, String dataName){
return createConditionThing(attrName, dataName, UtilCondition.eq, UtilCondition.AND);
}
/**
* 查询数据对象。
*
* @param dataObject
* @param conditionConfig
* @param conditionData
* @param pageInfo
* @return
*/
@SuppressWarnings("unchecked")
public static List query(Thing dataObject, Thing conditionConfig, Map conditionData, PageInfo pageInfo, ActionContext actionContext){
return (List) dataObject.doAction("query", actionContext, UtilMap.toMap(
new Object[]{"conditionConfig", conditionConfig, "conditionData", conditionData, "pageInfo", pageInfo}));
}
/**
* 查询数据对象。
*
* @param dataObject
* @param conditionConfig
* @param conditionData
* @param actionContext
* @return
*/
public static List query(Thing dataObject, Thing conditionConfig, Map conditionData, ActionContext actionContext){
return query(dataObject, conditionConfig, conditionData, null, actionContext);
}
/**
* 查询数据对象,使用Map<String, Object>生成查询条件和查询的参数值。
* 其中key为op_开头的为条件的操作类型,是UtilCondition的常量。
*
* @param dataObjectPath
* @param conditions
* @param actionContext
* @return
*/
public static List query(String dataObjectPath, Map conditions, ActionContext actionContext){
if(conditions == null){
conditions = Collections.emptyMap();
}
Thing dataObject = World.getInstance().getThing(dataObjectPath);
Thing condition = new Thing("xworker.dataObject.query.Condition");
for(String key : conditions.keySet()){
if(key.startsWith("op_")){
continue;
}
byte op = UtilData.getByte(conditions.get("op_" + key), UtilCondition.eq);
condition.addChild(createConditionThing(key, key, op));
}
return query(dataObject, condition, conditions, actionContext);
}
/**
* 批量删除数据。
*
* @param dataObjectPath
* @param conditions
* @param actionContext
* @return
*/
public static int deleteBatch(String dataObjectPath, Map conditions, ActionContext actionContext){
if(conditions == null){
conditions = Collections.emptyMap();
}
Thing dataObject = World.getInstance().getThing(dataObjectPath);
Thing condition = new Thing("xworker.dataObject.query.Condition");
for(String key : conditions.keySet()){
if(key.startsWith("op_")){
continue;
}
byte op = UtilData.getByte(conditions.get("op_" + key), UtilCondition.eq);
condition.addChild(createConditionThing(key, key, op));
}
return (Integer) dataObject.doAction("deleteBatch", actionContext, UtilMap.toMap("conditionConfig", condition, "conditionData", conditions));
}
/**
* 删除数据。
*
* @param dataObjectPath
* @param conditions
* @param actionContext
* @return
*/
public static int delete(String dataObjectPath, Map conditions, ActionContext actionContext){
if(conditions == null){
conditions = Collections.emptyMap();
}
Thing dataObject = World.getInstance().getThing(dataObjectPath);
Thing condition = new Thing("xworker.dataObject.query.Condition");
for(String key : conditions.keySet()){
if(key.startsWith("op_")){
continue;
}
byte op = UtilData.getByte(conditions.get("op_" + key), UtilCondition.eq);
condition.addChild(createConditionThing(key, key, op));
}
return (Integer) dataObject.doAction("delete", actionContext, UtilMap.toMap("conditionConfig", condition, "conditionData", conditions));
}
/**
* 使用标识读取一个数据对象。
*
* @param dataObject
* @param id
* @param actionContext
* @return
*/
public static DataObject load(Thing dataObject, Object id, ActionContext actionContext){
DataObject theData = new DataObject(dataObject);
Thing[] keys = theData.getMetadata().getKeys();
if(keys != null && keys.length > 0){
theData.put(keys[0].getString("name"), id);
return (DataObject) dataObject.doAction("load", actionContext, UtilMap.toMap(new Object[]{"theData", theData}));
}else{
throw new ActionException("DataObject has no keys defined, path=" + dataObject.getMetadata().getPath());
}
}
public static DataObject load(String dataObjectPath, Object id, ActionContext actionContext){
Thing dataObject = World.getInstance().getThing(dataObjectPath);
if(dataObject != null){
return load(dataObject, id, actionContext);
}else{
throw new ActionException("Load dataObject: thing not exists, path=" + dataObjectPath);
}
}
public static Object getValue(Object value, Thing attribute){
String type = attribute.getString("type");
if(type == null || "".equals(type)){
type = "string";
}
if("string".equals(type)){
return UtilData.getString(value, null);
}
//空的字符串当做null处理,不再使用其他默认值,2013-05-03
if(value == null || "".equals(value)){
return null;
}
if("byte".equals(type)){
return UtilData.getByte(value, (byte) 0);
}else if("char".equals(type)){
return UtilData.getChar(value, (char) 0);
}else if("short".equals(type)){
return UtilData.getShort(value, (short) 0);
}else if("int".equals(type)){
return (int) UtilData.getLong(value, 0);
}else if("long".equals(type)){
return UtilData.getLong(value, 0);
}else if("byte[]".equals(type)){
return UtilData.getBytes(value, null);
}else if("float".equals(type)){
return UtilData.getFloat(value, 0);
}else if("double".equals(type)){
return UtilData.getDouble(value, 0);
}else if("boolean".equals(type)){
return UtilData.getBoolean(value, false);
}else if("date".equals(type)){
String pattern = attribute.getString("editPattern");
if(pattern == null || "".equals(pattern)){
pattern = "yyyy-MM-dd";
}
Date date = UtilData.getDate(value, null, pattern);
if(date != null){
return new java.sql.Date(date.getTime());
}else{
return null;
}
}else if("datetime".equals(type)){
String pattern = attribute.getString("editPattern");
if(pattern == null || "".equals(pattern)){
pattern = "yyyy-MM-dd HH:mm:ss";
}
Date date = UtilData.getDate(value, null, pattern);
if(date != null){
return new java.sql.Timestamp(date.getTime());
}else{
return null;
}
}else if("time".equals(type)){
String pattern = attribute.getString("editPattern");
if(pattern == null || "".equals(pattern)){
pattern = "HH:mm:ss";
}
Date date = UtilData.getDate(value, null, pattern);
if(date != null){
return new java.sql.Time(date.getTime());
}else{
return null;
}
}else{
return UtilData.getString(value, null);
//throw new XWorkerException("not implemented type " + type);
}
}
/**
* 根据属性的类型定义返回具体类型的值。
*
* @param value
* @param type
* @return
*/
public static Object getAttributeValue(Object value, String type){
if(type == null || "".equals(type)){
type = "string";
}
if("string".equals(type)){
return UtilData.getString(value, null);
}else if("byte".equals(type)){
return UtilData.getByte(value, (byte) 0);
}else if("char".equals(type)){
return UtilData.getChar(value, (char) 0);
}else if("short".equals(type)){
return UtilData.getShort(value, (short) 0);
}else if("int".equals(type)){
return (int) UtilData.getLong(value, 0);
}else if("long".equals(type)){
return UtilData.getLong(value, 0);
}else if("byte[]".equals(type)){
return UtilData.getBytes(value, null);
}else if("float".equals(type)){
return UtilData.getFloat(value, 0);
}else if("double".equals(type)){
return UtilData.getDouble(value, 0);
}else if("boolean".equals(type)){
return UtilData.getBoolean(value, false);
}else if("date".equals(type)){
Date date = UtilData.getDate(value, null);
if(date != null){
return new java.sql.Date(date.getTime());
}else{
return null;
}
}else if("datetime".equals(type)){
Date date = UtilData.getDate(value, null);
if(date != null){
return new java.sql.Timestamp(date.getTime());
}else{
return null;
}
}else if("time".equals(type)){
Date date = UtilData.getDate(value, null);
if(date != null){
return new java.sql.Time(date.getTime());
}else{
return null;
}
}else{
return UtilData.getString(value, null);
//throw new XWorkerException("not implemented type " + type);
}
}
public static Object getDefaultValue(String type, String defaultValue, ActionContext actionContext){
if(defaultValue == null){
return null;
}
if("date".equals(type) || "datetime".equals(type) || "time".equals(type)){
//日期的默认值
defaultValue = defaultValue.toLowerCase();
String dateStr = defaultValue;
String numberStr = "";
int index = defaultValue.indexOf("+");
if(index != -1){
dateStr = defaultValue.substring(0, index).trim();
numberStr = defaultValue.substring(index, defaultValue.length()).trim();
}else{
index = defaultValue.indexOf("-");
if(index != -1){
dateStr = defaultValue.substring(0, index).trim();
numberStr = defaultValue.substring(index, defaultValue.length()).trim();
}
}
Date date = null;
if("now".equals(dateStr) || "sysdate".equals(dateStr)){
date = new Date();
}else if("tomorrow".equals(dateStr)){
date = UtilDate.getTomorrow();
}else if("yesterday".equals(dateStr)){
date = UtilDate.getYesterday();
}else if("weekstart".equals(dateStr)){
date = UtilDate.getWeekStart();
}else if("weekend".equals(dateStr)){
date = UtilDate.getWeekEnd();
}else if("monthstart".equals(dateStr)){
date = UtilDate.getMonthStart();
}else if("monthend".equals(dateStr)){
date = UtilDate.getMonthEnd();
}else if("yearstart".equals(dateStr)){
date = UtilDate.getYearStart();
}else if("yearend".equals(dateStr)){
date = UtilDate.getYearEnd();
}else{
date = new Date();
try{
double d = (Double) Ognl.getValue(dateStr, actionContext);
date = UtilDate.getDate(date, d);
}catch(Exception e){
}
}
if(numberStr != null && numberStr != ""){
try{
double d = (Double) Ognl.getValue(numberStr, actionContext);
//log.info("d=" + d);
date = UtilDate.getDate(date, d);
}catch(Exception e){
//log.info("error", e);
}
}
return date;
}else{
return defaultValue;
}
}
/**
* 数据库结果集到数据对象的转换。
*
* @param rs
* @param dataObject
* @return
* @throws SQLException
*/
public static List dbResultsToDataObjects(Thing dataObject, ResultSet rs) throws SQLException {
List ds = new ArrayList();
List attributes = dataObject.getChilds("attribute");
while (rs.next()) {
// 构造对象
DataObject data = new DataObject(dataObject);
data.setInited(false);
// 设置属性值
for (int i = 0; i < attributes.size(); i++) {
data.put(attributes.get(i).getString("name"), DbUtil.getValue(rs, attributes.get(i)));
}
data.setInited(true);
ds.add(data);
// log.info("data=" + data);
}
return ds;
}
}