View Javadoc

1   /*
2    * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 3.0 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17   * MA 02110-1301  USA
18   */
19  package fr.cnes.doi.resource.admin;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.logging.log4j.Level;
25  import org.apache.logging.log4j.Logger;
26  import org.restlet.data.Form;
27  import org.restlet.data.Status;
28  import org.restlet.resource.Delete;
29  import org.restlet.resource.Post;
30  import org.restlet.resource.ResourceException;
31  
32  import fr.cnes.doi.application.AdminApplication;
33  import static fr.cnes.doi.application.AdminApplication.SUFFIX_PROJECT_NAME_TEMPLATE;
34  import fr.cnes.doi.db.AbstractProjectSuffixDBHelper;
35  import fr.cnes.doi.plugin.PluginFactory;
36  import fr.cnes.doi.resource.AbstractResource;
37  import fr.cnes.doi.utils.spec.Requirement;
38  import org.restlet.data.Method;
39  import org.restlet.ext.wadl.MethodInfo;
40  import org.restlet.ext.wadl.ParameterStyle;
41  
42  /**
43   * Provide a resource to delete and rename a project from database.
44   */
45  public class ManageProjectsResource extends AbstractResource {
46  
47      /**
48       * Parameter for the project name {@value #PROJECT_NAME_PARAMETER}. This
49       * parameter is send to create a new identifier for the project.
50       */
51      public static final String PROJECT_NAME_PARAMETER = "newProjectName";
52  
53      /**
54       * Logger.
55       */
56      private volatile Logger LOG;
57  
58      /**
59       * Suffix of the project.
60       */
61      private volatile String suffixProject;
62  
63      /**
64       * Set-up method that can be overridden in order to initialize the state of
65       * the resource.
66       *
67       * @throws ResourceException - if a problem happens
68       */
69      @Override
70      protected void doInit() throws ResourceException {
71          super.doInit();
72          final AdminApplication app = (AdminApplication) getApplication();
73          LOG = app.getLog();
74          LOG.traceEntry();
75          setDescription("This resource handles deletion and renaming of a project");
76          this.suffixProject = getAttribute(SUFFIX_PROJECT_NAME_TEMPLATE);
77          LOG.debug(this.suffixProject);
78          LOG.traceExit();
79      }
80  
81      // TODO requirement
82      /**
83       * Rename the project from the project id sent in url.
84       *
85       * @param mediaForm form
86       */
87      @Requirement(reqId = Requirement.DOI_SRV_140, reqName = Requirement.DOI_SRV_140_NAME)
88      @Post
89      public void renameProject(final Form mediaForm) {
90          LOG.traceEntry("Parameters\n\tmediaForm : {}", mediaForm);
91          checkInputs(mediaForm);
92          final String newProjectName = mediaForm.getFirstValue(PROJECT_NAME_PARAMETER);
93          final AbstractProjectSuffixDBHelper manageProjects = PluginFactory.getProjectSuffix();
94          final boolean isRenamed = manageProjects.renameProject(Integer.parseInt(suffixProject),
95                  newProjectName);
96          if (isRenamed) {
97              setStatus(Status.SUCCESS_NO_CONTENT);
98          } else {
99              throw LOG.throwing(new ResourceException(
100                     Status.CLIENT_ERROR_BAD_REQUEST, "Cannot rename project to " + newProjectName));
101         }
102         LOG.traceExit();
103     }
104 
105     // TODO requirement
106     /**
107      * Delete the project from database.
108      */
109     @Requirement(reqId = Requirement.DOI_SRV_140, reqName = Requirement.DOI_SRV_140_NAME)
110     @Delete
111     public void deleteProject() {
112         LOG.traceEntry();
113 
114         final List<String> response = new ArrayList<>();
115         try {
116             response.addAll(((AdminApplication)getApplication()).getDois(suffixProject));
117 
118         } catch (Exception ex) {
119             LOG.error(ex + "\n"
120                     + "Error in SuffixProjectsDoisResource while searching for dois in project "
121                     + suffixProject);
122         }
123 
124         final AbstractProjectSuffixDBHelper manageProjects = PluginFactory.getProjectSuffix();
125 
126         // No DOIs have to be attached to a project before deleting it
127         if (!response.isEmpty()) {
128             throw LOG.throwing(new ResourceException(
129                     Status.CLIENT_ERROR_NOT_FOUND, suffixProject + " not found"));
130         } else if (manageProjects.deleteProject(Integer.parseInt(suffixProject))) {
131             setStatus(Status.SUCCESS_NO_CONTENT);
132         } else {
133             throw LOG.throwing(new ResourceException(
134                     Status.SERVER_ERROR_INTERNAL, "Cannot delete the project " + suffixProject));
135         }
136         LOG.traceExit();
137     }
138 
139     /**
140      * Tests if the {@link #PROJECT_NAME_PARAMETER} is set.
141      *
142      * @param mediaForm the parameters
143      * @throws ResourceException - if PROJECT_NAME_PARAMETER is not set
144      */
145     private void checkInputs(final Form mediaForm) throws ResourceException {
146         LOG.traceEntry("Parameters\n\tmediaForm : {}", mediaForm);
147         if (isValueNotExist(mediaForm, PROJECT_NAME_PARAMETER)) {
148             throw LOG.throwing(Level.ERROR, new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
149                     PROJECT_NAME_PARAMETER + " parameter must be set"));
150         }
151         LOG.debug("The form is valid");
152         LOG.traceExit();
153     }
154 
155     /**
156      * {@inheritDoc }
157      */
158     @Override
159     protected void describePost(final MethodInfo info) {
160         info.setName(Method.POST);
161         info.setDocumentation("Rename the project");
162         addRequestDocToMethod(info, createQueryParamDoc(
163                 SUFFIX_PROJECT_NAME_TEMPLATE, ParameterStyle.TEMPLATE,
164                 "projectID", true, "xs:integer")
165         );
166         addResponseDocToMethod(info, createResponseDoc(
167                 Status.SUCCESS_NO_CONTENT, "Operation successful",
168                 stringRepresentation())
169         );
170 
171         addResponseDocToMethod(info, createResponseDoc(
172                 Status.CLIENT_ERROR_BAD_REQUEST, "Cannot rename project",
173                 htmlRepresentation())
174         );
175     }
176 
177     /**
178      * {@inheritDoc }
179      */
180     @Override
181     protected void describeDelete(final MethodInfo info) {
182         info.setName(Method.DELETE);
183         info.setDocumentation("Delete a project");
184         addRequestDocToMethod(info, createQueryParamDoc(
185                 SUFFIX_PROJECT_NAME_TEMPLATE, ParameterStyle.TEMPLATE,
186                 "projectID", true, "xs:integer")
187         );
188         addResponseDocToMethod(info, createResponseDoc(
189                 Status.SUCCESS_NO_CONTENT, "Operation successful",
190                 stringRepresentation())
191         );
192 
193         addResponseDocToMethod(info, createResponseDoc(
194                 Status.CLIENT_ERROR_NOT_FOUND, "projectID not found",
195                 htmlRepresentation())
196         );
197 
198         addResponseDocToMethod(info, createResponseDoc(
199                 Status.CLIENT_ERROR_BAD_REQUEST, "Cannot delete the project",
200                 htmlRepresentation())
201         );
202     }
203 
204 }