com.jfinal.plugin.activerecord.Model Java Examples

The following examples show how to use com.jfinal.plugin.activerecord.Model. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonUtils.java    From my_curd with Apache License 2.0 6 votes vote down vote up
/**
 * 把model转为map,驼峰命名
 *
 * @param model
 * @return
 */
public static Map<String, Object> modelToCamelCaseMap(Model model) {
    if (null == model) {
        return null;
    }
    String[] keys = model._getAttrNames();
    Map<String, Object> map = new HashMap<>();
    for (String key : keys) {
        Object value = model.get(key);
        key = StrKit.toCamelCase(key.toLowerCase());
        if (null != value) {
            map.put(key, value);
        }
    }
    return map;
}
 
Example #2
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 6 votes vote down vote up
/**
 * 添加关联数据到某个model中去,避免关联查询,提高性能。
 *
 * @param model
 * @param joinOnField
 * @param joinName
 * @param attrs
 */
@Override
public <M extends Model> M join(M model, String joinOnField, String joinName, String[] attrs) {
    if (model == null)
        return model;
    Object id = model.get(joinOnField);
    if (id == null) {
        return model;
    }
    JbootModel m = joinById(id);
    if (m != null) {
        m = m.copy();
        m.keep(attrs);
        model.put(joinName, m);
    }
    return model;
}
 
Example #3
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 6 votes vote down vote up
/**
 * 添加关联数据到某个model中去,避免关联查询,提高性能。
 *
 * @param model
 * @param joinOnField
 * @param attrs
 */
@Override
public <M extends Model> M join(M model, String joinOnField, String[] attrs) {
    if (model == null)
        return model;
    Object id = model.get(joinOnField);
    if (id == null) {
        return model;
    }
    JbootModel m = joinById(id);
    if (m != null) {
        m = m.copy();
        m.keep(attrs);
        model.put(StrKit.firstCharToLowerCase(m.getClass().getSimpleName()), m);
    }
    return model;
}
 
Example #4
Source File: JPressJson.java    From jpress with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 优化 map 的属性
 *
 * @param map
 */
@Override
protected void optimizeMapAttrs(Model model, Map map) {
    if (map == null || map.isEmpty()) {
        return;
    }

    Iterator iter = map.entrySet().iterator();
    while (iter.hasNext()) {

        Map.Entry entry = (Map.Entry) iter.next();

        // 给图片类的属性,添加域名成为绝对路径,
        // 相对路径放到app上去麻烦
        // 这个行为会改变model的值(涉及到改变缓存的值)
        if (StrUtil.isNotBlank(resDomain) && needAddDomainAttrs.contains(entry.getKey())) {
            String value = (String) entry.getValue();
            if (StrUtil.isNotBlank(value) && !value.startsWith("http")) {
                entry.setValue(resDomain + value);
            }
        }

    }
}
 
Example #5
Source File: UserOpenidServiceProvider.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void shouldUpdateCache(int action, Model model, Object id) {
   if (model instanceof UserOpenid){
       UserOpenid userOpenid = (UserOpenid) model;
       Jboot.getCache().remove("useropenid",userOpenid.getType()+"-"+userOpenid.getValue());
   }
}
 
Example #6
Source File: JsonUtils.java    From jfinal-weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 将Collection<Model>转换为json字符串
 * @param models
 * @return JsonString
 */
public static String toJson(Collection<Model<? extends Model<?>>> models) {
	List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
	for (Model<? extends Model<?>> model : models) {
		list.add(CPI.getAttrs(model));
	}
	return toJson(list);
}
 
Example #7
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public <M extends Model> List<M> join(List<M> models, String joinOnField, String joinName, String[] attrs) {
    if (ArrayUtil.isNotEmpty(models)) {
        for (Model m : models) {
            join(m, joinOnField, joinName, attrs);
        }
    }
    return models;
}
 
Example #8
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 5 votes vote down vote up
/**
 * 添加关联数据到某个model中去,避免关联查询,提高性能。
 *
 * @param model
 * @param joinOnField
 * @param joinName
 */
@Override
public <M extends Model> M join(M model, String joinOnField, String joinName) {
    if (model == null)
        return model;
    Object id = model.get(joinOnField);
    if (id == null) {
        return model;
    }
    Model m = joinById(id);
    if (m != null) {
        model.put(joinName, m);
    }
    return model;
}
 
Example #9
Source File: ModelExt.java    From jfinal-ext3 with Apache License 2.0 5 votes vote down vote up
/**
 * check current instance is equal obj or not.[wrapper equal]
 * @param obj
 * @return true:equal, false:not equal.
 */
public boolean eq(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (this._getUsefulClass() != obj.getClass())
           return false;
       
       Model<?> other = (Model<?>) obj;
       Table tableinfo = this.table();
       Set<Entry<String, Object>> attrsEntrySet = this._getAttrsEntrySet();
       for (Entry<String, Object> entry : attrsEntrySet) {
           String key = entry.getKey();
           Object value = entry.getValue();
           Class<?> clazz = tableinfo.getColumnType(key);
           if (clazz == Float.class) {
           } else if (clazz == Double.class) {
           } else if (clazz == Model.class) {
           } else {
               if (value == null) {
                   if (other.get(key) != null)
                       return false;
               } else if (!value.equals(other.get(key)))
                   return false;
           }
       }
       return true;		
}
 
