1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45 public class ManageProjectsResource extends AbstractResource {
46
47
48
49
50
51 public static final String PROJECT_NAME_PARAMETER = "newProjectName";
52
53
54
55
56 private volatile Logger LOG;
57
58
59
60
61 private volatile String suffixProject;
62
63
64
65
66
67
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
82
83
84
85
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
106
107
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
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
141
142
143
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
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
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 }