io.jboot.db.model.JbootModel Java Examples

The following examples show how to use io.jboot.db.model.JbootModel. 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: ModelCopier.java    From jboot with Apache License 2.0 6 votes vote down vote up
/**
 * copy model set
 *
 * @param modelSet
 * @param <M>
 * @return
 */
public static <M extends JbootModel> Set<M> copy(Set<M> modelSet) {
    if (modelSet == null || modelSet.isEmpty()) {
        return modelSet;
    }

    Set<M> set = modelSet instanceof HashSet
            ? new HashSet<>(modelSet.size())
            : newInstance(modelSet.getClass());

    for (M m : modelSet) {
        set.add(copy(m));
    }

    return set;
}
 
Example #2
Source File: ModelCopier.java    From jboot with Apache License 2.0 6 votes vote down vote up
/**
 * copy model list
 *
 * @param modelList
 * @param <M>
 * @return
 */
public static <M extends JbootModel> List<M> copy(List<M> modelList) {
    if (modelList == null || modelList.isEmpty()) {
        return modelList;
    }

    List<M> list = modelList instanceof ArrayList
            ? new ArrayList<>(modelList.size())
            : newInstance(modelList.getClass());

    for (M m : modelList) {
        list.add(copy(m));
    }

    return list;
}
 
Example #3
Source File: JbootCacheInterceptor.java    From jboot with Apache License 2.0 6 votes vote down vote up
private <M extends JbootModel> Object getCopyObject(Invocation inv, Object data) {
    if (data instanceof List) {
        return ModelCopier.copy((List<? extends JbootModel>) data);
    } else if (data instanceof Set) {
        return ModelCopier.copy((Set<? extends JbootModel>) data);
    } else if (data instanceof Page) {
        return ModelCopier.copy((Page<? extends JbootModel>) data);
    } else if (data instanceof JbootModel) {
        return ModelCopier.copy((JbootModel) data);
    } else if (data.getClass().isArray()
            && JbootModel.class.isAssignableFrom(data.getClass().getComponentType())) {
        return ModelCopier.copy((M[]) data);
    } else {
        throw newException(null, inv, data);
    }
}
 
Example #4
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 #5
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 #6
Source File: Copyer.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static <M extends JbootModel> List<M> copy(List<M> list) {
    if (list == null || list.isEmpty()) {
        return null;
    }

    List<M> rlist = new ArrayList<>(list.size());
    list.forEach(m -> rlist.add((M) m.copy()));
    return rlist;
}
 
Example #7
Source File: AddonInfo.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void addModel(Class<? extends JbootModel> clazz) {
    Table table = clazz.getAnnotation(Table.class);
    if (table == null) {
        return;
    }
    if (models == null) {
        models = new ArrayList<>();
    }
    models.add(clazz);
}
 
Example #8
Source File: AddonManager.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 必须要移除所有的缓存,否则当插件卸载重新安装的时候,
 * 缓存里的可能还存在数据,由于可能是内存缓存,所有可能导致Class转化异常的问题
 * PS:每次新安装的插件,都是一个新的 Classloader
 *
 * @param addonInfo
 */
private void removeModelsCache(AddonInfo addonInfo) {
    List<Class<? extends JbootModel>> modelClasses = addonInfo.getModels();
    if (modelClasses != null) {
        modelClasses.forEach(aClass -> {
            Table table = aClass.getAnnotation(Table.class);
            String tableName = AnnotationUtil.get(table.tableName());
            JbootModelConfig.getConfig().getIdCache().removeAll(tableName);
            Jboot.getCache().removeAll(tableName);
        });
    }
}
 
Example #9
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 #10
Source File: ModelCopier.java    From jboot with Apache License 2.0 5 votes vote down vote up
/**
 * copy model page
 *
 * @param modelPage
 * @param <M>
 * @return
 */
public static <M extends JbootModel> Page<M> copy(Page<M> modelPage) {
    if (modelPage == null) {
        return null;
    }

    List<M> modelList = modelPage.getList();
    if (modelList == null || modelList.isEmpty()) {
        return modelPage;
    }

    modelPage.setList(copy(modelList));
    return modelPage;
}
 
Example #11
Source File: ModelCopier.java    From jboot with Apache License 2.0 5 votes vote down vote up
/**
 * copy model array
 * @param models
 * @param <M>
 * @return
 */
public static <M extends JbootModel> M[] copy(M[] models) {
    if (models == null || models.length == 0) {
        return models;
    }

    M[] array = (M[]) Array.newInstance(models.getClass().getComponentType(), models.length);
    int i = 0;
    for (M m : models) {
        array[i++] = copy(m);
    }
    return array;
}
 
Example #12
Source File: JbootAopFactory.java    From jboot with Apache License 2.0 4 votes vote down vote up
@Override
protected void doInject(Class<?> targetClass, Object targetObject) throws ReflectiveOperationException {
    targetClass = getUsefulClass(targetClass);
    Field[] fields = targetClass.getDeclaredFields();

    if (fields.length != 0) {

        for (Field field : fields) {

            Inject inject = field.getAnnotation(Inject.class);
            if (inject != null) {
                Bean bean = field.getAnnotation(Bean.class);
                String beanName = bean != null ? AnnotationUtil.get(bean.name()) : null;
                if (StrUtil.isNotBlank(beanName)) {
                    doInjectByName(targetObject, field, inject, beanName);
                } else {
                    doInjectJFinalOrginal(targetObject, field, inject);
                }
                continue;
            }

            ConfigValue configValue = field.getAnnotation(ConfigValue.class);
            if (configValue != null) {
                doInjectConfigValue(targetObject, field, configValue);
                continue;
            }

            RPCInject rpcInject = field.getAnnotation(RPCInject.class);
            if (rpcInject != null) {
                doInjectRPC(targetObject, field, rpcInject);
                continue;
            }
        }
    }


    // 是否对超类进行注入
    if (injectSuperClass) {
        Class<?> c = targetClass.getSuperclass();
        if (c != JbootController.class
                && c != Controller.class
                && c != JbootServiceBase.class
                && c != Object.class
                && c != JbootModel.class
                && c != Model.class
                && c != null
        ) {
            doInject(c, targetObject);
        }
    }
}
 
Example #13
Source File: JbootServiceBase.java    From jboot with Apache License 2.0 4 votes vote down vote up
public JbootModel getDao() {
    return DAO;
}
 
Example #14
Source File: AddonInfo.java    From jpress with GNU Lesser General Public License v3.0 4 votes vote down vote up
public List<Class<? extends JbootModel>> getModels() {
    return models;
}
 
Example #15
Source File: AddonInfo.java    From jpress with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void setModels(List<Class<? extends JbootModel>> models) {
    this.models = models;
}
 
Example #16
Source File: AddonClassLoader.java    From jpress with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void load() {
    for (String className : classNameList) {
        try {

            Class loadedClass = loadClass(className);

            Bean bean = (Bean) loadedClass.getDeclaredAnnotation(Bean.class);
            if (bean != null) {
                initBeanMapping(loadedClass);
            }

            // controllers
            if (Controller.class.isAssignableFrom(loadedClass)) {
                addonInfo.addController(loadedClass);
            }
            // interceptors
            else if (Interceptor.class.isAssignableFrom(loadedClass)) {
                if (loadedClass.getAnnotation(GlobalInterceptor.class) != null) {
                    addonInfo.addInterceptor(loadedClass);
                }
            }
            // handlers
            else if (Handler.class.isAssignableFrom(loadedClass)) {
                addonInfo.addHandler(loadedClass);
            }
            // models
            else if (JbootModel.class.isAssignableFrom(loadedClass)) {
                addonInfo.addModel(loadedClass);
            }
            // directives
            else if (Directive.class.isAssignableFrom(loadedClass)) {
                addonInfo.addDirective(loadedClass);
            }
            // wechatAddons
            else if (WechatAddon.class.isAssignableFrom(loadedClass)) {
                addonInfo.addWechatAddon(loadedClass);
            }
            // addonClass
            else if (Addon.class.isAssignableFrom(loadedClass)) {
                addonInfo.setAddonClass(loadedClass);
            }
            // upgraderClass
            else if (AddonUpgrader.class.isAssignableFrom(loadedClass)) {
                addonInfo.setUpgraderClass(loadedClass);
            }

        } catch (Exception ex) {
            LOG.error(ex.toString(), ex);
        }
    }
}
 
Example #17
Source File: JbootServiceJoinerImpl.java    From jboot with Apache License 2.0 2 votes vote down vote up
/**
 * 可以让子类去复写joinById ,比如默认只 join 部分字段等
 *
 * @param id
 * @return
 */
protected abstract JbootModel joinById(Object id);
 
Example #18
Source File: JbootServiceBase.java    From jboot with Apache License 2.0 2 votes vote down vote up
/**
 * 复写 JbootServiceJoinerImpl 的方法
 *
 * @param id
 * @return
 */
@Override
protected JbootModel joinById(Object id) {
    return findById(id);
}
 
Example #19
Source File: ModelCopier.java    From jboot with Apache License 2.0 2 votes vote down vote up
/**
 * copy model
 *
 * @param model
 * @param <M>
 * @return
 */
public static <M extends JbootModel> M copy(M model) {
    return model == null ? null : (M) model.copy();
}