Example #10
Source File: TableInfoManager.java    From jboot with Apache License 2.0 5 votes vote down vote up
private void addTable(List<TableInfo> tableInfoList, Class<Model> clazz, Table tb) {
    TableInfo tableInfo = new TableInfo();
    tableInfo.setModelClass(clazz);
    tableInfo.setPrimaryKey(AnnotationUtil.get(tb.primaryKey()));
    tableInfo.setTableName(AnnotationUtil.get(tb.tableName()));

    tableInfoList.add(tableInfo);
}
 
Example #11
Source File: JsoupUtils.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void clean(Model model, String... attrs) {
    if (attrs != null && attrs.length == 0) {
        return;
    }

    for (String attr : attrs) {
        Object data = model.get(attr);
        if (data == null || !(data instanceof String)) {
            continue;
        }

        model.set(attr, clean((String) data));
    }
}
 
Example #12
Source File: BaseController.java    From my_curd with Apache License 2.0 5 votes vote down vote up
/**
 * 接收 bean list
 *
 * @param modelClass
 * @param prefix
 * @param <T>
 * @return
 */
@SuppressWarnings("unchecked")
protected <T> List<T> getBeans(Class<? extends Model> modelClass, String prefix) {
    List<T> beanList = new ArrayList<>();
    int size = getArrayKeys(prefix).size();
    for (int i = 0; i < size; i++) {
        beanList.add((T) Injector.injectBean(modelClass, prefix + "[" + i + "]", getRequest(), false));
    }
    return beanList;
}
 
Example #13
Source File: RecordKit.java    From jfinal-ext3 with Apache License 2.0 5 votes vote down vote up
public static Model<?> toModel(Class<? extends Model<?>> clazz, Record record) {
    Model<?> model = null;
    try {
        model = clazz.newInstance();
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        return model;
    }
    model.put(record.getColumns());
    return model;
}
 
Example #14
Source File: ArticleCategoryServiceProvider.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
@CachesEvict({
        @CacheEvict(name = "articleCategory", key = "*"),
        @CacheEvict(name = "article-category", key = "*"),
})
public void shouldUpdateCache(int action, Model model, Object id) {
    super.shouldUpdateCache(action, model, id);
}
 
Example #15
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 5 votes vote down vote up
/**
 * 添加关联数据到某个model中去,避免关联查询,提高性能。
 *
 * @param model       要添加到的model
 * @param joinOnField model对于的关联字段
 */
@Override
public <M extends Model> M join(M model, String joinOnField) {
    if (model == null)
        return model;
    Object id = model.get(joinOnField);
    if (id == null) {
        return model;
    }
    Model m = joinById(id);
    if (m != null) {
        model.put(StrKit.firstCharToLowerCase(m.getClass().getSimpleName()), m);
    }
    return model;
}
 
Example #16
Source File: ControllerBase.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected boolean isLoginedUserModel(Model model, String attrName) {
    if (model == null) {
        return false;
    }
    Object userId = model.get(attrName);
    if (userId == null) {
        return false;
    }
    User loginedUser = getLoginedUser();
    if (loginedUser == null) {
        return false;
    }
    return userId.equals(loginedUser.getId());
}
 
Example #17
Source File: AddonManager.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void startActiveRecordPlugin(AddonInfo addonInfo) {
    List<Class<? extends JbootModel>> modelClasses = addonInfo.getModels();
    if (modelClasses != null && !modelClasses.isEmpty()) {

        ActiveRecordPlugin arp = addonInfo.getOrCreateArp();

        List<com.jfinal.plugin.activerecord.Table> tableList = getTableList(arp);

        for (Class<? extends JbootModel> c : modelClasses) {

            Table tableAnnotation = c.getAnnotation(Table.class);
            boolean needAddMapping = true;

            if (tableList != null && !tableList.isEmpty()) {
                for (com.jfinal.plugin.activerecord.Table t : tableList) {
                    if (t.getName().equals(AnnotationUtil.get(tableAnnotation.tableName()))) {
                        needAddMapping = false;
                        break;
                    }
                }
            }

            if (needAddMapping) {
                if (StrUtil.isNotBlank(tableAnnotation.primaryKey())) {
                    arp.addMapping(AnnotationUtil.get(tableAnnotation.tableName()), AnnotationUtil.get(tableAnnotation.primaryKey()), (Class<? extends Model<?>>) c);
                } else {
                    arp.addMapping(AnnotationUtil.get(tableAnnotation.tableName()), (Class<? extends Model<?>>) c);
                }
            }
        }
        addonInfo.setArp(arp);
        arp.start();
    }
}
 
