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

📄 entityfacade.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

        Sreq sreq = new Sreq();
        sreq.setChecklock(true);
        sreq.setGetresponse(false);
        sreq.setName(entityName);

        for(EntityData data : entityData) {
            SreqRecord sreqRecord = new SreqRecord();
            sreq.addSreqRecord(sreqRecord);
            sreqRecord.setTodo(TodoSType.DELETE);

            SreqField sreqField = new SreqField();
            sreqRecord.addSreqField(sreqField);
            sreqField.setName(pkeyName);
            sreqField.setSreqFieldValue(data.getRowID().toString());
        }

        SetRecordsLocal setRecordsManager = ctx.getSetRecordsManager();
        setRecordsManager.process(sreq, ls);
    }

    /**
     *  Delete the single entity with the given id.
     * @param entityName entity name to delete
     * @param entityId record id in DB.
     * @param ctx action context
     * @param ls logon session
     * @throws EQLException if some eql exception occured.
     */
    public static void deleteRecord(String entityName, Long entityId, ActionContext ctx, LogonSession ls)
            throws EQLException {

        String pkeyName = EntityViewHelper.getPkeyID(entityName, ctx);

        Sreq sreq = new Sreq();
        sreq.setChecklock(true);
        sreq.setGetresponse(false);
        sreq.setName(entityName);

        SreqRecord sreqRecord = new SreqRecord();
        sreq.addSreqRecord(sreqRecord);
        sreqRecord.setTodo(TodoSType.DELETE);

        SreqField sreqField = new SreqField();
        sreqRecord.addSreqField(sreqField);
        sreqField.setName(pkeyName);
        sreqField.setSreqFieldValue(entityId.toString());

        SetRecordsLocal setRecordsManager = ctx.getSetRecordsManager();
        setRecordsManager.process(sreq, ls);
    }

    public static void insertRecord(EntityData entityToUpdate, ActionContext ctx, LogonSession ls)
            throws EQLException, RecordDoesntExistsException {
        changeRecord(entityToUpdate, true, ctx, ls);
    }

    public static void updateRecord(EntityData entityToUpdate, ActionContext ctx, LogonSession ls)
            throws EQLException, RecordDoesntExistsException {
        changeRecord(entityToUpdate, false, ctx, ls);
    }

    private static void changeRecord(EntityData entityToUpdate, boolean toBeInserted, ActionContext ctx, LogonSession ls)
            throws RecordDoesntExistsException, EQLException {
        String entityName = entityToUpdate.getEntityID();
        Long rowID = entityToUpdate.getRowID();

        Collection<FieldData> oldData;
        if(toBeInserted) {//old data is empty
            oldData = Arrays.asList(entityToUpdate.getFields());
        } else {//get old data from DB
            IntegratedRecordSet dbFieldsData = getEntityByIDRequest(entityName, rowID, ls, ctx);
            if(dbFieldsData.getRowsCount() > 0) {//record still exists
                oldData = dbFieldsData.getRecordSet().get(0).getFieldSet().values();
            } else {
                throw new RecordDoesntExistsException("Could not update data. Record does not exist at the DB. ");
            }
        }

        //update base entity
        performInitialEntityUpdate(entityName, toBeInserted, ls, ctx, oldData, entityToUpdate);
    }

    /**
     * Performing initial entity update. Non entity datasets is updated after this call.
     *
     * @param entityName     entity to update
     * @param toBeInserted   to be inserted or to be updated
     * @param ls             logon session
     * @param ctx            servlet context
     * @param dbFieldsData   old data, that is stored in db (old data), or in case of new entity that is initial data,
     *                       that was sent from user (copy of another record for example, or empty collection)
     * @param entityToUpdate entity data to be inserted to db (new data)
     * @throws EQLException           thrown if eql exception occured
     *
     */
    private static void performInitialEntityUpdate(String entityName, boolean toBeInserted, LogonSession ls,
                                                   ActionContext ctx, Collection<FieldData> dbFieldsData, EntityData entityToUpdate)
            throws EQLException {
        //create update records castor request object
        Sreq setRecords = new Sreq();
        setRecords.setChecklock(true);
        setRecords.setName(entityName);
        setRecords.setGetresponse(false);//will get result record manualy. not performance decreasing.

        //fill up entities
        SreqRecord record = new SreqRecord();

        if(toBeInserted) {
            record.setTodo(TodoSType.INSERT);
        } else {
            record.setTodo(TodoSType.UPDATE);
        }

        //fill up fields to be updated
        Map<String, FieldMeta> metas = EntityViewHelper.getMetaForEntity(entityName, EntityViewHelper.FieldsModificator.FORM,
                false, ls, ctx);

        HashMap<String, FieldData> oldDatas = new HashMap<String, FieldData>();
        for(FieldData data : dbFieldsData) {//could not be null, because here came only if 1 rec exists
            oldDatas.put(data.getFieldID(), data);
        }

        FieldData[] fieldsList = entityToUpdate.getFields();
        LinkedList<SreqField> updateFields = new LinkedList<SreqField>();
        LinkedList<SreqDataset> updateDataset = new LinkedList<SreqDataset>();
        for(FieldData data : fieldsList) {
            FieldData oldData = oldDatas.get(data.getFieldID());
            FieldMeta meta = metas.get(data.getFieldID());
            if(meta != null && oldData != null) {//metadata should not be a null and old data field should not be a null
                try {
                    int fieldType = meta.getDataType();
                    if(fieldType == FieldMeta.MULTISELECT || fieldType == FieldMeta.ENTITYLINK || fieldType == FieldMeta.IN_FORM_GRID) {
                        DatasetDescriptor descriptor = EntityViewHelper.getDatasetMetadata(entityName, meta.getFieldID(), ctx);
                        if(toBeInserted) {//because it merges it, and doesn't add if it's the same.
                            oldData = EntitySerializeHelper.createEmptyFieldData(meta, ls.getUser());
                        }
                        addUpdatingDataset(data, meta, oldData, updateDataset, descriptor, entityToUpdate.getRowID());
                    } else {
                        addUpdatingField(data, meta, toBeInserted, oldData, updateFields);
                    }
                } catch (IllegalControlTypeException e) {
                    logger.ERROR("Field [" + data.getFieldID() + "], entity [" + entityName +
                            "] was been tried to update. See caused error for details. Field excluded from update. ", e);
                }
            }
        }

        //perform update
        if(updateFields.size() != 0 || updateDataset.size() != 0) {
            record.setSreqField(updateFields.toArray(new SreqField[updateFields.size()]));
            record.setSreqDataset(updateDataset.toArray(new SreqDataset[updateDataset.size()]));
            setRecords.setSreqRecord(new SreqRecord[]{record});
            SetRecordsLocal setter = ctx.getSetRecordsManager();
            setter.process(setRecords, ls);
        }
    }

    /**
     * This method creates entity prototype. It means, that nothing is inserted into the DB, but all the custom server scripts will be
     * applied and all def-src attributes performed. 
     * @param entityName creating entity name
     * @param prototypeDataList data list, that is to be used when no data was created by custom scripts.
     * @param ls logon session
     * @param ctx servlet context
     * @return entity prototype object
     * @throws EQLException thrown if some eql exc occured during the method.
     * @throws RecordDoesntExistsException thrown if record wasn't created.
     */
    public static EntityData createEntityPrototype(String entityName, Collection<FieldData> prototypeDataList, LogonSession ls, ActionContext ctx)
            throws EQLException, RecordDoesntExistsException {
        IntegratedRecordSet recordset = EntityFacade.createEmptyEntityRequest(entityName, ls, ctx);
        EntityData entityData;
        if (recordset.getRowsCount() > 0) {
            //find what exactly rows should be included to resultset
            Map<String, FieldMeta> metas = EntityViewHelper.getMetaForEntity(entityName, EntityViewHelper.FieldsModificator.FORM,
                    false, ls, ctx);

            //fill prototype data values
            HashMap<String, FieldData> prototypeDatas = new HashMap<String, FieldData>(prototypeDataList.size());
            for (FieldData fieldData : prototypeDataList) {
                prototypeDatas.put(fieldData.getFieldID(), fieldData);
            }

            List<FieldData> data = new LinkedList<FieldData>();

            IntegratedRecord firstRecord = recordset.getRecordSet().get(0);
            Map<String, FieldData> dbValuesMap = firstRecord.getFieldSet();
            Map<String, FieldMeta> dbMetaMap = recordset.getRecordMeta();
            Set<String> keys = dbValuesMap.keySet();

            //get row id
            FieldData rowIDField = firstRecord.getFieldSet().get(recordset.getPkeyFieldName());
            Long rowID = Long.parseLong(EntitySerializeHelper.getValueStringRepresentation(rowIDField, metas.get(
                    recordset.getPkeyFieldName())));

            //choose what should pass as new record 
            for (String key : keys) {
                FieldData dataToAdd;

                FieldMeta fieldMeta = dbMetaMap.get(key);
                FieldData fieldData = dbValuesMap.get(key);
                if (!fieldData.isEmpty()) {//core returned filled value, don't edit it
                    dataToAdd = fieldData;
                } else {
                    if (!fieldMeta.hasDefSrcAttribute() && !fieldMeta.hasClearAttribute()) {//can be filled up with client values
                        dataToAdd = prototypeDatas.get(key);
                    } else {//was filled up with empty value
                        dataToAdd = fieldData;
                        if (fieldMeta.hasDefSrcAttribute()) {
                            logger.INFO("Don't fill field: " + key + ", entity :" + entityName
                                    + ", because it has eql-defsrc value: [" + EntitySerializeHelper.getValueStringRepresentation(fieldData,
                                        fieldMeta) + "]");
                        }
                    }
                }
                if(dataToAdd != null) {
                    EntitySerializeHelper.initializeEmptyFieldData(fieldData, fieldMeta, rowID);//fill up with values
                    data.add(dataToAdd);
                }
            }

            entityData = new EntityData(entityName, rowID, data.toArray(new FieldData[data.size()]));
        } else {
            throw new RecordDoesntExistsException("Could not create initial entity prototype. ");
        }
        return entityData;
    }

    private static void addUpdatingDataset(FieldData data, FieldMeta meta, FieldData oldData,
                                           LinkedList<SreqDataset> updateDatasets, DatasetDescriptor descriptor,
                                           Long recordID)
            throws IllegalControlTypeException, EQLException, CouldntGetEJBException,
            IncorrectEntityDescriptionException {

        SreqDataset dataSet = new SreqDataset();
        dataSet.setName(descriptor.getLocationEntityFieldName());
        dataSet.setEntity(descriptor.getBindingEntity());
        dataSet.setExternal(descriptor.getLocationPkeyFieldName());
        dataSet.setInternal(descriptor.getBindingEntityFkeyFieldNameToLocation());

        switch(meta.getDataType()) {
            case FieldMeta.MULTISELECT: {
                MultiselectFieldData mfData = (MultiselectFieldData) data;
                MultiselectFieldData mfOldData = (MultiselectFieldData) oldData;

                long[] selectedIDsMultiselect = mfData.getItemsSelected().getSelectedIDs();
                long[] oldIDsMultiselect = mfOldData.getItemsSelected().getSelectedIDs();

                Set<Long> convSelectedIDs = new HashSet<Long>(selectedIDsMultiselect.length);
                Set<Long> convOldIDs = new HashSet<Long>(oldIDsMultiselect.length);

                CollectionsHelper.copyArrayToCollection(selectedIDsMultiselect, convSelectedIDs);
                CollectionsHelper.copyArrayToCollection(oldIDsMultiselect, convOldIDs);

⌨️ 快捷键说明

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