com.aurel.track.persist.TAccountPeer.java Source code

Java tutorial

Introduction

Here is the source code for com.aurel.track.persist.TAccountPeer.java

Source

/**
 * Genji Scrum Tool and Issue Tracker
 * Copyright (C) 2015 Steinbeis GmbH & Co. KG Task Management Solutions
    
 * <a href="http://www.trackplus.com">Genji Scrum Tool</a>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/* $Id:$ */

package com.aurel.track.persist;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.torque.TorqueException;
import org.apache.torque.util.Criteria;

import com.aurel.track.beans.TAccountBean;
import com.aurel.track.dao.AccountDAO;
import com.aurel.track.util.GeneralUtils;

/**
 * The skeleton for this class was autogenerated by Torque on:
 *
 * [Tue Jun 15 21:31:34 CEST 2004]
 *
 *  You should add additional methods to this class to meet the
 *  application requirements.  This class will only be generated as
 *  long as it does not already exist in the output directory.
 */
public class TAccountPeer extends com.aurel.track.persist.BaseTAccountPeer implements AccountDAO {
    private static final long serialVersionUID = -438138422430189242L;

    private static final Logger LOGGER = LogManager.getLogger(TAccountPeer.class);

    private static Class[] replacePeerClasses = { TCostPeer.class };

    private static String[] replaceFields = { TCostPeer.ACCOUNT };

    private static Class[] deletePeerClasses = { TProjectAccountPeer.class, TAccountPeer.class };

    private static String[] deleteFields = { TProjectAccountPeer.ACCOUNT, TAccountPeer.OBJECTID };

