All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.quamto.jira.data.time.dao.TeamMembersDAO Maven / Gradle / Ivy

The newest version!
package com.quamto.jira.data.time.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.quamto.core.QException;
import com.quamto.core.QException.ExceptionType;
import com.quamto.db.DbConnection;
import com.quamto.db.SQLParameter.SQLDataType;
import com.quamto.entity.BaseEntity;
import com.quamto.entity.BaseEntityDAO;
import com.quamto.jira.data.time.entity.TeamMembersEntity;

public class TeamMembersDAO extends BaseEntityDAO {

	public TeamMembersDAO(DbConnection dbConnection){
        super(dbConnection, "qji_team_members", "tme_id", TeamMembersDAO.class);
    }

    /**
     * Get an instance of the entity with data loaded according to Id provided
     */
    @Override
    public TeamMembersEntity get(Long id) throws QException {
        TeamMembersEntity entity = null;
        try {
            entity = (TeamMembersEntity)getEntityLoaded(id);
        } catch (Exception e) {
            throw new QException(e, ExceptionType.LoadData_err, subClassName + "-get");
        }
        return entity;
    }
    
    /**
     * Gets list of members related to a team
     * @param idTeam Team identifier to consult its members
     * @return List of Team Members with its Roles
     */
    public List> getAll(Long idTeam) throws QException {
    	List> teamMembersList = null;
    	List> membersRolesList = null;
        try {
        	
            String query = "SELECT * " 
					    + " FROM " + entityTableName 
						+ " WHERE tme_id_team = " + idTeam;
		
            teamMembersList = mapArrayFromQuery(query);
            
            String membersId = "";
            
            for (int i = 0; i < teamMembersList.size(); i++) {
            	if(i == teamMembersList.size() -1 ){
            		membersId += teamMembersList.get(i).get("id");
            	}
            	else{
            		membersId += teamMembersList.get(i).get("id") + ",";
            	}
			}
            
            if(teamMembersList.size() > 0){
            	query = "SELECT * " 
                   	 + " FROM qji_team_members_roles" 
                   	 + " INNER JOIN qji_roles ON mro_id_role = tro_id" 
       				 + " WHERE mro_id_team_member IN ( " + membersId +" )";
            	
                membersRolesList = mapArrayFromQuery(query);
                
                for (int i = 0; i < teamMembersList.size(); i++) { 
                	ArrayList> roles = new ArrayList>();
                	
                    for (int j = 0; j < membersRolesList.size(); j++) {
                    	if(teamMembersList.get(i).get("id").equals(
                				membersRolesList.get(j).get("idTeamMember") ) ){
                			roles.add(membersRolesList.get(j));
                		}
					}
                    Map item = teamMembersList.get(i);
                    item.put("roles", roles);
                    teamMembersList.set(i,  item);
    			}
            }
                
        } catch (Exception e) {
            throw new QException(e, ExceptionType.LoadData_err, subClassName + "-getAll");
        }
        return teamMembersList;
    }

    /**
     * Make assignments of fields and values of entity to be used in 
     * insertion and update database operations
     */
    @Override
    public void loadQueryParameters(BaseEntity entity)
            throws QException {
        try {
            TeamMembersEntity ent = (TeamMembersEntity) entity;
            addQueryParameter(entityIdFieldName, ent.getID(), SQLDataType.Long_sdt);
            addQueryParameter("tme_id_team", ent.getIdTeam(), SQLDataType.Long_sdt);
            addQueryParameter("tme_id_user", ent.getIdUser(), SQLDataType.String_sdt);
            addQueryParameter("tme_availability", ent.getAvailability(), SQLDataType.Decimal_sdt);
        } catch (Exception e) {
            throw new QException(e, ExceptionType.LoadData_err, subClassName + "-loadQueryParameters");
        }
    }

    /**
     * Load the values of ResultSet of table from entity in to returned object.
     */
    @Override
    public TeamMembersEntity loadEntityAttributesFromRs(ResultSet rs) throws QException {
        TeamMembersEntity entity = new TeamMembersEntity();
        try { 
        	entity.setID(rs.getLong(entityIdFieldName));
        	entity.setIdTeam(rs.getLong("tme_id_team"));
        	entity.setIdUser(rs.getString("tme_id_user"));
        	entity.setAvailability(rs.getDouble("tme_availability"));
        } catch (Exception e) {
            throw new QException(e, ExceptionType.LoadData_err, subClassName + "-loadEntityAttributesFromRs");
        }
        return entity;
    }

	@Override
	public void close() throws Exception {
		
	}

	@Override
	public List getAll() throws QException {
		return null;
	}	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy