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

📄 entityserializehelper.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright 2006-2007 Queplix Corp.
 *
 * Licensed under the Queplix Public License, Version 1.1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.queplix.core.integrator.entity;

import com.queplix.core.client.app.vo.CheckBoxData;
import com.queplix.core.client.app.vo.DateFieldData;
import com.queplix.core.client.app.vo.EntityData;
import com.queplix.core.client.app.vo.EntityLinkFieldData;
import com.queplix.core.client.app.vo.EntityReferenceData;
import com.queplix.core.client.app.vo.FieldData;
import com.queplix.core.client.app.vo.FieldMeta;
import com.queplix.core.client.app.vo.GridData;
import com.queplix.core.client.app.vo.HistoryFieldData;
import com.queplix.core.client.app.vo.InFormGridFieldData;
import com.queplix.core.client.app.vo.ListboxFieldData;
import com.queplix.core.client.app.vo.ListboxFieldMeta;
import com.queplix.core.client.app.vo.MemoFieldData;
import com.queplix.core.client.app.vo.MultiselectFieldData;
import com.queplix.core.client.app.vo.SubsetData;
import com.queplix.core.client.app.vo.SubsetMeta;
import com.queplix.core.client.app.vo.TextareaFieldData;
import com.queplix.core.client.app.vo.TextboxFieldData;
import com.queplix.core.integrator.ActionContext;
import com.queplix.core.integrator.security.LogonSession;
import com.queplix.core.integrator.security.SecurityHelper;
import com.queplix.core.integrator.security.User;
import com.queplix.core.integrator.security.WebLoginManager;
import com.queplix.core.modules.config.ejb.CaptionManagerLocal;
import com.queplix.core.modules.config.error.UnknownEntityException;
import com.queplix.core.modules.eqlext.jxb.gr.ReqFilter;
import com.queplix.core.modules.eqlext.jxb.gr.ResField;
import com.queplix.core.modules.eqlext.jxb.gr.types.ConditionSType;
import com.queplix.core.modules.eqlext.utils.ExtDateParser;
import com.queplix.core.utils.DateHelper;
import com.queplix.core.utils.StringHelper;
import com.queplix.core.utils.log.AbstractLogger;
import com.queplix.core.utils.log.Log;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TimeZone;

/**
 * Provides all the procedures for serialization/deserialization of {@link com.queplix.core.client.app.vo.EntityData} and all the types in it.
 *
 * Also this class contains operations with 2.6v objects by converting it to new 3.0v objects.
 * @author Sergey Kozmin
 * @since 12.02.2007
 */
public class EntitySerializeHelper {
    
    private static final AbstractLogger logger = Log.getLog(EntitySerializeHelper.class);

    private static final String CHECKBOX_GRID_TRUE_MESSAGE_ID = "grid_checkbox_true_value";
    private static final String CHECKBOX_GRID_FALSE_MESSAGE_ID = "grid_checkbox_false_value";

    public static final String ENTITY_DIV = "#";
    public static final String FIELDS_DIV = ";";
    public static final String ID_AND_TEXT_DIV = ":";

    public static final String DATE_RANGE_DIV = "..";
    
    public static final String DATE_TIME_SEPARATOR = " ";

