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

com.quamto.jira.data.common.dao.AttachmentDAO Maven / Gradle / Ivy

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

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

import com.quamto.core.QException;
import com.quamto.core.QException.ExceptionType;
import com.quamto.core.Result;
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.common.JEnumerators.PluginEntities;
import com.quamto.jira.data.common.entity.AttachmentEntity;


public class AttachmentDAO extends BaseEntityDAO {

	public AttachmentDAO(DbConnection connectionDB) {
		super(connectionDB, "qjj_attachments", "adj_id", AttachmentDAO.class);
	}
	
	/**
	 * Gets an object of type AttachmentEntitys charged according to the record identifier
	 * 
	 * @param id AttachmentEntitys identifier requested to be loaded
	 * @return Object  loaded with the information related to the AttachmentEntitys requested
	 */
	@Override
	public AttachmentEntity get(Long id) throws QException {
		
		AttachmentEntity entity = null;
		
		try {
			entity = (AttachmentEntity)getEntityLoaded(id);
		} catch (Exception e) {
			throw new QException(e, ExceptionType.OperationFailed_err, subClassName + "-get");
		} 
		return entity;
	}
	
	/**
	 * Gets the list of AttachmentEntitys based on an specific entity
	 * 
	 * @param entityType Type of entity that owns the attachments
	 * @param idEntity Entity Owner Identifier
	 * @return List of attachments associated with the entity ( without attachment only information attached)
	 * @throws QException Throws an exception if the operation did not achieve
	 */
	public List getAll(Long idEntity, PluginEntities entityType) throws QException {
		
		ResultSet rs = null;
		ArrayList attachmentsList = new ArrayList();
		
		try {	
			String sentenceSQL = "SELECT * "
								+ "  FROM " + entityTableName 
								+ " WHERE adj_entity_type=" + entityType.getInt() 
								+ "   AND adj_id_entity=" + idEntity;
			
			rs = getResultSet(sentenceSQL);
			while(rs.next()){
				attachmentsList.add(loadEntityAttributesFromRs(rs));
			}
		} catch (Exception e) {
			throw new QException(e, ExceptionType.OperationFailed_err, subClassName + "-getAll");
		}
		return attachmentsList;
	}

	/**
     * Make the assignment of the fields and values of the entity 
     * to be used in operations insert and update data
     * @param entity BaseEntidad Object containing the data to be loaded
	 * @throws QException Throws an exception in case of failing to load the parameters
     */
	@Override
	protected void loadQueryParameters(BaseEntity entity)
			throws QException {
		
		AttachmentEntity attachments = (AttachmentEntity) entity;
		
		try {
			
			addQueryParameter("adj_attachment_name", attachments.getName(), SQLDataType.String_sdt);
			addQueryParameter("adj_id_entity", attachments.getIdEntity(), SQLDataType.Long_sdt);
			addQueryParameter("adj_entity_type", attachments.getEntityType().getInt(), SQLDataType.Integer_sdt);
			addQueryParameter("adj_upload_date", attachments.getUploadDate(), SQLDataType.DateTime_sdt);
			addQueryParameter("adj_id_user", attachments.getIdUser(), SQLDataType.String_sdt);
		} catch (Exception e) {
			throw new QException(e, ExceptionType.OperationFailed_err, subClassName + "-loadQueryParameters");
		}
	}

	/**
     * ResultSet load values corresponding to the table of the entity in the object returned
     */
	@Override
	protected AttachmentEntity loadEntityAttributesFromRs(ResultSet rs) throws QException {
		
		AttachmentEntity attachments = new AttachmentEntity();
		
		try {
			attachments.setID(rs.getLong(entityIdFieldName));
			attachments.setName(rs.getString("adj_attachment_name"));
			attachments.setIdEntity(rs.getLong("adj_id_entity"));
			attachments.setEntityType(PluginEntities.Projects.getEnumValue(rs.getInt("adj_entity_type")));
			attachments.setUploadDate(rs.getTimestamp("adj_upload_date"));
			attachments.setIdUser(rs.getString("adj_id_user"));
		} catch (Exception e) {
			throw new QException(e, ExceptionType.OperationFailed_err, subClassName + "-loadEntityAttributesFromRs");
		} 
		return attachments;
	}
	
	/**
	 * Returns an unsuccessful result because is not allowed to execute modification on operations attachments
	 */
	@Override
	public Result update(BaseEntity model) {
		return new Result(new QException(ExceptionType.OperationNotAllowed_err));
	}

	@Override
	public void close() throws Exception {

	}

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

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy