package gov.nih.nci.cananolab.service.organization.impl;

import gov.nih.nci.cananolab.domain.common.PointOfContact;
import gov.nih.nci.cananolab.domain.particle.NanoparticleSample;
import gov.nih.nci.cananolab.dto.common.OrganizationBean;
import gov.nih.nci.cananolab.exception.OrganizationException;
import gov.nih.nci.cananolab.service.organization.OrganizationService;
import gov.nih.nci.cananolab.service.organization.helper.OrganizationServiceHelper;
import gov.nih.nci.cananolab.service.particle.NanoparticleSampleService;
import gov.nih.nci.cananolab.service.particle.impl.NanoparticleSampleServiceLocalImpl;
import gov.nih.nci.cananolab.service.security.AuthorizationService;
import gov.nih.nci.cananolab.system.applicationservice.CustomizedApplicationService;
import gov.nih.nci.cananolab.util.CaNanoLabConstants;
import gov.nih.nci.system.client.ApplicationServiceProvider;

import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * Local implementation of SourceService
 * 
 * @author tanq
 * 
 */
public class OrganizationServiceLocalImpl implements OrganizationService {
	private static Logger logger = Logger
			.getLogger(OrganizationServiceLocalImpl.class);
	private OrganizationServiceHelper helper = new OrganizationServiceHelper();

	/**
	 * Persist a new organization or update an existing organizations
	 * 
	 * @param primaryOrganization
	 * @param otherOrganizationCollection
	 * 
	 * @throws OrganizationException
	 */
	public void saveOrganization(String particleId,
			OrganizationBean primaryOrganization, 
			List<OrganizationBean> otherOrganizationCollection)
		throws OrganizationException{
		
		//TODO: to verify if organization.primaryNanoparticleSampleCollection is empty		
		try {
			CustomizedApplicationService appService = (CustomizedApplicationService) ApplicationServiceProvider
					.getApplicationService();
			AuthorizationService authService = new AuthorizationService(
					CaNanoLabConstants.CSM_APP_NAME);
			NanoparticleSampleService NanoparticleSampleService = new NanoparticleSampleServiceLocalImpl();
			NanoparticleSample nanoparticleSample = NanoparticleSampleService.findNanoparticleSampleById(particleId).getDomainParticleSample();
			Collection<NanoparticleSample> primaryNanoparticleSampleCollection = 
				primaryOrganization.getDomain().getPrimaryNanoparticleSampleCollection();
			if (primaryNanoparticleSampleCollection!=null &&
					!primaryNanoparticleSampleCollection.contains(nanoparticleSample)){
				primaryNanoparticleSampleCollection.add(nanoparticleSample);
			}
			saveOrganization(primaryOrganization, authService, appService);
			if (otherOrganizationCollection!=null) {
				for (OrganizationBean organizationBean: otherOrganizationCollection) {
					Collection<NanoparticleSample> otherNanoparticleSampleCollection = 
						organizationBean.getDomain().getNanoparticleSampleCollection();
					if (otherNanoparticleSampleCollection!=null &&
							!otherNanoparticleSampleCollection.contains(nanoparticleSample)){
						otherNanoparticleSampleCollection.add(nanoparticleSample);
					}
					saveOrganization(organizationBean, authService, appService);
				}
			}
		} catch (Exception e) {
			String err = "Error in saving the organization.";
			logger.error(err, e);
			throw new OrganizationException(err, e);
		}
	}
	

	
	public List<OrganizationBean> findOtherOrganizationCollection(String particleId)
		throws OrganizationException{
		return helper.findOtherOrganizationCollection(particleId);		
	}
	
	public OrganizationBean findPrimaryOrganization(String particleId)
		throws OrganizationException{
		return helper.findPrimaryOrganization(particleId);	
	}
	
	private void saveOrganization(OrganizationBean organizationBean,
			AuthorizationService authService, CustomizedApplicationService appService)
		throws Exception{
		if (organizationBean.getDomain().getPointOfContactCollection() == null) {
			organizationBean.getDomain().setPointOfContactCollection(new HashSet<PointOfContact>());
		} else {
			for (PointOfContact poc : organizationBean.getDomain().getPointOfContactCollection()) {
				if (poc.getId() != null)
					authService
							.removePublicGroup(poc.getId().toString());
			}
			organizationBean.getDomain().getPointOfContactCollection().clear();
		}
		if (organizationBean.getPocs() != null) {
			Calendar myCal = Calendar.getInstance();
			for (PointOfContact poc : organizationBean.getPocs()) {
				if (!StringUtils.isBlank(poc.getFirstName())
						|| !StringUtils.isBlank(poc.getLastName())
						|| !StringUtils.isBlank(poc.getMiddleInitial())) {
					if (poc.getCreatedDate() == null) {
						myCal.add(Calendar.SECOND, 1);
						poc.setCreatedDate(myCal.getTime());
					}
					organizationBean.getDomain().getPointOfContactCollection().add(poc);
				}
			}
		}
		appService.saveOrUpdate(organizationBean);		
	}

}

@@ -931,24 +931,47 @@
 Collection;%0A
+import java.util.Date;%0A
 import java.
@@ -2528,31 +2528,115 @@
 llection
-!
+=
 =null
- &&
+) %7B
 %0A%09%09%09%09
-%09
+primaryNanoparticleSampleCollection = new HashSet%3CNanoparticleSample%3E();%0A%09%09%09%7D%0A%09%09%09if (
 !primary
@@ -2765,16 +2765,146 @@
 );%0A%09%09%09%7D%0A
+%09%09%09primaryOrganization.getDomain()%0A%09%09%09%09%09.setPrimaryNanoparticleSampleCollection(%0A%09%09%09%09%09%09%09primaryNanoparticleSampleCollection);%0A%09%09%09%0A
 %09%09%09saveO
@@ -2962,16 +2962,20 @@
 rvice);%0A
+%09%09%09%0A
 %09%09%09if (o
@@ -2996,26 +2996,28 @@
 onCollection
+ 
 !=
+ 
 null) %7B%0A%09%09%09%09
@@ -5135,16 +5135,141 @@
 %09%09%7D%0A%09%09%7D%0A
+%09%09if (organizationBean.getDomain().getCreatedDate()==null) %7B%0A%09%09%09organizationBean.getDomain().setCreatedDate(new Date());%0A%09%09%7D%0A
 %09%09appSer
@@ -5302,16 +5302,28 @@
 tionBean
+.getDomain()
 );%09%09%0A%09%7D%0A

set created_date as system date when null