    /**
     * Loads a AccountBean by objectID 
     * @param objectIDs
     * @return
     */
    @Override
    public TAccountBean loadByPrimaryKey(Integer objectID) {
        TAccount tAccount = null;
        try {
            tAccount = retrieveByPK(objectID);
        } catch (Exception e) {
            LOGGER.info("Loading the account by primary key " + objectID + " failed with " + e.getMessage());
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
        if (tAccount != null) {
            return tAccount.getBean();
        }
        return null;
    }

    /**
     * Loads accounts by costcenter and label
     * @param costcenterID
     * @param accountNumber
     * @return
     */
    @Override
    public List<TAccountBean> loadByNumber(Integer costcenterID, String accountNumber) {
        Criteria crit = new Criteria();
        crit.add(ACCOUNTNUMBER, accountNumber);
        crit.add(COSTCENTER, costcenterID);
        try {
            return convertTorqueListToBeanList(doSelect(crit));
        } catch (Exception e) {
            LOGGER.error("Getting the accounts by costcenterID " + costcenterID + " and accountNumber "
                    + accountNumber + " failed with " + e);
            return null;
        }
    }

    /**
     * Gets all accounts
     * @return
     */
    @Override
    public List loadAll() {
        Criteria crit = new Criteria();
        crit.add(OBJECTID, 0, Criteria.GREATER_THAN);
        crit.addAscendingOrderByColumn(ACCOUNTNUMBER);
        try {
            return convertTorqueListToBeanList(doSelect(crit));
        } catch (TorqueException e) {
            LOGGER.error("Loading all accounts failed with " + e.getMessage());
            return null;
        }
    }

    /**
     * Gets all accounts for a costcenter
     * @return
     */
    @Override
    public List<TAccountBean> loadByCostcenter(Integer costcenterID) {
        Criteria crit = new Criteria();
        crit.add(COSTCENTER, costcenterID);
        crit.addAscendingOrderByColumn(ACCOUNTNUMBER);
        try {
            return convertTorqueListToBeanList(doSelect(crit));
        } catch (Exception e) {
            LOGGER.error("Loading the accounts for costcenter " + costcenterID + " failed with " + e);
            return null;
        }

    }

    /**
     * Loads a list of {@link TAccountBean} by costcenters
     * @param costcenters
     * @return
     */
    @Override
    public List<TAccountBean> loadByCostcenters(Integer[] costcenterKeys) {
        List torqueList = new ArrayList();
        if (costcenterKeys != null && costcenterKeys.length > 0) {
            Criteria crit = new Criteria();
            crit.addIn(COSTCENTER, costcenterKeys);
            crit.addAscendingOrderByColumn(ACCOUNTNUMBER);
            try {
                torqueList = doSelect(crit);
            } catch (Exception e) {
                LOGGER.error("Loading the accounts by costcenter keys " + costcenterKeys + " failed with " + e);
            }
        }
        return convertTorqueListToBeanList(torqueList);
    }

    /**
     * Loads a list of AccountBeans by objectID list 
     * @param objectIDs
     * @return
     */
    @Override
    public List<TAccountBean> loadByKeys(List<Integer> objectIDs) {
        if (objectIDs != null && !objectIDs.isEmpty()) {
            Criteria crit = new Criteria();
            crit.addIn(OBJECTID, objectIDs);
            crit.addAscendingOrderByColumn(ACCOUNTNUMBER);
            try {
                return convertTorqueListToBeanList(doSelect(crit));
            } catch (Exception e) {
                LOGGER.error("Loading the accounts by keys " + objectIDs + " failed with " + e);
                return null;
            }
        }
        return new LinkedList<TAccountBean>();
    }

    @Override
    public List<TAccountBean> loadByProjectForStateflag(Integer project, int stateflag) {
        Criteria crit = new Criteria();
        crit.addJoin(OBJECTID, BaseTProjectAccountPeer.ACCOUNT);
        crit.addJoin(STATUS, BaseTSystemStatePeer.OBJECTID);
        crit.add(BaseTSystemStatePeer.STATEFLAG, stateflag);
        crit.add(BaseTProjectAccountPeer.PROJECT, project);
        crit.addAscendingOrderByColumn(ACCOUNTNUMBER);
        try {
            return convertTorqueListToBeanList(doSelect(crit));
        } catch (Exception e) {
            LOGGER.error("Loading the accounts for project " + project + " and stateflag " + stateflag
                    + " failed with " + e);
            return null;
        }
    }

    /**
     * Loads the accounts with a specific stateflag
     * @param stateflag
     * @return list of {@link TAccountBean} beans in that project which match the given stateflag
     */
    @Override
    public List<TAccountBean> loadByStateflag(int stateflag) {
        Criteria crit = new Criteria();
        crit.addJoin(STATUS, TSystemStatePeer.OBJECTID);
        crit.add(TSystemStatePeer.STATEFLAG, stateflag);
        crit.addAscendingOrderByColumn(ACCOUNTNUMBER);
        try {
            return convertTorqueListToBeanList(doSelect(crit));
        } catch (Exception e) {
            LOGGER.error("Loading the accounts for stateflag " + stateflag + " failed with " + e);
            return null;
        }
    }

    /**
     * Loads the TAccountBean list by workItemKeys
     * @param workItemKeys
     * @return
     */
    @Override
    public List<TAccountBean> loadByWorkItemKeys(int[] workItemKeys) {
        List accounts = new ArrayList();
        if (workItemKeys == null || workItemKeys.length == 0) {
            return accounts;
        }
        Criteria criteria;
        List workItemIDChunksList = GeneralUtils.getListOfChunks(workItemKeys);
        if (workItemIDChunksList == null) {
            return accounts;
        }
        Iterator iterator = workItemIDChunksList.iterator();
        while (iterator.hasNext()) {
            int[] workItemIDChunk = (int[]) iterator.next();
            criteria = new Criteria();
            criteria.addJoin(BaseTCostPeer.ACCOUNT, OBJECTID);
            criteria.addIn(BaseTCostPeer.WORKITEM, workItemIDChunk);
            criteria.setDistinct();
            try {
                accounts.addAll(doSelect(criteria));
            } catch (Exception e) {
                LOGGER.error("Loading the accounts by workItemKeys failed with " + e.getMessage());
            }
        }
        return convertTorqueListToBeanList(accounts);
    }

    /**
     * Saves an account in the TAccount table
     * @param fieldBean
     * @return
     */
    @Override
    public Integer save(TAccountBean accountBean) {
        TAccount tAccount;
        try {
            tAccount = BaseTAccount.createTAccount(accountBean);
            tAccount.save();
            return tAccount.getObjectID();
        } catch (Exception e) {
            LOGGER.error("Saving of account failed with " + e.getMessage());
            return null;
        }
    }

    /**
     * Deletes an account from the database
     * @param objectID
     * @return
     */
    @Override
    public void delete(Integer objectID) {
        ReflectionHelper.delete(deletePeerClasses, deleteFields, objectID);
    }

    /**
     * Returns whether the account has dependent data
     * @param objectID 
     * @return
     */
    @Override
    public boolean hasDependentData(Integer pkey) {
        return ReflectionHelper.hasDependentData(replacePeerClasses, replaceFields, pkey);
    }

    /**
     * This method replaces all occurrences of <code>Account</code> 
     * value oldOID with value newOID going through all related
     * tables in the database.
     * @param oldOID object identifier of account to be replaced
     * @param newOID object identifier of replacement account
     */
    @Override
    public void replaceAndDelete(Integer oldOID, Integer newOID) {
        if (newOID != null) {
            ReflectionHelper.replace(replacePeerClasses, replaceFields, oldOID, newOID);
        }
        ReflectionHelper.delete(deletePeerClasses, deleteFields, oldOID);
    }

    /**
     * Returns a list of accountBeans according to a preconfigured criteria
     * It is not a member of the AccountDAO interface because the parameter is a torque specific object,
     * it should be used as helper method from other torque persisten classes  
     * @param criteria
     * @return
     */

    private List<TAccountBean> convertTorqueListToBeanList(List torqueList) {
        List<TAccountBean> beanList = new ArrayList();
        if (torqueList != null) {
            Iterator itrTorqueList = torqueList.iterator();
            while (itrTorqueList.hasNext()) {
                TAccount tAccount = (TAccount) itrTorqueList.next();
                beanList.add(tAccount.getBean());
            }
        }
        return beanList;
    }

}