⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericdelegator.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    }

    /** Creates a Entity in the form of a GenericValue and write it to the datasource
     *@param value The GenericValue to create a value in the datasource from
     *@param doCacheClear boolean that specifies whether or not to automatically clear cache entries related to this operation
     *@return GenericValue instance containing the new instance
     */
    public GenericValue create(GenericValue value, boolean doCacheClear) throws GenericEntityException {
        Map ecaEventMap = this.getEcaEntityEventMap(value.getEntityName());
        this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false);

        if (value == null) {
            throw new GenericEntityException("Cannot create a null value");
        }
        GenericHelper helper = getEntityHelper(value.getEntityName());

        this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false);

        value.setDelegator(this);
        value = helper.create(value);

        if (value != null) {
            value.setDelegator(this);
            if (value.lockEnabled()) {
                refresh(value, doCacheClear);
            } else {
                if (doCacheClear) {
                    this.evalEcaRules(EntityEcaHandler.EV_CACHE_CLEAR, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false);
                    this.clearCacheLine(value);
                }
            }
        }
        this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_CREATE, value, ecaEventMap, (ecaEventMap == null), false);
        return value;
    }

    /** Creates a Entity in the form of a GenericValue and write it to the datasource
     *@param primaryKey The GenericPK to create a value in the datasource from
     *@return GenericValue instance containing the new instance
     */
    public GenericValue create(GenericPK primaryKey) throws GenericEntityException {
        return this.create(primaryKey, true);
    }

    /** Creates a Entity in the form of a GenericValue and write it to the datasource
     *@param primaryKey The GenericPK to create a value in the datasource from
     *@param doCacheClear boolean that specifies whether to clear related cache entries for this primaryKey to be created
     *@return GenericValue instance containing the new instance
     */
    public GenericValue create(GenericPK primaryKey, boolean doCacheClear) throws GenericEntityException {
        if (primaryKey == null) {
            throw new GenericEntityException("Cannot create from a null primaryKey");
        }

        return this.create(new GenericValue(primaryKey), doCacheClear);
    }

    /** Creates or stores an Entity
     *@param value The GenericValue instance containing the new or existing instance
     *@param doCacheClear boolean that specifies whether or not to automatically clear cache entries related to this operation
     *@return GenericValue instance containing the new or updated instance
     */
    public GenericValue createOrStore(GenericValue value, boolean doCacheClear) throws GenericEntityException {
        GenericValue checkValue = this.findByPrimaryKey(value.getPrimaryKey());
        if (checkValue != null) {
            this.store(value, doCacheClear);
        } else {
            this.create(value, doCacheClear);
        }
        if (value.lockEnabled()) {
            this.refresh(value);
        }
        return value;
    }

    /** Creates or stores an Entity
     *@param value The GenericValue instance containing the new or existing instance
     *@return GenericValue instance containing the new or updated instance
     */
    public GenericValue createOrStore(GenericValue value) throws GenericEntityException {
        return createOrStore(value, true);
    }

    /** Find a Generic Entity by its Primary Key
     *@param primaryKey The primary key to find by.
     *@return The GenericValue corresponding to the primaryKey
     */
    public GenericValue findByPrimaryKey(GenericPK primaryKey) throws GenericEntityException {
        Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName());
        this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false);

        GenericHelper helper = getEntityHelper(primaryKey.getEntityName());
        GenericValue value = null;

        if (!primaryKey.isPrimaryKey()) {
            throw new GenericModelException("[GenericDelegator.findByPrimaryKey] Passed primary key is not a valid primary key: " + primaryKey);
        }
        this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false);
        try {
            value = helper.findByPrimaryKey(primaryKey);
        } catch (GenericEntityNotFoundException e) {
            value = null;
        }
        if (value != null) value.setDelegator(this);

        this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false);
        return value;
    }

    /** Find a CACHED Generic Entity by its Primary Key
     *@param primaryKey The primary key to find by.
     *@return The GenericValue corresponding to the primaryKey
     */
    public GenericValue findByPrimaryKeyCache(GenericPK primaryKey) throws GenericEntityException {
        Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName());
        this.evalEcaRules(EntityEcaHandler.EV_CACHE_CHECK, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false);

        GenericValue value = this.getFromPrimaryKeyCache(primaryKey);
        if (value == null) {
            value = findByPrimaryKey(primaryKey);
            if (value != null) {
                this.evalEcaRules(EntityEcaHandler.EV_CACHE_PUT, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false);
                this.putInPrimaryKeyCache(primaryKey, value);
            }
        }
        return value;
    }

    /** Find a Generic Entity by its Primary Key
     *@param entityName The Name of the Entity as defined in the entity XML file
     *@param fields The fields of the named entity to query by with their corresponging values
     *@return The GenericValue corresponding to the primaryKey
     */
    public GenericValue findByPrimaryKey(String entityName, Map fields) throws GenericEntityException {
        return findByPrimaryKey(makePK(entityName, fields));
    }

    /** Find a CACHED Generic Entity by its Primary Key
     *@param entityName The Name of the Entity as defined in the entity XML file
     *@param fields The fields of the named entity to query by with their corresponging values
     *@return The GenericValue corresponding to the primaryKey
     */
    public GenericValue findByPrimaryKeyCache(String entityName, Map fields) throws GenericEntityException {
        return findByPrimaryKeyCache(makePK(entityName, fields));
    }

    /** Find a Generic Entity by its Primary Key and only returns the values requested by the passed keys (names)
     *@param primaryKey The primary key to find by.
     *@param keys The keys, or names, of the values to retrieve; only these values will be retrieved
     *@return The GenericValue corresponding to the primaryKey
     */
    public GenericValue findByPrimaryKeyPartial(GenericPK primaryKey, Set keys) throws GenericEntityException {
        Map ecaEventMap = this.getEcaEntityEventMap(primaryKey.getEntityName());
        this.evalEcaRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false);

        GenericHelper helper = getEntityHelper(primaryKey.getEntityName());
        GenericValue value = null;

        if (!primaryKey.isPrimaryKey()) {
            throw new GenericModelException("[GenericDelegator.findByPrimaryKey] Passed primary key is not a valid primary key: " + primaryKey);
        }

        this.evalEcaRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false);
        try {
            value = helper.findByPrimaryKeyPartial(primaryKey, keys);
        } catch (GenericEntityNotFoundException e) {
            value = null;
        }
        if (value != null) value.setDelegator(this);

        this.evalEcaRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, primaryKey, ecaEventMap, (ecaEventMap == null), false);
        return value;
    }

    /** Find a number of Generic Value objects by their Primary Keys, all at once
     *@param primaryKeys A Collection of primary keys to find by.
     *@return List of GenericValue objects corresponding to the passed primaryKey objects
     */
    public List findAllByPrimaryKeys(Collection primaryKeys) throws GenericEntityException {
        //TODO: add eca eval calls
        if (primaryKeys == null) return null;
        List results = new LinkedList();

        // from the delegator level this is complicated because different GenericPK
        // objects in the list may correspond to different helpers
        HashMap pksPerHelper = new HashMap();
        Iterator pkiter = primaryKeys.iterator();

        while (pkiter.hasNext()) {
            GenericPK curPK = (GenericPK) pkiter.next();
            String helperName = this.getEntityHelperName(curPK.getEntityName());
            List pks = (List) pksPerHelper.get(helperName);

            if (pks == null) {
                pks = new LinkedList();
                pksPerHelper.put(helperName, pks);
            }
            pks.add(curPK);
        }

        Iterator helperIter = pksPerHelper.entrySet().iterator();

        while (helperIter.hasNext()) {
            Map.Entry curEntry = (Map.Entry) helperIter.next();
            String helperName = (String) curEntry.getKey();
            GenericHelper helper = GenericHelperFactory.getHelper(helperName);
            List values = helper.findAllByPrimaryKeys((List) curEntry.getValue());

            results.addAll(values);
        }
        return results;
    }

    /** Find a number of Generic Value objects by their Primary Keys, all at once;
     *  this first looks in the local cache for each PK and if there then it puts it
     *  in the return list rather than putting it in the batch to send to
     *  a given helper.
     *@param primaryKeys A Collection of primary keys to find by.
     *@return List of GenericValue objects corresponding to the passed primaryKey objects
     */
    public List findAllByPrimaryKeysCache(Collection primaryKeys) throws GenericEntityException {
        //TODO: add eca eval calls
        if (primaryKeys == null)
            return null;
        List results = new LinkedList();

        // from the delegator level this is complicated because different GenericPK
        // objects in the list may correspond to different helpers
        HashMap pksPerHelper = new HashMap();
        Iterator pkiter = primaryKeys.iterator();

        while (pkiter.hasNext()) {
            GenericPK curPK = (GenericPK) pkiter.next();

            GenericValue value = this.getFromPrimaryKeyCache(curPK);

            if (value != null) {
                // it is in the cache, so just put the cached value in the results
                results.add(value);
            } else {
                // is not in the cache, so put in a list for a call to the helper
                String helperName = this.getEntityHelperName(curPK.getEntityName());
                List pks = (List) pksPerHelper.get(helperName);

                if (pks == null) {
                    pks = new LinkedList();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -