Java tutorial
/** * 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.TSystemStateBean; import com.aurel.track.dao.SystemStateDAO; import com.workingdogs.village.Record; /** * The skeleton for this class was autogenerated by Torque on: * * [Mon Mar 27 12:21:16 EEST 2006] * * 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 TSystemStatePeer extends com.aurel.track.persist.BaseTSystemStatePeer implements SystemStateDAO { /** * */ private static final long serialVersionUID = 7189624378457281833L; private static final Logger LOGGER = LogManager.getLogger(TSystemStatePeer.class); private static Class[] replacePeerClasses = { TProjectPeer.class, TReleasePeer.class, TAccountPeer.class }; private static String[] replaceFields = { TProjectPeer.STATUS, TReleasePeer.STATUS, TAccountPeer.STATUS }; private static Class[] deletePeerClasses = { TSystemStatePeer.class }; private static String[] deleteFields = { TSystemStatePeer.OBJECTID }; /** * Gets a TSystemStateBean by label * @param entityFlag * @param label * @return */ @Override public List<TSystemStateBean> loadByLabel(Integer entityFlag, String label) { Criteria crit = new Criteria(); crit.add(LABEL, label); crit.add(ENTITYFLAG, entityFlag); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Loading the system state " + entityFlag + " by label " + label + " failed with " + e.getMessage()); return null; } } /** * Gets the label beans with a certain symbol * @param listID * @param symbol * @param excludeObjectID * @return */ @Override public List<TSystemStateBean> loadBySymbol(Integer listID, String symbol, Integer excludeObjectID) { Criteria crit = new Criteria(); crit.add(ENTITYFLAG, listID); crit.add(SYMBOL, symbol); if (excludeObjectID != null) { crit.add(OBJECTID, excludeObjectID, Criteria.NOT_EQUAL); } try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Loading the sytem state by icon " + listID + " and icon " + symbol + " failed with " + e.getMessage(), e); return null; } } /** * List of TSystemStatePeer objects by * entityFlag and stateflags * @param entityFlag defines the entity, the state refers to * @param stateFlags * @throws Exception */ @Override public List<TSystemStateBean> loadWithStateFlagForEntity(int entityFlag, int[] stateFlags) { Criteria crit = new Criteria(); crit.add(OBJECTID, new Integer(0), Criteria.GREATER_THAN); crit.add(ENTITYFLAG, entityFlag, Criteria.EQUAL); if (stateFlags != null && stateFlags.length > 0) { crit.addIn(STATEFLAG, stateFlags); } crit.addAscendingOrderByColumn(SORTORDER); crit.addAscendingOrderByColumn(LABEL); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error( "Exception when trying to get the TSystemStates with specific stateFlags and for an entityFlag " + entityFlag + ": " + e.getMessage()); return null; } } /** * Loads a SystemStateBeans by primary key * @param objectID * @return */ @Override public TSystemStateBean loadByPrimaryKey(Integer objectID) { TSystemState systemState = null; try { systemState = retrieveByPK(objectID); } catch (Exception e) { LOGGER.info("Loading the system state by primary key " + objectID + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } if (systemState != null) { return systemState.getBean(); } return null; } /** * List with all available TSystemStatePeer objects * according to the entityFlag * @param entityFlag defines the entity, the state refers to * @throws Exception */ @Override public List<TSystemStateBean> loadAllForEntityFlag(Integer entityFlag) { Criteria crit = new Criteria(); crit.add(BaseTSystemStatePeer.OBJECTID, new Integer(0), Criteria.GREATER_THAN); crit.add(BaseTSystemStatePeer.ENTITYFLAG, entityFlag, Criteria.EQUAL); crit.addAscendingOrderByColumn(BaseTSystemStatePeer.SORTORDER); crit.addAscendingOrderByColumn(BaseTSystemStatePeer.LABEL); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Exception when trying to get all the TSystemStates for an entityFlag " + entityFlag + ": " + e.getMessage()); return null; } } /** * Load by roleIDs * @return */ @Override public List<TSystemStateBean> loadByStateIDs(List<Integer> primaryKeys) { List list = new ArrayList(); if (primaryKeys == null || primaryKeys.isEmpty()) { return list; } Criteria crit = new Criteria(); crit.addIn(OBJECTID, primaryKeys); crit.addAscendingOrderByColumn(SORTORDER); crit.addAscendingOrderByColumn(LABEL); try { list = doSelect(crit); } catch (TorqueException e) { LOGGER.error("Loading the system states by keys " + primaryKeys + " failed with " + e.getMessage()); } return convertTorqueListToBeanList(list); } /** * Get the status flag of the project * @param entityID * @return */ @Override public int getStatusFlag(Integer entityID, int entityFlag) { List systemStates = new ArrayList(); Criteria crit = new Criteria(); switch (entityFlag) { case TSystemStateBean.ENTITYFLAGS.PROJECTSTATE: crit.addJoin(TProjectPeer.STATUS, TSystemStatePeer.OBJECTID); crit.add(TProjectPeer.PKEY, entityID); break; case TSystemStateBean.ENTITYFLAGS.RELEASESTATE: crit.addJoin(TReleasePeer.STATUS, TSystemStatePeer.OBJECTID); crit.add(BaseTReleasePeer.PKEY, entityID); break; case TSystemStateBean.ENTITYFLAGS.ACCOUNTSTATE: crit.addJoin(TAccountPeer.STATUS, TSystemStatePeer.OBJECTID); crit.add(BaseTAccountPeer.OBJECTID, entityID); break; } try { systemStates = doSelect(crit); } catch (TorqueException e) { LOGGER.error("Exception when trying to get the system " + " state flag for the entity " + entityID + " and entity type " + entityFlag + "" + e.getMessage(), e); } if (systemStates.isEmpty()) { return TSystemStateBean.STATEFLAGS.ACTIVE; } TSystemState systemState = (TSystemState) systemStates.get(0); return systemState.getStateflag().intValue(); } /** * Loads all SystemStateBeans * @return */ @Override public List<TSystemStateBean> loadAll() { Criteria crit = new Criteria(); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Loading of all systemStateBeans failed with " + e.getMessage()); return null; } } /** * Gets the next available sortorder * @param entityID * @return */ @Override public Integer getNextSortOrder(Integer entityID) { Integer sortOrder = null; String max = "max(" + SORTORDER + ")"; Criteria crit = new Criteria(); crit.add(ENTITYFLAG, entityID); crit.addSelectColumn(max); try { sortOrder = ((Record) doSelectVillageRecords(crit).get(0)).getValue(1).asIntegerObj(); } catch (Exception e) { LOGGER.error("Getting the next sortorder for severity failed with: " + e); } if (sortOrder == null) { return Integer.valueOf(1); } else { return Integer.valueOf(sortOrder.intValue() + 1); } } /** * Saves a systemState to the TSystemStateBean table. * @param systemStateBean * @return */ @Override public Integer save(TSystemStateBean systemStateBean) { TSystemState tSystemState; try { tSystemState = BaseTSystemState.createTSystemState(systemStateBean); tSystemState.save(); return tSystemState.getObjectID(); } catch (Exception e) { LOGGER.error("Saving of systemState failed with " + e.getMessage()); return null; } } @Override public boolean hasDependentData(Integer pkey) { return ReflectionHelper.hasDependentData(replacePeerClasses, replaceFields, pkey); } /** * This method replaces all occurrences of state value oldOID with * state value newOID. * @param oldOID * @param newOID */ @Override public void replace(Integer oldOID, Integer newOID) { if (newOID != null) { ReflectionHelper.replace(replacePeerClasses, replaceFields, oldOID, newOID); } } /** * Deletes a state from the TState table * @param objectID */ @Override public void delete(Integer objectID) { ReflectionHelper.delete(deletePeerClasses, deleteFields, objectID); } /** * Returns the sort order column name * @return */ @Override public String getSortOrderColumn() { return "SORTORDER"; } /** * Returns the table name * @return */ @Override public String getTableName() { return TABLE_NAME; } private static List<TSystemStateBean> convertTorqueListToBeanList(List<TSystemState> torqueList) { List<TSystemStateBean> beanList = new LinkedList<TSystemStateBean>(); if (torqueList != null) { Iterator<TSystemState> itrTorqueList = torqueList.iterator(); while (itrTorqueList.hasNext()) { beanList.add(itrTorqueList.next().getBean()); } } return beanList; } }