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

import gov.nih.nci.cananolab.service.common.PointOfContactService;
import gov.nih.nci.cananolab.service.common.impl.PointOfContactServiceLocalImpl;
import gov.nih.nci.cananolab.ui.security.InitSecuritySetup;

import java.util.SortedSet;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.directwebremoting.impl.DefaultWebContextBuilder;

public class DWRSampleManager {

	Logger logger = Logger.getLogger(DWRSampleManager.class);

	public DWRSampleManager() {
	}

	/* remove organization associated with the POC from the visiblity group */
	public String[] removeOrgVisibility(String pocId) {

		PointOfContactService pocService = new PointOfContactServiceLocalImpl();

		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		try {
			SortedSet<String> visibilityGroup = InitSecuritySetup.getInstance()
					.getAllVisibilityGroups(request);
			if (!pocId.equalsIgnoreCase("other")) {
				String sampleOrg = pocService.findPointOfContactById(pocId)
						.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[] { "" };
	}

	public String[] getNanoparticleEntityTypes(String searchLocations) {
		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		try {
			boolean isLocal = false;
			if ("local".equals(searchLocations)) {
				isLocal = true;
			}
			SortedSet<String> types = null;
			if (isLocal) {
				types = InitCompositionSetup.getInstance()
						.getNanoparticleEntityTypes(request);
			} else {
				types = InitCompositionSetup.getInstance()
						.getDefaultNanoparticleEntityTypes(request);
			}
			String[] eleArray = new String[types.size()];
			return types.toArray(eleArray);
		} catch (Exception e) {
			logger.error("Problem setting nanoparticle entity types: \n", e);
			e.printStackTrace();
		}
		return new String[] { "" };
	}

	public String[] getFunctionalizingEntityTypes(String searchLocations) {
		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		try {
			boolean isLocal = false;
			if ("local".equals(searchLocations)) {
				isLocal = true;
			}
			SortedSet<String> types = null;
			if (isLocal) {
				types = InitCompositionSetup.getInstance()
						.getFunctionalizingEntityTypes(request);
			} else {
				types = InitCompositionSetup.getInstance()
						.getDefaultFunctionalizingEntityTypes(request);
			}
			String[] eleArray = new String[types.size()];
			return types.toArray(eleArray);
		} catch (Exception e) {
			logger.error("Problem setting functionalizing entity types: \n", e);
			e.printStackTrace();
		}
		return new String[] { "" };
	}

	public String[] getFunctionTypes(String searchLocations) {
		DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();
		org.directwebremoting.WebContext webContext = dwcb.get();
		HttpServletRequest request = webContext.getHttpServletRequest();
		try {
			boolean isLocal = false;
			if ("local".equals(searchLocations)) {
				isLocal = true;
			}
			SortedSet<String> types = null;
			if (isLocal) {
				types = InitCompositionSetup.getInstance().getFunctionTypes(
						request);
			} else {
				types = InitCompositionSetup.getInstance()
						.getDefaultFunctionTypes(request);
			}
			String[] eleArray = new String[types.size()];
			return types.toArray(eleArray);
		} catch (Exception e) {
			logger.error("Problem setting function types: \n", e);
			e.printStackTrace();
		}
		return new String[] { "" };
	}
}

@@ -1486,32 +1486,801 @@
 ing%5B%5D %7B %22%22 %7D;%0A%09%7D
+%0A%09%0A%09/* remove organization name from the visiblity group */%0A%09public String%5B%5D removeOrgNameVisibility(String orgName) %7B%0A%09%09DefaultWebContextBuilder dwcb = new DefaultWebContextBuilder();%0A%09%09org.directwebremoting.WebContext webContext = dwcb.get();%0A%09%09HttpServletRequest request = webContext.getHttpServletRequest();%0A%09%09try %7B%0A%09%09%09SortedSet%3CString%3E visibilityGroup = InitSecuritySetup.getInstance()%0A%09%09%09%09%09.getAllVisibilityGroups(request);%0A%09%09%09if (!orgName.equalsIgnoreCase(%22other%22)) %7B%0A%09%09%09%09visibilityGroup.remove(orgName);%0A%09%09%09%7D%0A%09%09%09String%5B%5D eleArray = new String%5BvisibilityGroup.size()%5D;%0A%09%09%09return visibilityGroup.toArray(eleArray);%0A%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09System.out.println(%22removeOrgNameVisibility exception.%22);%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09return new String%5B%5D %7B %22%22 %7D;%0A%09%7D
 %0A%0A%09public String

add removeOrgNameVisibility