<commit_before>/*
 * $Id: ImageManagerImpl.java,v 1.28 2007-10-31 19:06:10 pandyas Exp $
 *
 * $Log: not supported by cvs2svn $
 * Revision 1.27  2007/09/12 19:36:03  pandyas
 * modified debug statements for build to stage tier
 *
 * Revision 1.26  2007/08/01 18:11:46  pandyas
 * Fixed image upload issue - the code was not saving the constructs in the correct folder so the image was not viewable in the edit mode.
 *
 * Also changed a few properties in camod.properties to comple the fix
 *
 * Revision 1.25  2007/08/01 18:05:02  pandyas
 * VCDE changes
 *
 * Revision 1.24  2007/07/31 12:02:22  pandyas
 * VCDE silver level  and caMOD 2.3 changes
 *
 * Revision 1.23  2007/06/25 16:57:11  pandyas
 * Fixed code so staining_method will not clear when other fields are edited
 *
 * Revision 1.22  2007/04/20 17:51:27  pandyas
 * Modified to add Staining Method tree to Image submission
 *
 * Revision 1.21  2006/08/17 18:26:17  pandyas
 * Defect# 410: Externalize properties files - Code Changes to send mail method
 *
 * Revision 1.20  2006/05/24 20:25:29  georgeda
 * Fixed staining methods
 *
 * Revision 1.19  2006/05/24 19:01:24  georgeda
 * Cleaned up
 *
 * Revision 1.18  2006/05/24 16:46:14  pandyas
 * Converted StainingMethod to lookup - modified code to pull dropdown list from DB
 *
 * Revision 1.17  2006/04/17 19:11:05  pandyas
 * caMod 2.1 OM changes
 *
 */
package gov.nih.nci.camod.service.impl;

import gov.nih.nci.camod.Constants;
import gov.nih.nci.camod.domain.AnimalModel;
import gov.nih.nci.camod.domain.Image;
import gov.nih.nci.camod.domain.StainingMethod;
import gov.nih.nci.camod.service.ImageManager;
import gov.nih.nci.camod.util.FtpUtil;
import gov.nih.nci.camod.util.RandomGUID;
import gov.nih.nci.camod.webapp.form.ImageData;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import org.apache.struts.upload.FormFile;

public class ImageManagerImpl extends BaseManager implements ImageManager {

	public List getAll() throws Exception {
		log.trace("In ImageManagerImpl.getAll");
		return super.getAll(Image.class);
	}

	public Image get(String id) throws Exception {
		log.trace("In ImageManagerImpl.get");
		return (Image) super.get(id, Image.class);
	}

	public void save(Image Image) throws Exception {
		log.debug("In ImageManagerImpl.save");
		super.save(Image);
	}

	public void remove(String id, AnimalModel inAnimalModel) throws Exception {
		log.trace("In ImageManagerImpl.remove");

		inAnimalModel.getImageCollection().remove(get(id));
		super.save(inAnimalModel);
	}

	public Image create(AnimalModel inAnimalModel, ImageData inImageData,
			String inPath, String inStorageDirKey) throws Exception {

		log.debug("Entering ImageManagerImpl.create");

		Image inImage = new Image();
		populateImage(inAnimalModel, inImageData, inImage, inPath,
				inStorageDirKey);

		log.debug("Exiting ImageManagerImpl.create");

		return inImage;
	}

	public void update(AnimalModel inAnimalModel, ImageData inImageData,
			Image inImage, String inPath, String inStorageDirKey)
			throws Exception {

		log.trace("Entering ImageManagerImpl.update");
		log.debug("Updating ImageForm: " + inImage.getId());

		// Populate w/ the new values and save
		populateImage(inAnimalModel, inImageData, inImage, inPath,
				inStorageDirKey);
		save(inImage);

		log.trace("Exiting ImageManagerImpl.update");
	}

