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

📄 boundformsoperationshelper.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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.EntityData;
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.RowData;
import com.queplix.core.integrator.ActionContext;
import com.queplix.core.integrator.security.LogonSession;
import com.queplix.core.integrator.security.User;
import com.queplix.core.modules.config.ejb.FocusConfigManagerLocal;
import com.queplix.core.modules.config.jxb.ExternalField;
import com.queplix.core.modules.config.jxb.ExternalSet;
import com.queplix.core.modules.config.utils.EntityHelper;
import com.queplix.core.modules.eql.error.EQLException;
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.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * This class helps to build bindings among the forms.
 *
 * @author Sergey Kozmin
 * @since 24.11.2006, 12:42:33
 */
public class BoundFormsOperationsHelper {
    private static final AbstractLogger logger = Log.getLog(BoundFormsOperationsHelper.class);

    public static ResultFormStructure getClearBoundFormResult(String formName, LogonSession ls, ActionContext ctx) throws CouldntGetEJBException {
        ResultFormStructure fs = new ResultFormStructure();
        FocusConfigManagerLocal focusManager = ctx.getFocusConfigManager();
        User user = ls.getUser();

        try {
            List<ExternalFieldStructure> externalFieldStructures = getBoundByExternalFields(formName, focusManager, ls, ctx);

            for(ExternalFieldStructure externalField : externalFieldStructures) {
                FieldData[] fieldDatas = new FieldData[]{EntitySerializeHelper.createEmptyFieldData(externalField.fieldMeta, user)};
                EntityData entityData = new EntityData(EntityHelper.getFormEntityName(externalField.formName), -1L, fieldDatas);
                fs.getFieldDatas().add(entityData);
            }
        } catch (Exception e) {
            logger.ERROR("Could not load clearing information. ", e);
        }
        return fs;
    }
    
    public static ResultFormStructure getBoundFormResult(String formName, Long rowID, LogonSession ls, ActionContext ctx) throws CouldntGetEJBException {
        ResultFormStructure fs = new ResultFormStructure();
        FocusConfigManagerLocal focusManager = ctx.getFocusConfigManager();
        //temp object, used to break a linked to circle chain
        Set<String> passedEntities = new HashSet<String>();
        retrieveNextLevelBoundFormResult(formName, EntityOperationsHelper.getEntityNameFromFormID(formName), rowID, fs, passedEntities,
                focusManager, false, ls, ctx);
        return fs;
    }

    private static void retrieveNextLevelBoundFormResult(String formName, String filteringEntityName, Long filteringEntityID,
                                                         ResultFormStructure ret, Set<String> passedEntities,
                                                         FocusConfigManagerLocal focusManager,
                                                         boolean addGridData, LogonSession ls, ActionContext ctx)
            throws CouldntGetEJBException {

        String requestedEntityName = EntityOperationsHelper.getEntityNameFromFormID(formName);
        if (!passedEntities.contains(requestedEntityName)) {
            passedEntities.add(requestedEntityName);
            try {
                IntegratedRecordSet result = EntityFacade.getEntityByIDRequest(requestedEntityName, filteringEntityName, filteringEntityID, ls, ctx);
                //fill main entity data
                if (result.getRowsCount() == 1) {
                    //get form result
                    Map<String, FieldData> dataMap = result.getRecordSet().get(0).getFieldSet();
                    Collection<FieldData> datas = dataMap.values();
                    FieldData[] resultFields = datas.toArray(new FieldData[datas.size()]);
                    String pkeyFieldName = result.getPkeyFieldName();
                    FieldData rowID = dataMap.get(pkeyFieldName);
                    Long pkey = Long.parseLong(EntitySerializeHelper.getValueStringRepresentation(rowID, result.getRecordMeta().get(pkeyFieldName)));
                    ret.addEntityData(new EntityData(requestedEntityName, pkey, resultFields));

                    if(addGridData) {//check, should we add data for grid. 
                        //retrieve data for grid
                        RowData[] records = EntityOperationsHelper.retrieveGridRowsDataFromResult(result, ls, ctx);
                        ret.addGridData(new GridData(records, requestedEntityName));
                    }

                    //go next external sets
                    List<String> boundForms = getBoundByExternalSets(formName, focusManager);
                    for (String boundForm : boundForms) {
                        retrieveNextLevelBoundFormResult(boundForm, requestedEntityName, pkey, ret, passedEntities, focusManager, true, ls, ctx);
                    }
                    //parse external fields
                    List<ExternalFieldStructure> boundFields = getBoundByExternalFields(formName, focusManager, ls, ctx);
                    try {
                        retrieveExternalFieldValue(boundFields, result, ret, requestedEntityName, ls, ctx);
                    } catch (Exception e) {
                        logger.ERROR("Could not load external fields data for entity [" + requestedEntityName + "], with id [" + filteringEntityID + "]", e);
                    }


                } else {//if there is no linked entities with this entity, fill response with empty data.
                    logger.INFO("There is no [" + requestedEntityName + "], entities found linked with entity ["
                            + filteringEntityName + "], with id [" + filteringEntityID + "]. Fill external form with empty values. ");
                    Map<String, FieldMeta> fieldsMeta = result.getRecordMeta();
                    for (String key : fieldsMeta.keySet()) {
                        ret.addFieldData(new EntityData(requestedEntityName, -1L,
                                new FieldData[]{EntitySerializeHelper.createEmptyFieldData(fieldsMeta.get(key), ls.getUser())}));
                    }
                }
            } catch (Exception e) {
                logger.ERROR("Could not find entity data for entity [" + requestedEntityName + "], with id [" + filteringEntityID + "]", e);
            }
        }
    }

    private static void retrieveExternalFieldValue(List<ExternalFieldStructure> boundFields,
                                                   IntegratedRecordSet baseResultSet,
                                                   ResultFormStructure ret,
                                                   String requestedEntityName,
                                                   LogonSession ls,
                                                   ActionContext ctx)
            throws EQLException {
        //go next external fields
        for (ExternalFieldStructure field : boundFields) {

            FieldMeta sourceFieldMeta = baseResultSet.getRecordMeta().get( field.sourceFieldName);
            FieldData sourceFieldData = baseResultSet.getRecordSet().get(0).getFieldSet() .get(field.sourceFieldName);

            Long sourceRowId = EntitySerializeHelper.retrieveRowId( sourceFieldMeta, sourceFieldData);

            if (sourceRowId != null) {//if source field was filled out, otherwise do nothing
                //make request to retrieve entity filter name
                IntegratedRecordSet recordSet = EntityFacade.getEntityByIDRequest(field.referencedEntityName, sourceRowId, ls, ctx);
                if (recordSet.getRowsCount() > 0) {
                    //parse the entity to get listref field value
                    FieldMeta listRefFieldMeta = recordSet.getRecordMeta().get(recordSet.getListRefFieldName());
                    Map<String, FieldData> referencedDataMap = recordSet.getRecordSet().get(0).getFieldSet();
                    FieldData listRefFieldData = referencedDataMap.get(recordSet.getListRefFieldName());
                    String listRefEntityRepresentation = EntitySerializeHelper.getStringRepresentationForGrid(listRefFieldData,
                            listRefFieldMeta, ls, ctx);

                    //create field data for external field according to it's type
                    FieldData fieldData = EntitySerializeHelper.createExternalFieldDataForPkey( field.fieldMeta, sourceRowId, listRefEntityRepresentation);

                    ret.addFieldData(new EntityData( EntityOperationsHelper.getEntityNameFromFormID( field.formName), -1L, new FieldData[]{fieldData}));
                } else {
                    logger.ERROR("Could not find entity ["
                            + field.referencedEntityName +
                            "] with id [" + sourceRowId
                            + "]. Please, check that entity [" +
                            field.referencedEntityName
                            + "] and [" + requestedEntityName +
                            "] are mapped to the same DB table. ");
                }
            }
        }
    }

    private static List<String> getBoundByExternalSets(String formName, FocusConfigManagerLocal focusManager) {

⌨️ 快捷键说明

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