package gov.nih.nci.cananolab.ui.sample;

import gov.nih.nci.cananolab.domain.common.Organization;
import gov.nih.nci.cananolab.domain.common.PointOfContact;
import gov.nih.nci.cananolab.dto.common.PointOfContactBean;
import gov.nih.nci.cananolab.dto.common.UserBean;
import gov.nih.nci.cananolab.dto.particle.SampleBean;
import gov.nih.nci.cananolab.service.sample.SampleService;
import gov.nih.nci.cananolab.service.sample.helper.SampleServiceHelper;
import gov.nih.nci.cananolab.service.sample.impl.SampleServiceLocalImpl;
import gov.nih.nci.cananolab.ui.security.InitSecuritySetup;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts.validator.DynaValidatorForm;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.impl.DefaultWebContextBuilder;

public class DWRPointOfContactManager {

	private Logger logger = Logger.getLogger(DWRPointOfContactManager.class);
	private SampleService service = new SampleServiceLocalImpl();
	private SampleServiceHelper helper = new SampleServiceHelper();

	public DWRPointOfContactManager() {
	}

	public PointOfContactBean getPointOfContactById(String id,
			Boolean primaryStatus) throws Exception {
		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		UserBean user = (UserBean) request.getSession().getAttribute("user");
		PointOfContactBean poc = service.findPointOfContactById(id, user);
		poc.setPrimaryStatus(primaryStatus);
		return poc;
	}

	public PointOfContactBean resetThePointOfContact() {
		DynaValidatorForm sampleForm = (DynaValidatorForm) (WebContextFactory
				.get().getSession().getAttribute("sampleForm"));
		SampleBean sample = (SampleBean) sampleForm.get("sampleBean");
		PointOfContactBean poc = new PointOfContactBean();
		sample.setThePOC(poc);
		//if primary POC already exists, the POC is secondar
		if (sample.getPrimaryPOCBean().getDomain().getId()!=null) {
			poc.setPrimaryStatus(false);
		}
		return poc;
	}

	public PointOfContactBean getPointOfContactByNameAndOrg(String firstName,
			String lastName, String orgName) throws Exception {
		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		UserBean user = (UserBean) request.getSession().getAttribute("user");
		PointOfContact poc=helper.findPointOfContactByNameAndOrg(firstName, lastName, orgName, user);
		PointOfContactBean pocBean = new PointOfContactBean(poc);
		return pocBean;
	}

	public Organization getOrganizationByName(String name) throws Exception {
		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		UserBean user = (UserBean) request.getSession().getAttribute("user");
		Organization org = helper.findOrganizationByName(name, user);
		return org;
	}

	/* remove organization associated with the POC from the visiblity group */
	public String[] removeOrgVisibility(String pocId) {
		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		UserBean user = (UserBean) request.getSession().getAttribute("user");
		try {
			List<String> visibilityGroup = InitSecuritySetup.getInstance()
					.getAllVisibilityGroups(request);
			if (!pocId.equalsIgnoreCase("other")) {
				String sampleOrg = service.findPointOfContactById(pocId, user)
						.getDomain().getOrganization().getName();
				visibilityGroup.remove(sampleOrg);
			}
			String[] eleArray = new String[visibilityGroup.size()];
			return visibilityGroup.toArray(eleArray);

		} catch (Exception e) {
			System.out.println("removeOrgVisibility exception.");
			e.printStackTrace();
		}

		return new String[] { "" };
	}

	/* remove organization name from the visiblity group */
	public String[] removeOrgNameVisibility(String orgName) {
		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		try {
			List<String> visibilityGroup = InitSecuritySetup.getInstance()
					.getAllVisibilityGroups(request);
			if (!orgName.equalsIgnoreCase("other")) {
				visibilityGroup.remove(orgName);
			}
			String[] eleArray = new String[visibilityGroup.size()];
			return visibilityGroup.toArray(eleArray);

		} catch (Exception e) {
			System.out.println("removeOrgNameVisibility exception.");
			e.printStackTrace();
		}
		return new String[] { "" };
	}

}

@@ -1679,16 +1679,33 @@
 ontact()
+ throws Exception
  %7B%0A%09%09Dyn
@@ -1824,16 +1824,45 @@
 orm%22));%0A
+%09%09if (sampleForm != null) %7B%0A%09
 %09%09Sample
@@ -1914,24 +1914,25 @@
 mpleBean%22);%0A
+%09
 %09%09PointOfCon
@@ -1974,16 +1974,17 @@
 an();%0A%09%09
+%09
 sample.s
@@ -2002,12 +2002,14 @@
 c);%0A
+%09
 %09%09//
+ 
 if p
@@ -2055,16 +2055,17 @@
 econdar%0A
+%09
 %09%09if (sa
@@ -2108,18 +2108,20 @@
 .getId()
+ 
 !=
+ 
 null) %7B%0A
@@ -2123,16 +2123,17 @@
 l) %7B%0A%09%09%09
+%09
 poc.setP
@@ -2151,26 +2151,28 @@
 s(false);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09return poc
@@ -2165,32 +2165,65 @@
 %0A%09%09%09return poc;%0A
+%09%09%7D%0A%09%09else %7B%0A%09%09%09return null;%0A%09%09%7D%0A
 %09%7D%0A%0A%09public Poin
@@ -2625,17 +2625,19 @@
 tact poc
-=
+ = 
 helper.f
@@ -2676,17 +2676,21 @@
 rstName,
- 
+%0A%09%09%09%09
 lastName

check for null form