Example #18
Source File: JsonUtils.java    From my_curd with Apache License 2.0 5 votes vote down vote up
/**
 * Page<Page<? extends Model>转为Page<Map<String, Object>>,驼峰命名
 *
 * @param models
 * @return
 */
public static Page<Map<String, Object>> modelsToCamelCaseMaps(Page<? extends Model> models) {
    List<? extends Model> modelList = models.getList();
    List<Map<String, Object>> maps = new ArrayList<>();
    for (Model model : modelList) {
        maps.add(modelToCamelCaseMap(model));
    }
    return new Page<>(maps, models.getPageNumber(), models.getPageSize(), models.getTotalPage(),
            models.getTotalRow());
}
 
Example #19
Source File: ModelExt.java    From jfinal-ext3 with Apache License 2.0 5 votes vote down vote up
/**
 * wrapper hash code
 */
public int hcode() {
	final int prime = 31;
	int result = 1;
	Table table = this.table();
	Set<Entry<String, Object>> attrsEntrySet = this._getAttrsEntrySet();
	for (Entry<String, Object> entry : attrsEntrySet) {
		String key = entry.getKey();
		Object value = entry.getValue();
		Class<?> clazz = table.getColumnType(key);
		if (clazz == Integer.class) {
			result = prime * result + (Integer) value;
		} else if (clazz == Short.class) {
			result = prime * result + (Short) value;
		} else if (clazz == Long.class) {
			result = prime * result + (int) ((Long) value ^ ((Long) value >>> 32));
		} else if (clazz == Float.class) {
			result = prime * result + Float.floatToIntBits((Float) value);
		} else if (clazz == Double.class) {
			long temp = Double.doubleToLongBits((Double) value);
			result = prime * result + (int) (temp ^ (temp >>> 32));
		} else if (clazz == Boolean.class) {
			result = prime * result + ((Boolean) value ? 1231 : 1237);
		} else if (clazz == Model.class) {
			result = this.hcode();
		} else {
			result = prime * result + ((value == null) ? 0 : value.hashCode());
		}
	}
	return result;
}
 
Example #20
Source File: JsonUtils.java    From my_curd with Apache License 2.0 5 votes vote down vote up
/**
 * List<? extends Model>转为List<Map<String, Object>>,驼峰命名
 *
 * @param models
 * @return
 */
public static List<Map<String, Object>> modelsToCamelCaseMaps(List<? extends Model> models) {
    List<Map<String, Object>> maps = new ArrayList<>();
    for (Model model : models) {
        maps.add(modelToCamelCaseMap(model));
    }
    return maps;
}
 
Example #21
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 4 votes vote down vote up
@Override
public <M extends Model> Page<M> join(Page<M> page, String joinOnField, String[] attrs) {
    join(page.getList(), joinOnField, attrs);
    return page;
}
 
Example #22
Source File: ControllerBase.java    From jpress with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected boolean notLoginedUserModel(Model model, String attrName) {
    return !isLoginedUserModel(model, attrName);
}
 
Example #23
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 4 votes vote down vote up
@Override
public <M extends Model> Page<M> join(Page<M> page, String joinOnField) {
    join(page.getList(), joinOnField);
    return page;
}
 
Example #24
Source File: TableInfo.java    From jboot with Apache License 2.0 4 votes vote down vote up
public Class<? extends Model> getModelClass() {
    return modelClass;
}
 
Example #25
Source File: ControllerBase.java    From jpress with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected boolean notLoginedUserModel(Model model) {
    return !isLoginedUserModel(model, "user_id");
}
 
Example #26
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 4 votes vote down vote up
@Override
public <M extends Model> Page<M> join(Page<M> page, String joinOnField, String joinName) {
    join(page.getList(), joinOnField, joinName);
    return page;
}
 
Example #27
Source File: ResService.java    From jboot-admin with Apache License 2.0 2 votes vote down vote up
/**
 * 添加关联数据到某个model中去,避免关联查询,提高性能。
 *
 * @param model       要添加到的model
 * @param joinOnField model对于的关联字段
 */
public void join(Model model, String joinOnField);
 
Example #28
Source File: DataService.java    From jboot-admin with Apache License 2.0 2 votes vote down vote up
/**
 * 添加关联数据到某个model中去,避免关联查询,提高性能。
 *
 * @param model       要添加到的model
 * @param joinOnField model对于的关联字段
 */
public void join(Model model, String joinOnField);
 
Example #29
Source File: FastJsonKit.java    From jfinal-ext3 with Apache License 2.0 2 votes vote down vote up
/**
 * json String to Model<T extends Model<T>>
 * @param json
 * @param clazz
 */
public static <M extends Model<M>> Model<M> jsonToModel(String json, Class<M> clazz){
	return FastJsonKit.parse(json, clazz);
}
 
Example #30
Source File: ProductService.java    From jboot-admin with Apache License 2.0 2 votes vote down vote up
/**
 * 添加关联数据到某个model中去,避免关联查询,提高性能。
 *
 * @param model
 * @param joinOnField
 * @param joinName
 */
public void join(Model model, String joinOnField, String joinName);