001    package org.hackystat.sensorbase.resource.projects;
002    
003    import org.hackystat.sensorbase.resource.sensorbase.SensorBaseResource;
004    import org.restlet.Context;
005    import org.restlet.data.Request;
006    import org.restlet.data.Response;
007    import org.restlet.data.Status;
008    import org.restlet.resource.Representation;
009    import org.restlet.resource.Variant;
010    
011    /**
012     * The resource for processing POST host/projects/{email}/{projectname}/rename/{newprojectname}.
013     * 
014     * @author Philip Johnson
015     */
016    public class UserProjectRenameResource extends SensorBaseResource {
017      
018      /** To be retrieved from the URL; the new name. */
019      private String newProjectName;
020      
021      /**
022       * Provides the following representational variants: TEXT_XML.
023       * @param context The context.
024       * @param request The request object.
025       * @param response The response object.
026       */
027      public UserProjectRenameResource(Context context, Request request, Response response) {
028        super(context, request, response);
029        this.newProjectName = (String) request.getAttributes().get("newprojectname");
030      }
031      
032      /** 
033       * Indicate the GET method is not supported.
034       * @return False.
035       */
036      @Override
037      public boolean allowGet() {
038        return false;
039      }
040      
041      /**
042       * Returns nothing since GET is not supported.
043       * 
044       * @param variant The representational variant requested, or null if conditions are violated.
045       * @return The representation. 
046       */
047      @Override
048      public Representation represent(Variant variant) {
049        return null;
050      }
051      
052      /** 
053       * Indicate the POST method is supported. 
054       * @return True.
055       */
056      @Override
057      public boolean allowPost() {
058        return true;
059      }
060    
061      /**
062       * Implement the POST method that processes a project rename command. 
063       * <ul>
064       * <li> UriUser must be a defined user, and user/project must be a defined project.
065       * <li> The authorized user must be the project owner. 
066       * </ul>
067       * @param entity ignored.
068       */
069      @Override
070      public void acceptRepresentation(Representation entity) {
071       
072        if (!validateUriUserIsUser() ||
073            !validateUriProjectName() ||
074            !validateProjectOwner()) {
075          return;
076        }
077        
078        // Cannot rename the default project.
079        if (ProjectManager.DEFAULT_PROJECT_NAME.equals(this.projectName)) {
080          setStatusMiscError("Cannot rename the default project.");
081          return;
082        }
083        
084        // Cannot rename a project to the name of an already existing project.
085        if (super.projectManager.getProject(this.user, newProjectName) != null) {
086          setStatusMiscError(String.format("Project %s already exists.", newProjectName));
087          return;
088        }
089    
090        // Now we tell the project manager to attempt to rename the project and return.
091        // The renameProject method is synchronized for thread safety. 
092        try {
093          super.projectManager.renameProject(this.user, projectName, newProjectName);
094          getResponse().setStatus(Status.SUCCESS_OK);
095        }
096        catch (Exception e) {
097          setStatusInternalError(e);
098        }
099      }
100    }