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

📄 entityserializehelper.java

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

    public static EntityData deSerializeEntity(String serializedEntity, LogonSession ls, ActionContext ctx) {
        EntityData ent = null;
        if(!StringHelper.isEmpty(serializedEntity)) {
            int nameEndIndex = serializedEntity.indexOf(FIELDS_DIV);
            if(nameEndIndex < 1) {//no need an empty entity name
                ent = null;
            } else {
                String entityName = serializedEntity.substring(0, nameEndIndex);//cant holds escaping chars in name
                ent = new EntityData();
                ent.setEntityID(entityName);
                try {
                    Map<String, FieldMeta> meta = EntityViewHelper.getMetaForEntity(entityName, EntityViewHelper.FieldsModificator.FORM,
                            false, ls, ctx);
                    FieldData[] fields = deSerializeFields(serializedEntity.substring(nameEndIndex + 1), entityName, meta, ls, ctx);
                    ent.setFields(fields);
                } catch (UnknownEntityException e) {
                    logger.WARN("Couldn't find entity [" + entityName + "], fields for this serializedEntity won't be added to the report. ", e);
                }
            }
        }
        return ent;
    }

    public static FieldData[] deSerializeFields(String fieldsStr, String entityName, Map<String, FieldMeta> meta, LogonSession ls,
                                                 ActionContext ctx) {
        List<FieldData> fields = new LinkedList<FieldData>();
        if(!StringHelper.isEmpty(fieldsStr)) {
            String[] nameAndValues = splitEscaped(fieldsStr, FIELDS_DIV);
            for(String nameAndValue : nameAndValues) {
                if(nameAndValue.length() > 2) {//don't need empty id or value
                    String[] arr = splitEscaped(nameAndValue, ID_AND_TEXT_DIV);
                    //cant have escape symbols in name, but can have in value and text
                    if(arr.length >= 2) {
                        FieldMeta fMeta = meta.get(arr[0]);
                        if(fMeta != null) {
                            String fieldValue = unEscape(arr[1]);
                            String fieldText = arr.length > 2 ? unEscape(arr[2]) : "";
                            FieldData fieldData = createFieldData(arr[0], fieldValue,
                                    fieldText,
                                    fMeta, -1L,
                                    ls.getUser(), ctx, true);
                            fields.add(fieldData);
                        } else {
                            logger.WARN("Couldn't find field [" + arr[0] + "] in entity [" + entityName + "]. The field won't be added to the report. ");
                        }
                    }
                }
            }
        }
        return fields.toArray(new FieldData[fields.size()]);
    }

    /**
     * Creates filter for the given field meta and field data. It field has unsupported type or value is empty method
     * return null object.
     *
     * @param locationEntityName
     * @param fieldMeta
     * @param filterData
     * @return
     */
    public static ReqFilter createEFieldFilter(String locationEntityName, FieldMeta fieldMeta, FieldData filterData, LogonSession ls) {
        ReqFilter filter = new ReqFilter();
        filter.setEntity(locationEntityName);
        filter.setName(fieldMeta.getFieldID());

        filter.setConditiontype(ConditionSType.AND);

        switch(fieldMeta.getDataType()) {
            case FieldMeta.CHECKBOX: {
                CheckBoxData cbd = (CheckBoxData) filterData;
                Boolean isChecked = cbd.isChecked();
                //zero value is the value that not equal to 1
                if(isChecked != null) {//if value is null, don't add the filter.
                    if(isChecked.equals(Boolean.TRUE)) {
                        filter.addReqFilterValue("1");
                    } else {
                        filter.setConditiontype(ConditionSType.OR);
                        filter.addReqFilterValue("!1");
                        filter.addReqFilterValue(StringHelper.NULL_VALUE);
                    }
                }
                break;
            }
            case FieldMeta.DATEFIELD: {
                User user = ls.getUser();
                String datePattern = user.getDatePattern();
                String timePattern = user.getTimePattern();
                DateFieldData dfd = (DateFieldData) filterData;
                if (dfd.getFormatedDate().equalsIgnoreCase(StringHelper.NULL_VALUE)) {
                    filter.addReqFilterValue(StringHelper.NULL_VALUE);
                } else if (dfd.getStartDate() != null && dfd.getEndDate() == null) {
                    String dateTimePattern = datePattern + DATE_TIME_SEPARATOR + timePattern;
                    filter.addReqFilterValue(DateHelper.formatDate(dfd.getStartDate(), dateTimePattern));
                } else if (dfd.getStartDate() != null && dfd.getEndDate() != null) {
                    Date end = new Date(dfd.getEndDate().getTime());
                    String startDateFormated = DateHelper.formatDate(dfd.getStartDate(), datePattern);
                    String endDateFormated = DateHelper.formatDate(end, datePattern);
                    String dateRange = startDateFormated + ".." + endDateFormated;
                    filter.addReqFilterValue(dateRange);
                } else {//if we don't need the filter
                    filter = null;
                }

                break;
            }
            case FieldMeta.LISTBOX: {
                filter.setConditiontype(ConditionSType.OR);
                ListboxFieldData lfd = (ListboxFieldData) filterData;
                long[] ids = lfd.getItemsSelected().getSelectedIDs();
                if(ids.length > 0) {
                    for(long id : ids) {
                        if(id == -1) {
                            filter.addReqFilterValue(StringHelper.NULL_VALUE);
                        } else {
                            filter.addReqFilterValue(String.valueOf(id));
                        }
                    }
                } else {
                    filter = null;
                }
                break;
            }
            case FieldMeta.MEMO: {
                MemoFieldData mfd = (MemoFieldData) filterData;
                if(!StringHelper.isEmpty(mfd.getText())) {
                    filter.addReqFilterValue(mfd.getText());
                } else {
                    filter = null;
                }
                break;
            }
            case FieldMeta.TEXTAREA: {
                TextareaFieldData tfd = (TextareaFieldData) filterData;
                if(!StringHelper.isEmpty(tfd.getText())) {
                    filter.addReqFilterValue(tfd.getText());
                } else {
                    filter = null;
                }
                break;
            }
            case FieldMeta.TEXTBOX: {
                TextboxFieldData tbfm = (TextboxFieldData) filterData;
                if(!StringHelper.isEmpty(tbfm.getText())) {
                    filter.addReqFilterValue(tbfm.getText());
                } else {
                    filter = null;
                }
                break;
            }
            case FieldMeta.ENTITYREFERENCE: {
                EntityReferenceData erd = (EntityReferenceData) filterData;
                Long rowID = erd.getSelectedRowID();
                if(rowID != null) {
                    filter.addReqFilterValue(String.valueOf(rowID));
                } else {
                    String selectedFilter = erd.getSelectedFilter();
                    if(!StringHelper.isEmpty(selectedFilter)) {
                        filter.setUsetextfield(true);//old core emulation
                        filter.addReqFilterValue(selectedFilter);
                        filter.addReqFilterText(selectedFilter);
                    } else {
                        filter = null;
                    }
                }
                break;
            }
        }
        return filter;
    }

    public static FieldData initializeEmptyFieldData(FieldData data, FieldMeta fieldMeta, Long recordId) {
        switch(fieldMeta.getDataType()) {
            case FieldMeta.CHECKBOX:
            case FieldMeta.DATEFIELD:
            case FieldMeta.LISTBOX:
            case FieldMeta.MULTISELECT:
            case FieldMeta.TEXTAREA:
            case FieldMeta.TEXTBOX:
            case FieldMeta.IN_FORM_GRID:
            case FieldMeta.ENTITYREFERENCE:
            case FieldMeta.ENTITYLINK:
                break;
            case FieldMeta.MEMO: {
                ((MemoFieldData)data).setRecordId(recordId);
                break;
            }
            case FieldMeta.HISTORY: {
                ((HistoryFieldData)data).setRecordId(recordId);
                break;
            }
            default: {
                throw new IllegalControlTypeException("Couldn't initialize the field with type [" + fieldMeta.getDataType() +
                        "], because it doesn't supported now. ");
            }
        }
        return data;
    }

    /**
     * Create field data for the given type, where rowID value (record id) will be stored. This method is used when
     * client code needs to create data for the field which is set to be "external-field" for some form.
     * For now only 4 types of the external field is supported.
     *
     * @param fieldMeta         meta for field
     * @param rowID             external field id
     * @param listRefFieldValue value, that is string representation of the entity
     * @return empty field data
     * @throws IllegalControlTypeException if there is no such control presented in metadata
     */
    public static FieldData createExternalFieldDataForPkey(FieldMeta fieldMeta, Long rowID, String listRefFieldValue)
            throws IllegalControlTypeException {

        FieldData data;
        String fieldID = fieldMeta.getFieldID();
        switch(fieldMeta.getDataType()) {
            case FieldMeta.MEMO: {
                data = new MemoFieldData(fieldID, Long.toString(rowID), rowID);
                break;
            }
            case FieldMeta.TEXTAREA: {
                data = new TextareaFieldData(fieldID, Long.toString(rowID));
                break;
            }
            case FieldMeta.TEXTBOX: {
                data = new TextboxFieldData(fieldID, Long.toString(rowID));
                break;
            }
            case FieldMeta.ENTITYREFERENCE: {
                data = new EntityReferenceData(fieldID, listRefFieldValue, rowID);
                break;
            }
            default: {
                throw new IllegalControlTypeException("Couldn't create external field data for the field with type ["
                        + fieldMeta.getDataType() +
                        "], because this field could not store Long value. ");
            }
        }
        return data;
    }

    /**
     * Retrieve rowId from field data.
     *
     * @param fieldMeta         meta for field
     * @param data field data
     * @return rowId row id
     * @throws IllegalControlTypeException if there is no such control presented
     * in metadata or such control doesn't hold rowId.
     * @throws IncorrectFormDescriptionException thrown if data doesn't hold
     * appropriate rowId value. This value should be filled automaticaly.
     */
    public static Long retrieveRowId(FieldMeta fieldMeta, FieldData data)
            throws IllegalControlTypeException,
            IncorrectFormDescriptionException {
        Long rowId;
        try {
            switch(fieldMeta.getDataType()) {
                case FieldMeta.MEMO: {
                    rowId = Long.parseLong(((MemoFieldData)data).getText());
                    break;
                }
                case FieldMeta.TEXTAREA: {
                    rowId = Long.parseLong(((TextareaFieldData)data).getText());
                    break;
                }
                case FieldMeta.TEXTBOX: {
                    rowId = Long.parseLong(((TextboxFieldData)data).getText());
                    break;
                }
                case FieldMeta.ENTITYREFERENCE: {
                    rowId = ((EntityReferenceData)data).getSelectedRowID();
                    break;
                }
                default: {
                    throw new IllegalControlTypeException("Couldn't retrieve rowId from field of type ["
                            + fieldMeta.getDataType() +
                            "], because this field could not store Long value. ");
                }
            }
        } catch (NumberFormatException e) {
            throw new IncorrectFormDescriptionException("Couldn't retrieve rowId from field of type ["
                        + fieldMeta.getDataType() +
                        "], because this field could not store Long value. ");
        }
        return rowId;
    }
}

⌨️ 快捷键说明

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