package gov.nih.nci.cananolab.service.common.helper;

import gov.nih.nci.cananolab.domain.common.Keyword;
import gov.nih.nci.cananolab.domain.common.LabFile;
import gov.nih.nci.cananolab.exception.ParticleCharacterizationException;
import gov.nih.nci.cananolab.system.applicationservice.CustomizedApplicationService;
import gov.nih.nci.system.client.ApplicationServiceProvider;
import gov.nih.nci.system.query.hibernate.HQLCriteria;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;

/**
 * Utility service for file retrieving and writing.
 * 
 * @author pansu, tanq
 * 
 */
public class FileServiceHelper {
	Logger logger = Logger.getLogger(FileServiceHelper.class);

	public FileServiceHelper() {
	}

	/**
	 * Load the file for the given fileId from the database
	 * 
	 * @param fileId
	 * @return
	 */
	public LabFile findFile(String fileId) throws Exception {
		CustomizedApplicationService appService = (CustomizedApplicationService) ApplicationServiceProvider
				.getApplicationService();

		DetachedCriteria crit = DetachedCriteria.forClass(LabFile.class).add(
				Property.forName("id").eq(new Long(fileId)));
		List result = appService.query(crit);
		LabFile file = null;
		if (!result.isEmpty()) {
			file = (LabFile) result.get(0);
		}
		return file;
	}

	public Collection<LabFile> findFilesByCompositionInfoId(String id,
			String className) {
		Collection<LabFile> files = new ArrayList<LabFile>();
		//TODO fill in HQL
		return files;
	}

	public Collection<Keyword> findKeywordsByFileId(String labFileId)
			throws ParticleCharacterizationException {
		Collection<Keyword> keywords = new ArrayList<Keyword>();
		try {
			CustomizedApplicationService appService = (CustomizedApplicationService) ApplicationServiceProvider
					.getApplicationService();
			HQLCriteria crit = new HQLCriteria(
					"select labFile.keywordCollection from gov.nih.nci.cananolab.domain.common.LabFile labFile where labFile.id = "
							+ labFileId);
			List results = appService.query(crit);
			for (Object obj : results) {
				Keyword keyword = (Keyword) obj;
				keywords.add(keyword);
			}
		} catch (Exception e) {
			logger.error("Problem to retrieve LabFile keyword.", e);
			throw new ParticleCharacterizationException(
					"Problem to retrieve retrieve LabFile keyword ");
		}
		return keywords;
	}

}

@@ -151,16 +151,70 @@
 abFile;%0A
+import gov.nih.nci.cananolab.exception.FileException;%0A
 import g
@@ -271,32 +271,32 @@
 ationException;%0A
-
 import gov.nih.n
@@ -560,16 +560,70 @@
 il.List;
+%0Aimport java.util.SortedSet;%0Aimport java.util.TreeSet;
 %0A%0Aimport
@@ -1615,101 +1615,763 @@
 me) 
-%7B%0A%09%09Collection%3CLabFile%3E files = new ArrayList%3CLabFile%3E();%0A%09%09//TODO fill in HQL%0A%09%09return files
+throws FileException%7B%0A%09%09SortedSet%3CLabFile%3E labFileCollection = new TreeSet%3CLabFile%3E();%0A%09%09try %7B%0A%09%09%09CustomizedApplicationService appService = (CustomizedApplicationService) ApplicationServiceProvider%0A%09%09%09%09%09.getApplicationService();%0A%09%09%09String hql = %22select anEntity.labFileCollection from %22+className+%0A%09%09%09%09%22 anEntity where anEntity.id = %22+id;%0A%09%09%09%0A%09%09%09HQLCriteria crit = new HQLCriteria(hql);%0A%09%09%09List results = appService.query(crit);%0A%09%09%09for (Object obj : results) %7B%0A%09%09%09%09LabFile labFile = (LabFile) obj;%0A%09%09%09%09labFileCollection.add(labFile);%0A%09%09%09%7D%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09logger%0A%09%09%09%09%09.error(%22Problem to retrieve labFileCollection.%22,%0A%09%09%09%09%09%09%09e);%0A%09%09%09throw new FileException(%0A%09%09%09%09%09%22Problem to retrieve retrieve labFileCollection %22);%0A%09%09%7D%0A%09%09return labFileCollection
 ;%0A%09%7D
@@ -2453,32 +2453,12 @@
 ows 
-ParticleCharacterization
+File
 Exce
@@ -3089,32 +3089,12 @@
 new 
-ParticleCharacterization
+File
 Exce

add HQL in helper