From c808a2d7a8ffe1c7f7bcfc5b4d9d14c5ad0c53a1 Mon Sep 17 00:00:00 2001
From: Mike McKay <mfmckay@wustl.edu>
Date: Tue, 17 May 2016 21:35:42 -0500
Subject: [PATCH] Reverted a change that was causing the pipelines tab not to
 load.

---
 .../ProjectPipelineListResource.java          | 119 +++++++++++-------
 1 file changed, 71 insertions(+), 48 deletions(-)

diff --git a/src/main/java/org/nrg/xnat/restlet/resources/ProjectPipelineListResource.java b/src/main/java/org/nrg/xnat/restlet/resources/ProjectPipelineListResource.java
index 648d4e21..08dc5bac 100644
--- a/src/main/java/org/nrg/xnat/restlet/resources/ProjectPipelineListResource.java
+++ b/src/main/java/org/nrg/xnat/restlet/resources/ProjectPipelineListResource.java
@@ -13,7 +13,6 @@ package org.nrg.xnat.restlet.resources;
 
 import org.nrg.pipeline.PipelineRepositoryManager;
 import org.nrg.xdat.om.ArcProject;
-import org.nrg.xdat.om.PipePipelinerepository;
 import org.nrg.xdat.om.XnatProjectdata;
 import org.nrg.xdat.security.helpers.Permissions;
 import org.nrg.xft.XFTTable;
@@ -29,21 +28,21 @@ import org.restlet.resource.Variant;
 public class ProjectPipelineListResource extends SecureResource  {
 	XnatProjectdata proj = null;
 	String pID = null;
-	
-	
-	
+
+
+
 	public ProjectPipelineListResource(Context context, Request request, Response response) {
 		super(context, request, response);
 		this.getVariants().add(new Variant(MediaType.APPLICATION_JSON));
 		this.getVariants().add(new Variant(MediaType.TEXT_XML));
-		
+
 		pID= (String)getParameter(request,"PROJECT_ID");
 		if(pID!=null){
 			proj = XnatProjectdata.getProjectByIDorAlias(pID, user, false);
 		}
 	}
-	
-	
+
+
 	@Override
 	public boolean allowGet() {
 		return true;
@@ -53,7 +52,7 @@ public class ProjectPipelineListResource extends SecureResource  {
 	public boolean allowDelete() {
 		return true;
 	}
-	
+
 	public void handleDelete() {
 		//Remove the Pipeline identified by the path for the project and the datatype
 		if (proj != null) {
@@ -63,85 +62,109 @@ public class ProjectPipelineListResource extends SecureResource  {
 			datatype = this.getQueryVariable("datatype");
 			if (pathToPipeline != null && datatype != null) {
 				pathToPipeline = pathToPipeline.trim();
-				datatype = datatype.trim();
-
-				boolean isUserAuthorized = false;
-				try {
-					isUserAuthorized = Permissions.canDelete(user, proj);
-				} catch (Exception e) {
-					e.printStackTrace();
-					getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, "Encountered exception " + e.getMessage());
-					return;
-				}
-
+				datatype=datatype.trim();
+				boolean isUserAuthorized = isUserAuthorized();
 				if (isUserAuthorized) {
 					try {
 						ArcProject arcProject = ArcSpecManager.GetFreshInstance().getProjectArc(proj.getId());
 						boolean success = PipelineRepositoryManager.GetInstance().delete(arcProject, pathToPipeline, datatype, user);
 						if (!success) {
 							getLogger().log(getLogger().getLevel(), "Couldnt delete the pipeline " + pathToPipeline + " for the project " + proj.getId());
-							getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, " Couldn't successfully save Project Specification");
+							getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, " Couldnt succesfully save Project Specification" );
 							return;
-						} else {
+						}else {
 							ArcSpecManager.Reset();
-							returnDefaultRepresentation();
+							getResponse().setEntity(getRepresentation(getVariants().get(0)));
+							Representation selectedRepresentation = getResponse().getEntity();
+							if (getRequest().getConditions().hasSome()) {
+								final Status status = getRequest().getConditions()
+										.getStatus(getRequest().getMethod(),
+												selectedRepresentation);
+
+								if (status != null) {
+									getResponse().setStatus(status);
+									getResponse().setEntity(null);
+								}
+							}
 							//Send a 200 OK message back
 							//getResponse().setStatus(Status.SUCCESS_OK,"Pipeline has been removed from project " + _project.getId());
 						}
-					} catch (Exception e) {
+					}catch(Exception e) {
 						e.printStackTrace();
 						getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, "Encountered exception " + e.getMessage());
 					}
-				} else {
-					getResponse().setStatus(Status.CLIENT_ERROR_FORBIDDEN, "User unauthorized to remove pipeline from project");
+				}else {
+					getResponse().setStatus(Status.CLIENT_ERROR_FORBIDDEN, "User unauthroized to remove pipeline from project");
 				}
-			} else {
-				getResponse().setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED, "Expecting path and datatype as query parameters");
 			}
+		}else {
+			getResponse().setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED, "Expecting path and datatype as query parameters");
 		}
 	}
-	
-	@Override
-	public Representation getRepresentation(Variant variant) {
-		boolean isUserAuthorized;
+
+
+
+
+	private boolean isUserAuthorized() {
+		boolean isUserAuthorized = false;
 		try {
-			isUserAuthorized = Permissions.canRead(user, proj);
-		} catch (Exception e) {
+			isUserAuthorized = Permissions.canDelete(user,proj);
+		}catch(Exception e) {
 			e.printStackTrace();
-			getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, "Encountered exception " + e.getMessage());
-			return null;
+			getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
 		}
+		return isUserAuthorized;
+	}
+
+	@Override
+	public Representation getRepresentation(Variant variant) {
+		//Document xmldoc = null;
+		boolean isUserAuthorized = isUserAuthorized();
+		ArcProject arcProject = ArcSpecManager.GetFreshInstance().getProjectArc(proj.getId());
+		String comment = "existing";
 		if (isUserAuthorized) {
-			ArcProject arcProject = ArcSpecManager.GetFreshInstance().getProjectArc(proj.getId());
-			boolean additional = this.isQueryVariableTrue("additional");
+			boolean additional=this.isQueryVariableTrue("additional");
 
 			//Check to see if the Project already has an entry in the ArcSpec.
 			//If yes, then return that entry. If not then construct a new ArcProject element and insert an attribute to say that it's an already existing
 			//entry or not
 			try {
-				if (additional) {
-					PipePipelinerepository repository = PipelineRepositoryManager.GetInstance();
-					arcProject = arcProject == null ? repository.createNewArcProject(proj) : repository.getAdditionalPipelines(proj);
-				} else {
-					getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, "No archive spec entry for project " + proj.getId());
+				if (arcProject == null) { // No Project pipelines set in the archive specification
+					if (additional) {
+						arcProject = PipelineRepositoryManager.GetInstance().createNewArcProject(proj);
+						comment = "new";
+					}else {
+						getResponse().setStatus(Status.SERVER_ERROR_INTERNAL, "No archive spec entry for project " + proj.getId());
+					}
+				}else {
+					if (additional) { //Return all the pipelines that are applicable to the project but not selected
+						arcProject = PipelineRepositoryManager.GetInstance().getAdditionalPipelines(proj);
+						comment = "additional";
+					}else {
+						//XFTItem hack = arcProject.getCurrentDBVersion(true);
+						//arcProject.setItem(hack);
+					}
 				}
-			} catch(Exception e) {
+				//xmldoc = arcProject.toXML();
+				//Comment commentNode = xmldoc.createComment(comment);
+				//xmldoc.appendChild(commentNode);
+			}catch(Exception e) {
 				e.printStackTrace();
 				getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
 			}
 			MediaType mt = overrideVariant(variant);
 			if (mt.equals(MediaType.TEXT_XML)) {
-				return representItem(arcProject.getItem(), mt, null, false, true);
-			} else if (mt.equals(MediaType.APPLICATION_JSON)) {
+				return representItem(arcProject.getItem(), mt, null,false, true);
+			}else if (mt.equals(MediaType.APPLICATION_JSON)) {
 				XFTTable table = PipelineRepositoryManager.GetInstance().toTable(arcProject);
 
 				return representTable(table, mt,null);
-			} else {
+			}else {
 				return null;
 			}
-		} else {
+		}else {
 			getResponse().setStatus(Status.CLIENT_ERROR_FORBIDDEN);
 		}
 		return null;
 	}
-}
+}
\ No newline at end of file
-- 
GitLab