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.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.TRoleBean; import com.aurel.track.dao.RoleDAO; /** * The skeleton for this class was autogenerated by Torque on: * * [Fri Jun 14 10:06:48 GMT+02:00 2002] * * 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 TRolePeer extends com.aurel.track.persist.BaseTRolePeer implements RoleDAO { private static final long serialVersionUID = -8651495670074410073L; private static final Logger LOGGER = LogManager.getLogger(TRolePeer.class); private static Class[] deletePeerClasses = { TAccessControlListPeer.class, TWorkFlowRolePeer.class, TRoleListTypePeer.class, TRoleFieldPeer.class, TConfigOptionsRolePeer.class, TWorkflowGuardPeer.class, TPRolePeer.class, TRolePeer.class }; private static String[] deleteFields = { TAccessControlListPeer.ROLEKEY, TWorkFlowRolePeer.MAYCHANGEROLE, TRoleListTypePeer.PROLE, TRoleFieldPeer.ROLEKEY, TConfigOptionsRolePeer.ROLEKEY, TWorkflowGuardPeer.ROLEKEY, TPRolePeer.ROLEKEY, TRolePeer.PKEY }; /************************************************************************ *************************Bean methods*********************************** ************************************************************************/ /** * Loads a roleBean by primaty key * @return */ @Override public TRoleBean loadByPrimaryKey(Integer objectID) { TRole tRole = null; try { tRole = retrieveByPK(objectID); } catch (Exception e) { LOGGER.warn("Loading of a role by primary key " + objectID + " failed with " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } if (tRole != null) { return tRole.getBean(); } return null; } /** * Loads a roleBean by label * Might be used in Groowy scripts * @param label * @return */ @Override public TRoleBean loadByName(String label) { if (label == null) { return null; } List<TRole> roles = null; Criteria crit = new Criteria(); crit.add(LABEL, label); try { roles = doSelect(crit); } catch (Exception e) { LOGGER.error("Loading all roles failed with " + e.getMessage()); } if (roles != null && !roles.isEmpty()) { return ((TRole) roles.get(0)).getBean(); } return null; } /** * Loads all RoleBeans * @return */ @Override public List<TRoleBean> loadAll() { Criteria crit = new Criteria(); crit.addAscendingOrderByColumn(LABEL); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Loading all roles failed with " + e.getMessage()); return null; } } /** * Loads all visible RoleBeans * @return */ @Override public List<TRoleBean> loadVisible() { Criteria crit = new Criteria(); crit.add(PKEY, 0, Criteria.GREATER_THAN); crit.addAscendingOrderByColumn(LABEL); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Loading all visisble roles failed with " + e.getMessage()); return null; } } /** * Loads all not visible RoleBeans * @return */ @Override public List<TRoleBean> loadNotVisible() { Criteria crit = new Criteria(); crit.add(PKEY, 0, Criteria.LESS_THAN); crit.addAscendingOrderByColumn(LABEL); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Loading all not visible roles failed with " + e.getMessage()); return null; } } /** * Load by roleIDs * @return */ @Override public List<TRoleBean> loadByRoleIDs(List<Integer> primaryKeys) { if (primaryKeys == null || primaryKeys.isEmpty()) { return new LinkedList<TRoleBean>(); } Criteria crit = new Criteria(); crit.addIn(PKEY, primaryKeys); crit.addAscendingOrderByColumn(LABEL); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (TorqueException e) { LOGGER.error("Loading the roles by keys " + primaryKeys + " failed with " + e.getMessage()); return null; } } /** * Load the roles which have explicit issueType(s) assignments * @param roleIDs * @return */ @Override public List<TRoleBean> loadWithExplicitIssueType(Object[] roleIDs) { if (roleIDs == null || roleIDs.length == 0) { return new LinkedList<TRoleBean>(); } Criteria crit = new Criteria(); //join because at least one issue type assignment is needed crit.addJoin(PKEY, TRoleListTypePeer.PROLE); crit.addIn(PKEY, roleIDs); //get only the distinct roles crit.setDistinct(); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Getting the roles with list type restrictions for " + roleIDs.length + " failed with " + e.getMessage()); return null; } } /** * Load the roles which have explicit field restrictions * @param roleIDs * @return */ @Override public List<TRoleBean> loadWithExplicitFieldRestrictions(Object[] roleIDs) { if (roleIDs == null || roleIDs.length == 0) { return new LinkedList<TRoleBean>(); } Criteria crit = new Criteria(); crit.addJoin(PKEY, TRoleFieldPeer.ROLEKEY); crit.addIn(PKEY, roleIDs); //get only the distinct roles crit.setDistinct(); try { return convertTorqueListToBeanList(doSelect(crit)); } catch (Exception e) { LOGGER.error("Getting the roles with field restrictions for " + roleIDs.length + " failed with " + e.getMessage()); return null; } } /** * Gets the roles needed for any state transition from stateFrom for projectType and issueType * @param projectType * @param issueType * @param stateFrom */ @Override public List<TRoleBean> getRolesForTransition(Integer projectType, Integer issueType, Integer stateFrom) { if (projectType == null || issueType == null || stateFrom == null) { LOGGER.error("All the following fields should have valid values: " + "projectType=" + projectType + " issueType=" + issueType + " stateFrom=" + stateFrom); return new LinkedList<TRoleBean>(); } //load the project for getting the projectType Criteria criteria = new Criteria(); criteria.add(BaseTWorkFlowPeer.STATEFROM, stateFrom); criteria.add(BaseTWorkFlowPeer.PROJECTTYPE, projectType); criteria.addJoin(BaseTWorkFlowPeer.OBJECTID, BaseTWorkFlowCategoryPeer.WORKFLOW); criteria.add(BaseTWorkFlowCategoryPeer.CATEGORY, issueType); criteria.addJoin(BaseTWorkFlowPeer.OBJECTID, BaseTWorkFlowRolePeer.WORKFLOW); criteria.addJoin(BaseTWorkFlowRolePeer.MAYCHANGEROLE, BaseTRolePeer.PKEY); //criteria.add(TAccessControlListPeer.PROJKEY, project); //criteria.addIn(TAccessControlListPeer.PERSONKEY, AccessBeans.getGroupsAndSelf(person)); criteria.setDistinct(); try { return convertTorqueListToBeanList(doSelect(criteria)); } catch (Exception e) { LOGGER.error("Getting the workflows for projectType " + projectType + ", issueType " + issueType + ", stateFrom " + stateFrom + " failed with " + e.getMessage(), e); return null; } } /** * Saves a new/existing roleBean * @param roleBean * @param defaultLocale whether to save as default locale * @return the created roleID */ @Override public Integer save(TRoleBean roleBean) { try { TRole tTole = BaseTRole.createTRole(roleBean); tTole.save(); return tTole.getObjectID(); } catch (Exception e) { LOGGER.error("Saving of a role failed with " + e.getMessage()); return null; } } /** * Deletes a roleBean * @param objectID */ @Override public void delete(Integer objectID) { ReflectionHelper.delete(deletePeerClasses, deleteFields, objectID); } private static List<TRoleBean> convertTorqueListToBeanList(List<TRole> torqueList) { List<TRoleBean> beanList = new LinkedList<TRoleBean>(); if (torqueList != null) { Iterator<TRole> itrTorqueList = torqueList.iterator(); while (itrTorqueList.hasNext()) { beanList.add(itrTorqueList.next().getBean()); } } return beanList; } }