	private void populateImage(AnimalModel inAnimalModel,
			ImageData inImageData, Image inImage, String inPath,
			String inStorageDirKey) throws Exception {

		log.debug("Entering populateImage");
		if (inImageData.getStainingMethodCode() != null
				&& inImageData.getStainingMethodCode().length() >0) {

            // every submission - lookup the StainingMethod or create one new
			StainingMethod stainingMethod = StainingMethodManagerSingleton
					.instance().getOrCreate(inImageData.getStainingMethodCode(),
							inImageData.getStainingMethodName());
            log.debug("populateImage inImageData.getStainingMethodCode(): " +inImageData.getStainingMethodCode());
            log.debug("populateImage inImageData.getStainingMethodName(): " +inImageData.getStainingMethodName());            
            inImage.setStainingMethod(stainingMethod);
            
            inImage.setComments(inImageData.getComments());

		} else {
			// null staining method - covers editing
			inImage.setStainingMethod(null);
		}


		if (inImage != null) {
			inImage.setTitle(inImageData.getTitle());
			inImage.setDescription(inImageData.getDescriptionOfConstruct());
		}

		// Upload Construct File location, Title of Construct, Description of
		// Construct
		// Check for exisiting Image for this Image
		if (inImageData.getFileLocation() != null) {

			log.debug("<ImageManagerImpl> Uploading a file");

			// If this is a new Image, upload it to the server
			FormFile f = inImageData.getFileLocation();

			// Retrieve the file type
			String fileType = null;
			StringTokenizer strToken = new StringTokenizer(f.getFileName(), ".");

			while (strToken.hasMoreTokens()) {<commit_msg>Added url for image<commit_after>/*
 * $Id: ImageManagerImpl.java,v 1.29 2008-08-14 06:26:09 schroedn Exp $
 *
 * $Log: not supported by cvs2svn $
 * Revision 1.28  2007/10/31 19:06:10  pandyas
 * Fixed #8355 	Add comments field to every submission page
 *
 * Revision 1.27  2007/09/12 19:36:03  pandyas
 * modified debug statements for build to stage tier
 *
 * Revision 1.26  2007/08/01 18:11:46  pandyas
 * Fixed image upload issue - the code was not saving the constructs in the correct folder so the image was not viewable in the edit mode.
 *
 * Also changed a few properties in camod.properties to comple the fix
 *
 * Revision 1.25  2007/08/01 18:05:02  pandyas
 * VCDE changes
 *
 * Revision 1.24  2007/07/31 12:02:22  pandyas
 * VCDE silver level  and caMOD 2.3 changes
 *
 * Revision 1.23  2007/06/25 16:57:11  pandyas
 * Fixed code so staining_method will not clear when other fields are edited
 *
 * Revision 1.22  2007/04/20 17:51:27  pandyas
 * Modified to add Staining Method tree to Image submission
 *
 * Revision 1.21  2006/08/17 18:26:17  pandyas
 * Defect# 410: Externalize properties files - Code Changes to send mail method
 *
 * Revision 1.20  2006/05/24 20:25:29  georgeda
 * Fixed staining methods
 *
 * Revision 1.19  2006/05/24 19:01:24  georgeda
 * Cleaned up
 *
 * Revision 1.18  2006/05/24 16:46:14  pandyas
 * Converted StainingMethod to lookup - modified code to pull dropdown list from DB
 *
 * Revision 1.17  2006/04/17 19:11:05  pandyas
 * caMod 2.1 OM changes
 *
 */
package gov.nih.nci.camod.service.impl;

import gov.nih.nci.camod.Constants;
import gov.nih.nci.camod.domain.AnimalModel;
import gov.nih.nci.camod.domain.Image;
import gov.nih.nci.camod.domain.StainingMethod;
import gov.nih.nci.camod.service.ImageManager;
import gov.nih.nci.camod.util.FtpUtil;
import gov.nih.nci.camod.util.RandomGUID;
import gov.nih.nci.camod.webapp.form.ImageData;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import org.apache.struts.upload.FormFile;

public class ImageManagerImpl extends BaseManager implements ImageManager {

	public List getAll() throws Exception {
		log.trace("In ImageManagerImpl.getAll");
		return super.getAll(Image.class);
	}

	public Image get(String id) throws Exception {
		log.trace("In ImageManagerImpl.get");
		return (Image) super.get(id, Image.class);
	}

	public void save(Image Image) throws Exception {
		log.debug("In ImageManagerImpl.save");
		super.save(Image);
	}

	public void remove(String id, AnimalModel inAnimalModel) throws Exception {
		log.trace("In ImageManagerImpl.remove");

		inAnimalModel.getImageCollection().remove(get(id));
		super.save(inAnimalModel);
	}

	public Image create(AnimalModel inAnimalModel, ImageData inImageData,
			String inPath, String inStorageDirKey) throws Exception {

		log.debug("Entering ImageManagerImpl.create");

		Image inImage = new Image();
		populateImage(inAnimalModel, inImageData, inImage, inPath,
				inStorageDirKey);

		log.debug("Exiting ImageManagerImpl.create");

		return inImage;
	}

	public void update(AnimalModel inAnimalModel, ImageData inImageData,
			Image inImage, String inPath, String inStorageDirKey)
			throws Exception {

		log.trace("Entering ImageManagerImpl.update");
		log.debug("Updating ImageForm: " + inImage.getId());

		// Populate w/ the new values and save
		populateImage(inAnimalModel, inImageData, inImage, inPath,
				inStorageDirKey);
		save(inImage);

		log.trace("Exiting ImageManagerImpl.update");
	}

	private void populateImage(AnimalModel inAnimalModel,
			ImageData inImageData, Image inImage, String inPath,
			String inStorageDirKey) throws Exception {

		log.debug("Entering populateImage");
		
        System.out.println("<ImageAction save> following Characteristics:" + "\n\t getUrl: "
                + inImageData.getUrl() + "\n\t getTitle: " + inImageData.getTitle()
                + "\n\t getDescription: " + inImageData.getDescription()
                + "\n\t getStainingMethodName() : " + inImageData.getStainingMethodName()                   
                + "\n\t getStainingMethodCode(): " + inImageData.getStainingMethodCode()    
                + "\n\t Altern_URL: " + inImageData.getAltern_url() 
                + "\n\t Comments(): " + inImageData.getComments());
		
        if (inImage != null) {
	        inImage.setComments(inImageData.getComments());
	        inImage.setAltern_url(inImageData.getAltern_url());			
		}
        
		if (inImageData.getStainingMethodCode() != null
				&& inImageData.getStainingMethodCode().length() >0) {

            // every submission - lookup the StainingMethod or create one new
			StainingMethod stainingMethod = StainingMethodManagerSingleton
					.instance().getOrCreate(inImageData.getStainingMethodCode(),
							inImageData.getStainingMethodName());
            log.debug("populateImage inImageData.getStainingMethodCode(): " +inImageData.getStainingMethodCode());
            log.debug("populateImage inImageData.getStainingMethodName(): " +inImageData.getStainingMethodName());            
            inImage.setStainingMethod(stainingMethod);
            
		} else {
			// null staining method - covers editing
			inImage.setStainingMethod(null);
		}


		if (inImage != null) {
			inImage.setTitle(inImageData.getTitle());
			inImage.setDescription(inImageData.getDescriptionOfConstruct());
		}

		// Upload Construct File location, Title of Construct, Description of
		// Construct
		// Check for exisiting Image for this Image
		if (inImageData.getFileLocation() != null) {

			log.debug("<ImageManagerImpl> Uploading a file");

			// If this is a new Image, upload it to the server
			FormFile f = inImageData.getFileLocation();

			// Retrieve the file type
			String fileType = null;
			StringTokenizer strToken = new StringTokenizer(f.getFileName(), ".");

			while (strToken.hasMoreTokens()) {
{'fraction_non_alphanumeric': 0.10381652661064426, 'fraction_numerical': 0.0391281512605042, 'mean_word_length': 3.8866552609067577, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7147381', 'n_tokens_mistral': 4117, 'n_tokens_neox': 3537, 'n_words': 910}