package gov.nih.nci.calab.service.workflow;

import gov.nih.nci.calab.db.DataAccessProxy;
import gov.nih.nci.calab.db.IDataAccess;
import gov.nih.nci.calab.domain.Aliquot;
import gov.nih.nci.calab.domain.DataStatus;
import gov.nih.nci.calab.service.util.CalabConstants;

import java.util.List;

/**
 * Generalizes Mask functionality for masking Aliquot, File, etc.  
 * @author doswellj
 * @param strType Type of Mask (e.g., aliquot, file, run, etc.)
 * @param strId	  The id associated to the type
 * @param strDescription The mask description associated to the mask type and Id. 
 *
 */
public class MaskService 
{
    
	//This functionality is pending the completed Object Model
	public void setMask(String strType, String strId, String strDescription) throws Exception
    {
        IDataAccess ida = (new DataAccessProxy()).getInstance(IDataAccess.HIBERNATE);

		try {
	        ida.open();
	        
	        if (strType.equals("aliquot"))
	        {
	            //TODO Find Aliquot record based on the strID
				List aliquots = ida.search("from Aliquot aliquot where aliquot.name='" + strId + "'");
				Aliquot aliquot = (Aliquot)aliquots.get(0);
				DataStatus maskStatus = new DataStatus();
				maskStatus.setReason(strDescription);
				maskStatus.setStatus(CalabConstants.MASK_STATUS);
				
				aliquot.setDataStatus(maskStatus);
				ida.store(aliquot); 	        	
	        }
	        if (strType.equals("file"))
	        {
	        	//TODO Find File record based on the its strID
	        	
	        	//TODO Set File Status record to "Masked". 
	        }
			ida.close();
		}catch (Exception e) {
			
			ida.rollback();
			ida.close();
			
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
        	
        	 
    }
    
}

@@ -209,16 +209,57 @@
 Status;%0A
+import gov.nih.nci.calab.domain.LabFile;%0A
 import g
@@ -308,30 +308,57 @@
 ts;%0A
-%0A
 import 
-java.util.List
+gov.nih.nci.calab.service.util.StringUtils
 ;%0A%0A/
@@ -1076,16 +1076,18 @@
 e strID%0A
+//
 %09%09%09%09List
@@ -1204,22 +1204,64 @@
 uot)
-aliquots.get(0
+ida.load(Aliquot.class, StringUtils.convertToLong(strId)
 );%0A%09
@@ -1401,16 +1401,50 @@
 TATUS);%0A
+%09%09%09%09ida.createObject(maskStatus);%0A
 %09%09%09%09%0A%09%09%09
@@ -1578,128 +1578,342 @@
  %7B%0A%09
-        %09//TODO Find File record based on the its strID%0A%09        %09%0A%09        %09//TODO Set File Status record to %22Masked%22. 
+%09%09%09LabFile file = (LabFile)ida.load(LabFile.class, StringUtils.convertToLong(strId));%0A%09%09%09%09DataStatus maskStatus = new DataStatus();%0A%09%09%09%09maskStatus.setReason(strDescription);%0A%09%09%09%09maskStatus.setStatus(CalabConstants.MASK_STATUS);%0A%09%09%09%09ida.createObject(maskStatus);%0A%09%09%09%09%0A%09%09%09%09file.setDataStatus(maskStatus);%0A%09%09%09%09ida.store(file); %09        %09
 %0A%09  

Implement the mask File