com.aimluck.eip.gpdb.GpdbRecordSelectData.java Source code

Java tutorial

Introduction

Here is the source code for com.aimluck.eip.gpdb.GpdbRecordSelectData.java

Source

/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2015 Aimluck,Inc.
 * http://www.aipo.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aimluck.eip.gpdb;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;

import org.apache.commons.lang.StringUtils;
import org.apache.jetspeed.portal.portlets.VelocityPortlet;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.turbine.util.RunData;
import org.apache.velocity.context.Context;

import com.aimluck.eip.cayenne.om.portlet.EipTGpdb;
import com.aimluck.eip.cayenne.om.portlet.EipTGpdbItem;
import com.aimluck.eip.cayenne.om.portlet.EipTGpdbRecord;
import com.aimluck.eip.common.ALAbstractSelectData;
import com.aimluck.eip.common.ALDBErrorException;
import com.aimluck.eip.common.ALData;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.common.ALPageNotFoundException;
import com.aimluck.eip.gpdb.util.GpdbUtils;
import com.aimluck.eip.modules.actions.common.ALAction;
import com.aimluck.eip.orm.Database;
import com.aimluck.eip.orm.query.ResultList;
import com.aimluck.eip.orm.query.SQLTemplate;
import com.aimluck.eip.util.ALEipUtils;

/**
 * Web????? <BR>
 * 
 */
public class GpdbRecordSelectData extends ALAbstractSelectData<EipTGpdbRecord, EipTGpdbRecord> implements ALData {

    /** logger */
    private static final JetspeedLogger logger = JetspeedLogFactoryService
            .getLogger(GpdbRecordSelectData.class.getName());

    /** ??? */
    private List<GpdbItemResultData> gpdbItemList;

    /** WebID */
    private String gpdbId;

    /** Web */
    private GpdbResultData gpdb;

    /** Web */
    private List<GpdbResultData> gpdbAllList;

    /** WebID */
    private Map<String, GpdbRecordResultData> mapGpdbRecord;

    /** ID */
    private Map<String, List<GpdbKubunValueResultData>> mapGpdbKubunValue;

    /**
     * ?
     * 
     * @param action
     *          ALAction
     * @param rundata
     *          RunData
     * @param context
     *          Context
     */
    @Override
    public void init(ALAction action, RunData rundata, Context context)
            throws ALPageNotFoundException, ALDBErrorException {
        String sort = ALEipUtils.getTemp(rundata, context, LIST_SORT_STR);
        if (sort == null || "".equals(sort.trim())) {
            ALEipUtils.setTemp(rundata, context, LIST_SORT_STR, GpdbUtils.SORT_STRING);
        }

        super.init(action, rundata, context);

        // super.init()?LIST_FILTER_STR???
        // gpdbId = ALEipUtils.getTemp(rundata, context, LIST_FILTER_STR);
    }

    /**
     * ????
     * 
     * @param rundata
     *          RunData
     * @param context
     *          Context
     * @return ResultList
     */
    @Override
    protected ResultList<EipTGpdbRecord> selectList(RunData rundata, Context context) {
        try {

            // Web
            gpdbAllList = GpdbUtils.getGpdbAllList();
            // 
            setCurrentFilterAndSort(rundata, context);
            if (gpdbAllList == null || gpdbAllList.size() == 0) {
                // 
                return null;
            }

            // -----------------------
            // ?
            // -----------------------
            gpdbItemList = GpdbUtils.getGpdbItemResultList(gpdbId);

            // db?
            {
                EipTGpdb etgpdb = GpdbUtils.getEipTGpdb(gpdbId);
                if (etgpdb != null) {
                    gpdb = GpdbUtils.getGpdbResultData(etgpdb);
                }
            }

            // -----------------------
            // ?
            // -----------------------

            Integer sort;
            try {
                sort = Integer.valueOf(current_sort); // ?ID???
            } catch (Exception e) {
                sort = null;
            }

            if (sort == null) {
                // ?
                for (GpdbItemResultData item : gpdbItemList) {
                    if (GpdbUtils.FLG_ON.equals(item.getDefaultSortFlg().getValue())) {
                        sort = (int) item.getGpdbItemId().getValue();
                        current_sort_type = item.getAscDesc().getValue();
                    }
                }
            }

            String sortWhere;
            String sortValue;
            String nullSort = "";
            if (sort == null) {

                // ?????
                // ???????
                sortWhere = " AND r2.gpdb_item_id = (" + "       SELECT r3.gpdb_item_id"
                        + "         FROM eip_t_gpdb_record r3" + "        WHERE r3.record_no = r2.record_no"
                        + "        ORDER BY update_date DESC" + "        LIMIT 1" + "     )";
                sortValue = "r2.update_date";

            } else {
                // ?????
                sortWhere = " AND r2.gpdb_item_id = #bind($sort) ";

                EipTGpdbItem item = GpdbUtils.getEipTGpdbItem("" + sort);
                if (item != null && GpdbUtils.ITEM_TYPE_SEQ.equals(item.getType())) {
                    // ???????
                    sortValue = Database.castToIntRawColumn("r2.value");
                    nullSort = " IS NULL ";
                } else {
                    sortValue = "r2.value";
                    nullSort = " = '' ";
                }
            }

            String ascDesc;
            if (current_sort_type == null) {
                ascDesc = "DESC";
            } else {
                ascDesc = current_sort_type;
            }

            // -----------------------
            // ?
            // -----------------------
            StringBuilder sql = new StringBuilder().append("SELECT r.* FROM eip_t_gpdb_record r")
                    .append(" INNER JOIN eip_t_gpdb_item i").append("    ON i.gpdb_item_id = r.gpdb_item_id")
                    .append(" INNER JOIN eip_t_gpdb g").append("    ON g.gpdb_id = r.gpdb_id")
                    .append(" WHERE r.gpdb_id = #bind($gpdb_id)").append("   AND i.list_flg = #bind($list_flg)")
                    .append(" ORDER BY (SELECT " + sortValue).append("             FROM eip_t_gpdb_record r2")
                    .append("            WHERE r2.record_no = r.record_no").append(sortWhere)
                    .append("          ) " + nullSort + ascDesc).append("        , (SELECT " + sortValue)
                    .append("             FROM eip_t_gpdb_record r2")
                    .append("            WHERE r2.record_no = r.record_no").append(sortWhere)
                    .append("          ) " + ascDesc).append("        , r.record_no")
                    .append("        , i.order_no");

            SQLTemplate<EipTGpdbRecord> sqltemp = Database.sql(EipTGpdbRecord.class, String.valueOf(sql));
            sqltemp.param("gpdb_id", Integer.valueOf(gpdbId));
            sqltemp.param("list_flg", GpdbUtils.FLG_ON);
            if (sort != null) {
                sqltemp.param("sort", sort);
                sqltemp.param("sort", sort);
            }

            ResultList<EipTGpdbRecord> list = new ResultList<EipTGpdbRecord>(sqltemp.fetchList());

            // ??
            return new ResultList<EipTGpdbRecord>(buildPaginatedList(list));
        } catch (RuntimeException ex) {
            logger.error("RuntimeException", ex);
            return null;
        } catch (Exception ex) {
            logger.error("Exception", ex);
            return null;
        }
    }

    /**
     * ????
     * 
     * @param rundata
     *          RunData
     * @param context
     *          Context
     */
    private void setCurrentFilterAndSort(RunData rundata, Context context) {
        // buildSelectQueryForFilter???????
        current_filter = ALEipUtils.getTemp(rundata, context, LIST_FILTER_STR);
        current_filter_type = ALEipUtils.getTemp(rundata, context, LIST_FILTER_TYPE_STR);

        // buildSelectQueryForListViewSort???????
        current_sort = ALEipUtils.getTemp(rundata, context, LIST_SORT_STR);
        current_sort_type = ALEipUtils.getTemp(rundata, context, LIST_SORT_TYPE_STR);
    }

    /**
     * ??????
     * 
     * @param records
     *          ?
     */
    @Override
    protected List<EipTGpdbRecord> buildPaginatedList(List<EipTGpdbRecord> records) {
        List<EipTGpdbRecord> list = new ArrayList<EipTGpdbRecord>();

        // ???
        int countDisp = 0;
        for (GpdbItemResultData item : gpdbItemList) {
            if (GpdbUtils.FLG_ON.equals(item.getListFlg().getValue())) {
                countDisp++;
            }
        }
        int size = records.size(); // 

        // ???
        setPageParam(size / countDisp);

        int start = getStart() * countDisp; // 
        int rowNum = getRowsNum() * countDisp; // 

        // ??
        int end = (start + rowNum <= size) ? start + rowNum : size;

        for (int i = start; i < end; i++) {
            list.add(records.get(i));
        }

        return list;
    }

    /**
     * ????
     * 
     * @param rundata
     *          RunData
     * @param context
     *          Context
     * @return 
     * @throws ALPageNotFoundException
     */
    @Override
    protected EipTGpdbRecord selectDetail(RunData rundata, Context context) throws ALPageNotFoundException {
        // 
        setCurrentFilterAndSort(rundata, context);

        // ?
        mapGpdbKubunValue = GpdbUtils.getKubunMap();

        // ?
        mapGpdbRecord = GpdbUtils.getGpdbRecordMap(gpdbId,
                ALEipUtils.getTemp(rundata, context, ALEipConstants.ENTITY_ID));

        // ?()
        // ?getResultData??
        return new EipTGpdbRecord();
    }

    /**
     * ResultData????
     * 
     * @param record
     *          
     * @return ResultData
     */
    @Override
    protected Object getResultData(EipTGpdbRecord record) {
        return GpdbUtils.getGpdbRecordResultData(record);
    }

    /**
     * ResultData????
     * 
     * @param record
     *          
     * @return ResultData
     */
    @Override
    protected List<GpdbItemResultData> getResultDataDetail(EipTGpdbRecord record) {
        List<GpdbItemResultData> list = GpdbUtils.getGpdbItemResultList(gpdbId);

        // ?
        for (GpdbItemResultData item : list) {

            String type = item.getType().getValue();
            if (!GpdbUtils.ITEM_TYPE_FILE.equals(type) && !GpdbUtils.ITEM_TYPE_IMAGE.equals(type)) {
                continue;
            }

            GpdbRecordResultData data = mapGpdbRecord.get("" + item.getGpdbItemId().getValue());

            data.setAttachmentFiles(GpdbUtils.getAttachmentFiles((int) data.getGpdbRecordId().getValue()));
        }

        return list;
    }

    /**
     * ??
     * 
     * @return 
     */
    @Override
    protected Attributes getColumnMap() {
        Attributes map = new Attributes();
        map.putValue("gpdb_id", EipTGpdb.GPDB_ID_PK_COLUMN);
        return map;
    }

    /**
     * WebID??
     * 
     * @return WebID
     */
    public String getGpdbId() {
        return gpdbId;
    }

    /**
     * WebID?
     * 
     */

    public void setGpdbId(Context context, RunData rundata) {
        String gpdbId = rundata.getParameters().getString("filter", "");
        String sesGpdbId = ALEipUtils.getTemp(rundata, context, LIST_FILTER_STR);
        if ("".equals(gpdbId)) {
            gpdbId = null == sesGpdbId ? "" : sesGpdbId;
        }
        String filterType = rundata.getParameters().getString("filtertype", "");

        List<GpdbResultData> gpdbAllList = GpdbUtils.getGpdbAllList();
        if (!"".equals(gpdbId) && ("gpdb_id".equals(filterType) || filterType == null || "".equals(filterType))) { // ????????
            if (validateGpdbId(gpdbAllList, gpdbId)) {
                this.gpdbId = gpdbId;
            }
        }

        if (this.gpdbId == null) {
            VelocityPortlet portlet = ALEipUtils.getPortlet(rundata, context);
            String database = portlet.getPortletConfig().getInitParameter("p1d-database", "");
            if (!database.isEmpty() && StringUtils.isNumeric(database)) {
                if (validateGpdbId(gpdbAllList, database)) {
                    this.gpdbId = database;
                }
            }

            if (this.gpdbId == null && gpdbAllList.size() > 0) {// ????????
                this.gpdbId = gpdbAllList.get(0).gpdb_id.toString();
            }
        }

        GpdbUtils.passPSML(rundata, context, "p1d-database", this.gpdbId);
        ALEipUtils.setTemp(rundata, context, LIST_FILTER_STR, this.gpdbId);
    }

    /**
     * Web??
     * 
     * @return Web
     */
    public List<GpdbResultData> getGpdbAllList() {
        return gpdbAllList;
    }

    /**
     * ????
     * 
     * @return 
     */
    public List<GpdbItemResultData> getGpdbItemList() {
        return gpdbItemList;
    }

    /**
     * ????
     * 
     * @return 
     */
    public GpdbResultData getGpdb() {
        return gpdb;
    }

    /**
     * ??ID?????
     * 
     * @param gpdbItemId
     *          ID
     * @return 
     */
    public GpdbRecordResultData getRecordResult(String gpdbItemId) {
        return mapGpdbRecord.get(gpdbItemId);
    }

    /**
     * ?????
     * 
     * @param kubunId
     *          ID
     * @return 
     */
    public List<GpdbKubunValueResultData> getKubunValueList(String kubunId) {
        return mapGpdbKubunValue.get(kubunId);
    }

    private boolean validateGpdbId(List<GpdbResultData> allList, String gpdbId) {
        for (GpdbResultData gpdb : allList) {
            if (gpdb.getGpdbId().toString().equals(gpdbId)) {
                return true;
            }
        }
        return false;
    }
}