    /**
     * Could not return a null. Can return empty string object.
     *
     * @param data data to be converted to string value
     * @param meta metadata for field to determine type of it's value and it's RequestProperties
     * @return value string representation of data object
     * @throws IllegalControlTypeException thrown when data and metadata types mistmatch or metadata type doesn't supported
     */
    public static String getValueStringRepresentation(FieldData data, FieldMeta meta) throws IllegalControlTypeException {
        String repr;
        switch(meta.getDataType()) {
            case FieldMeta.CHECKBOX: {
                CheckBoxData cbd = (CheckBoxData) data;
                repr = (cbd.isChecked() == null) ? "0":(cbd.isChecked() ? "1":"0");
                break;
            }
            case FieldMeta.DATEFIELD: {
                DateFieldData dfd = (DateFieldData) data;
                String formatedDate = dfd.getStartDate() != null ? dfd.getFormatedDate() : "";
                repr = formatedDate == null ? "" : formatedDate;
                break;
            }
            case FieldMeta.LISTBOX: {
                ListboxFieldData lfd = (ListboxFieldData) data;
                StringBuffer sb = new StringBuffer("");
                long[] ids = lfd.getItemsSelected().getSelectedIDs();
                for(long id : ids) {
                    if(sb.length() > 0) {
                        sb.append("_");
                    }
                    sb.append(String.valueOf(id));
                }
                repr = sb.toString();
                break;
            }
            case FieldMeta.MEMO: {
                MemoFieldData mfd = (MemoFieldData) data;
                String text = mfd.getText();
                repr = text != null ? text:"";
                break;
            }
            case FieldMeta.HISTORY: {
                HistoryFieldData hfd = (HistoryFieldData) data;
                String text = hfd.getHistoryId();
                repr = text != null ? text : "";
                break;
            }
            case FieldMeta.TEXTAREA: {
                TextareaFieldData tfd = (TextareaFieldData) data;
                String text = tfd.getText();
                repr = text != null ? text:"";
                break;
            }
            case FieldMeta.TEXTBOX: {
                TextboxFieldData tbfm = (TextboxFieldData) data;
                String text = tbfm.getText();
                repr = text != null ? text:"";
                break;
            }
            case FieldMeta.ENTITYREFERENCE: {
                EntityReferenceData erd = (EntityReferenceData) data;
                repr = (erd.getSelectedRowID() != null) ? String.valueOf(erd.getSelectedRowID()) : "";
                break;
            }
            case FieldMeta.MULTISELECT: {
                throw new IllegalControlTypeException(
                        "Multiselect field type could not be presented by string value, should be updated as linked dataset. ");
            }
            case FieldMeta.IN_FORM_GRID: {
                throw new IllegalControlTypeException(
                        "Inform grid field type could not be presented by string value, should be updated as linked dataset. ");
            }
            case FieldMeta.ENTITYLINK: {
                throw new IllegalControlTypeException(
                        "EntityLink field type could not be presented by string value, should be updated as linked dataset. ");
            }
            default: {
                throw new IllegalControlTypeException(
                        "Unsupported field type [" + meta.getDataType() + "], for serialization. See FieldMeta class. ");
            }
        }
        return repr;
    }

    /**
     * Could not return a null. Can return empty string object.
     *
     * @param data data to be converted to string value
     * @param meta metadata for field to determine type of it's value and it's RequestProperties
     * @return text string representation of data object
     * @throws IllegalControlTypeException thrown when data and metadata types mistmatch or metadata type doesn't supported
     */
    public static String getTextStringRepresentation(FieldData data, FieldMeta meta) throws IllegalControlTypeException {
        String repr;
        switch(meta.getDataType()) {
            case FieldMeta.LISTBOX: {
                ListboxFieldData lfd = (ListboxFieldData) data;
                ListboxFieldMeta lbfm = (ListboxFieldMeta) meta;
                long[] ids = lfd.getItemsSelected().getSelectedIDs();
                repr = (ids.length > 0) ? lbfm.getAvailableChoises().getItemByID(ids[0]).getCaption() : "";
                break;
            }
            case FieldMeta.ENTITYREFERENCE: {
                EntityReferenceData erd = (EntityReferenceData) data;
                String filter = erd.getSelectedFilter();
                repr = (filter != null) ? filter: "";
                break;
            }
            default: {
                repr = "";
            }
        }
        return repr;
    }

    public static String getStringRepresentationForGrid(FieldData data, FieldMeta meta, LogonSession ls, ActionContext ctx)
            throws IllegalControlTypeException, CouldntGetEJBException {

        String result = "";
        if(data != null) {//if data could be a null object if selectable attribute set to false in entity xml config. 
            switch(meta.getDataType()) {
                case FieldMeta.DATEFIELD:
                case FieldMeta.TEXTAREA:
                case FieldMeta.TEXTBOX:
                    result = getValueStringRepresentation(data, meta);
                    break;

                case FieldMeta.CHECKBOX: {
                    CheckBoxData cbd = (CheckBoxData) data;

                    CaptionManagerLocal captionManager = ctx.getCaptionManager();
                    String languageID = WebLoginManager.getLogonLanguage(ls);
                    String trueValue = captionManager.getServerMessage(languageID, CHECKBOX_GRID_TRUE_MESSAGE_ID);
                    String falseValue = captionManager.getServerMessage(languageID, CHECKBOX_GRID_FALSE_MESSAGE_ID);

                    result = (cbd.isChecked() == null) ? falseValue:(cbd.isChecked() ? trueValue:falseValue);
                    break;
                }
                case FieldMeta.ENTITYREFERENCE: {
                    EntityReferenceData entityRefData = (EntityReferenceData) data;
                    String selectedFilter = entityRefData.getSelectedFilter(); // actually this is a listref field
                    result = (selectedFilter == null) ? "":selectedFilter;
                    break;
                }
                case FieldMeta.LISTBOX: {
                    List<String> results = new ArrayList<String>();
                    ListboxFieldData listboxData = (ListboxFieldData) data;
                    ListboxFieldMeta listboxMeta = (ListboxFieldMeta) meta;
                    SubsetMeta choises = listboxMeta.getAvailableChoises();
                    for(long id : listboxData.getItemsSelected().getSelectedIDs()) {
                        results.add(choises.getItemByID(id).getCaption());
                    }
                    result = results.size() == 0
                        ? "" : StringHelper.join(results.toArray(new String[results.size()]), ", ");
                    break;
                }
                case FieldMeta.MEMO: {
                    MemoFieldData memoData = (MemoFieldData) data;
                    result = StringHelper.html2text(memoData.getText());
                    break;
                }
                case FieldMeta.HISTORY: {
                    HistoryFieldData historyData = (HistoryFieldData) data;
                    result = StringHelper.html2text(historyData.getText());
                    break;
                }
                case FieldMeta.ENTITYLINK:
                    throw new IllegalControlTypeException("EntityLink field type could not be presented in grid");
                case FieldMeta.IN_FORM_GRID:
                    throw new IllegalControlTypeException("InFormGrid field type could not be presented in grid");
                case FieldMeta.MULTISELECT:
                    throw new IllegalControlTypeException("MultiSelect field type could not be presented in grid");

                default:
                    throw new IllegalControlTypeException(
                            "Unsupported field type [" + meta.getDataType() + "], for serialization. See FieldMeta class.");
            }
        }
        return result;
    }

    /**
     * Could not return a null object
     *
     * @param fld       initial field
     * @param fieldMeta meta for field
     * @param recordId record id where resField is located.
     * @param user      user
     * @param ctx servlet context @return field data, parsed from initial field
     * @throws IllegalControlTypeException if there is no such control presented in metadata
     */
    public static FieldData createFieldDataFromString(ResField fld, FieldMeta fieldMeta, Long recordId, User user, ActionContext ctx)
            throws IllegalControlTypeException {

        boolean hasContent = fld.getHasContent();
        String fieldValue = fld.getResFieldValue();
        String fieldID = fld.getName();
        String fieldText = fld.getResFieldText();
        return createFieldData(fieldID, fieldValue, fieldText, fieldMeta, recordId, user, ctx, hasContent);
    }

    public static FieldData createFieldData(String fieldID, String fieldValue, String fieldText, FieldMeta fieldMeta, Long recordId,
                                            User user, ActionContext ctx, boolean hasContent) {
        FieldData data;
        switch(fieldMeta.getDataType()) {
            case FieldMeta.CHECKBOX: {
                Boolean value = false;
                if(!StringHelper.isEmpty(fieldValue)) {
                    value = "1".equalsIgnoreCase(fieldValue);

⌨️ 快捷键说明

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