source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/DepositorAction.java@ 33731

Last change on this file since 33731 was 33731, checked in by kjdon, 4 years ago

now that userContext has teh right info, we don't need to check userinformation anymore

  • Property svn:executable set to *
File size: 16.2 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.Serializable;
7import java.lang.reflect.Type;
8import java.util.ArrayList;
9import java.util.HashMap;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Map;
13
14import javax.xml.parsers.DocumentBuilderFactory;
15import javax.xml.transform.Transformer;
16import javax.xml.transform.TransformerFactory;
17import javax.xml.transform.dom.DOMSource;
18import javax.xml.transform.stream.StreamResult;
19
20import org.apache.commons.io.FileUtils;
21import org.greenstone.gsdl3.util.DerbyWrapper;
22import org.greenstone.gsdl3.util.GSConstants;
23import org.greenstone.gsdl3.util.GSParams;
24import org.greenstone.gsdl3.util.GSXML;
25import org.greenstone.gsdl3.util.GSXSLT;
26import org.greenstone.gsdl3.util.UserContext;
27import org.greenstone.gsdl3.util.XMLConverter;
28import org.greenstone.util.GlobalProperties;
29import org.w3c.dom.Document;
30import org.w3c.dom.Element;
31import org.w3c.dom.Node;
32
33import com.google.gson.Gson;
34import com.google.gson.reflect.TypeToken;
35
36public class DepositorAction extends Action
37{
38 //Sub actions
39 private final String DE_RETRIEVE_WIZARD = "getwizard";
40 private final String DE_DEPOSIT_FILE = "depositfile";
41 private final String DE_CLEAR_CACHE = "clearcache";
42 private final String DE_CLEAR_DATABASE = "cleardatabase";
43
44 // cgi args
45 private final String DE_PAGE_ARG = "dePage";
46 private final String CURRENT_PAGE_ARG = "currentPage";
47 private final String FILE_TO_ADD_ARG = "fileToAdd";
48
49 public Node process(Node message)
50 {
51 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
52 Document doc = request.getOwnerDocument();
53
54 UserContext uc = new UserContext((Element) request);
55
56 Element responseMessage = doc.createElement(GSXML.MESSAGE_ELEM);
57 Element response = GSXML.createBasicResponse(doc, this.getClass().getSimpleName());
58 responseMessage.appendChild(response);
59
60 addSiteMetadata(response, uc);
61 addInterfaceOptions(response);
62
63 String currentUsername = uc.getUsername();
64
65 // logger.debug("username="+username+", groups = "+groups);
66 if (currentUsername == null || currentUsername.equals(""))
67 {
68
69 // TODO if user is not logged in, push to login page
70 request.setAttribute("subaction", "");
71 GSXML.addError(response, "You need to be logged in to use the depositor");
72 return responseMessage;
73 }
74
75 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
76 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
77
78 String collection = (String) params.get(GSParams.COLLECTION);
79
80 if (collection !=null && !collection.equals("")) {
81 if (!userHasCollectionEditPermissions(collection, uc)) {
82 // we need to reset back to empty subaction here
83 request.setAttribute("subaction", "");
84 logger.error("found collection "+collection+", need to check user groups");
85 GSXML.addError(response, "You are not in the right group to access this collection. Please log in as a different user.");
86 return responseMessage;
87
88 }
89 }
90 int pageNum = -1;
91 boolean pageNumParseFail = false;
92 try
93 {
94 pageNum = Integer.parseInt(((String) params.get(DE_PAGE_ARG)));
95 }
96 catch (Exception ex)
97 {
98 pageNumParseFail = true;
99 }
100
101 int prevPageNum = -1;
102 boolean prevPageNumFail = false;
103 try
104 {
105 prevPageNum = Integer.parseInt((String) params.get(CURRENT_PAGE_ARG));
106 }
107 catch (Exception ex)
108 {
109 prevPageNumFail = true;
110 }
111
112 DerbyWrapper database = new DerbyWrapper(GlobalProperties.getGSDL3Home() + File.separatorChar + "etc" + File.separatorChar + "usersDB");
113 if (pageNumParseFail)
114 {
115 try
116 {
117 pageNum = Integer.parseInt(database.getUserData(currentUsername, "DE___" + collection + "___CACHED_PAGE"));
118 }
119 catch (Exception ex)
120 {
121 pageNum = 1;
122 }
123 }
124
125 int highestVisitedPage = -1;
126 String result = "";
127 int counter = 1;
128 while (result != null)
129 {
130 result = database.getUserData(currentUsername, "DE___" + collection + "___" + counter + "___VISITED_PAGE");
131 if (result != null)
132 {
133 counter++;
134 }
135 }
136 highestVisitedPage = counter - 1;
137 if (highestVisitedPage == 0)
138 {
139 highestVisitedPage = 1;
140 }
141
142 if (pageNum > highestVisitedPage + 1)
143 {
144 pageNum = highestVisitedPage + 1;
145 }
146
147 database.addUserData(currentUsername, "DE___" + collection + "___" + pageNum + "___VISITED_PAGE", "VISITED");
148
149 String subaction = ((Element) request).getAttribute(GSXML.SUBACTION_ATT);
150 if (subaction.toLowerCase().equals(DE_RETRIEVE_WIZARD))
151 {
152 //Save given metadata
153 StringBuilder saveString = new StringBuilder("[");
154 Iterator<String> paramIter = params.keySet().iterator();
155 while (paramIter.hasNext())
156 {
157 String paramName = paramIter.next();
158 if (paramName.startsWith(GSParams.MD_PREFIX))
159 {
160 Object paramValue = params.get(paramName);
161
162 if (paramValue instanceof String)
163 {
164 saveString.append("{name:\"" + paramName + "\", value:\"" + (String) paramValue + "\"},");
165 }
166 else if (paramValue instanceof HashMap)
167 {
168 HashMap<String, String> subMap = (HashMap<String, String>) paramValue;
169 Iterator<String> subKeyIter = subMap.keySet().iterator();
170 while (subKeyIter.hasNext())
171 {
172 String subName = subKeyIter.next();
173 saveString.append("{name:\"" + paramName + "." + subName + "\", value:\"" + subMap.get(subName) + "\"},");
174 }
175 }
176 }
177 }
178 if (saveString.length() > 2)
179 {
180 saveString.deleteCharAt(saveString.length() - 1);
181 saveString.append("]");
182
183 if (!prevPageNumFail)
184 {
185 database.addUserData(currentUsername, "DE___" + collection + "___" + prevPageNum + "___CACHED_VALUES", saveString.toString());
186 }
187 }
188
189 //Construct the xsl
190 Document compiledDepositorFile = null;
191 try
192 {
193 compiledDepositorFile = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
194 }
195 catch (Exception ex)
196 {
197 ex.printStackTrace();
198 }
199 Document depositorBaseFile = GSXSLT.mergedXSLTDocumentCascade("depositor/depositor.xsl", (String) this.config_params.get(GSConstants.SITE_NAME), collection, (String) this.config_params.get(GSConstants.INTERFACE_NAME), (ArrayList<String>) this.config_params.get(GSConstants.BASE_INTERFACES), false);
200
201 Element numOfPagesElement = GSXML.getNamedElement(depositorBaseFile.getDocumentElement(), "xsl:variable", "name", "numOfPages");
202 int numberOfPages = Integer.parseInt(numOfPagesElement.getTextContent());
203
204 compiledDepositorFile.appendChild(compiledDepositorFile.importNode(depositorBaseFile.getDocumentElement(), true));
205
206 ArrayList<Document> pageDocs = new ArrayList<Document>();
207 ArrayList<String> pageNames = new ArrayList<String>();
208 for (int i = 1; i <= numberOfPages; i++)
209 {
210 Document page = GSXSLT.mergedXSLTDocumentCascade("depositor/de_page" + i + ".xsl", (String) this.config_params.get(GSConstants.SITE_NAME), collection, (String) this.config_params.get(GSConstants.INTERFACE_NAME), (ArrayList<String>) this.config_params.get(GSConstants.BASE_INTERFACES), false);
211 pageDocs.add(page);
212
213 Element pageTitleElem = (Element) GSXML.getNamedElement(page.getDocumentElement(), "xsl:variable", "name", "title");
214 pageNames.add(pageTitleElem.getTextContent());
215
216 Element wizardPageElem = (Element) GSXML.getNamedElement(page.getDocumentElement(), "xsl:template", "name", "wizardPage");
217 wizardPageElem.setAttribute("name", "wizardPage" + i);
218 compiledDepositorFile.getDocumentElement().appendChild(compiledDepositorFile.importNode(wizardPageElem, true));
219 }
220
221 //Create the wizard bar
222 Element wizardBarTemplate = GSXML.getNamedElement(compiledDepositorFile.getDocumentElement(), "xsl:template", "name", "wizardBar");
223 Element wizardBar = compiledDepositorFile.createElement("ul");
224 wizardBar.setAttribute("id", "wizardBar");
225 wizardBarTemplate.appendChild(wizardBar);
226
227 for (int i = 0; i < pageNames.size(); i++)
228 {
229 String pageName = pageNames.get(i);
230 Element pageLi = compiledDepositorFile.createElement("li");
231 if (pageNum == i + 1)
232 {
233 pageLi.setAttribute("class", "wizardStepLink ui-state-active ui-corner-all");
234 }
235 else if (i + 1 > highestVisitedPage + 1 && i + 1 > pageNum + 1)
236 {
237 pageLi.setAttribute("class", "wizardStepLink ui-state-disabled ui-corner-all");
238 }
239 else
240 {
241 pageLi.setAttribute("class", "wizardStepLink ui-state-default ui-corner-all");
242 }
243 Element link = compiledDepositorFile.createElement("a");
244 pageLi.appendChild(link);
245
246 link.setAttribute(GSXML.HREF_ATT, "javascript:;");
247 link.setAttribute("page", "" + (i + 1));
248 link.appendChild(compiledDepositorFile.createTextNode(pageName));
249 wizardBar.appendChild(pageLi);
250 }
251
252 //Add a call-template call to the appropriate page in the xsl
253 Element mainDePageElem = GSXML.getNamedElement(compiledDepositorFile.getDocumentElement(), "xsl:template", "match", "/page");
254 Element wizardContainer = GSXML.getNamedElement(mainDePageElem, "div", "id", "wizardContainer");
255 Element formContainer = GSXML.getNamedElement(wizardContainer, "form", "name", "depositorform");
256 Element callToPage = compiledDepositorFile.createElement("xsl:call-template");
257 callToPage.setAttribute("name", "wizardPage" + pageNum);
258 formContainer.appendChild(callToPage);
259
260 Element cachedValueElement = doc.createElement("cachedValues");
261 response.appendChild(cachedValueElement);
262 try
263 {
264 for (int i = pageNum; i > 0; i--)
265 {
266 Element page = doc.createElement("pageCache");
267 page.setAttribute("pageNum", "" + i);
268 String cachedValues = database.getUserData(currentUsername, "DE___" + collection + "___" + i + "___CACHED_VALUES");
269 if (cachedValues != null)
270 {
271 page.appendChild(doc.createTextNode(cachedValues));
272 cachedValueElement.appendChild(page);
273 }
274 }
275 }
276 catch (Exception ex)
277 {
278 ex.printStackTrace();
279 }
280
281 try
282 {
283 Transformer transformer = TransformerFactory.newInstance().newTransformer();
284
285 File newFileDir = new File(GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + this.config_params.get(GSConstants.SITE_NAME) + File.separator + "collect" + File.separator + collection + File.separator + "transform" + File.separator + "depositor");
286 newFileDir.mkdirs();
287
288 File newFile = new File(newFileDir, File.separator + "compiledDepositor.xsl");
289
290 //initialize StreamResult with File object to save to file
291 StreamResult sresult = new StreamResult(new FileWriter(newFile));
292 DOMSource source = new DOMSource(compiledDepositorFile);
293 transformer.transform(source, sresult);
294 }
295 catch (Exception ex)
296 {
297 ex.printStackTrace();
298 }
299 database.closeDatabase();
300 }
301 else if (subaction.toLowerCase().equals(DE_DEPOSIT_FILE))
302 {
303 String fileToAdd = (String) params.get(FILE_TO_ADD_ARG);
304 File tempFile = new File(GlobalProperties.getGSDL3Home() + File.separator + "tmp" + File.separator + fileToAdd);
305 if (tempFile.exists())
306 {
307 File newFileLocationDir = new File(GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + this.config_params.get(GSConstants.SITE_NAME) + File.separator + "collect" + File.separator + collection + File.separator + "import" + File.separator + fileToAdd);
308 if (!newFileLocationDir.exists())
309 {
310 newFileLocationDir.mkdir();
311 }
312 File newFileLocation = new File(newFileLocationDir, fileToAdd);
313
314 try
315 {
316 FileUtils.copyFile(tempFile, newFileLocation);
317 }
318 catch (Exception ex)
319 {
320 ex.printStackTrace();
321 GSXML.addError(responseMessage, "Failed to copy the deposited file into the collection.");
322 return responseMessage;
323 }
324
325 HashMap<String, String> metadataMap = new HashMap<String, String>();
326 for (int i = pageNum; i > 0; i--)
327 {
328 String cachedValues = database.getUserData(currentUsername, "DE___" + collection + "___" + i + "___CACHED_VALUES");
329 if (cachedValues != null)
330 {
331 Type type = new TypeToken<List<Map<String, String>>>()
332 {
333 }.getType();
334
335 Gson gson = new Gson();
336 List<Map<String, String>> metadataList = gson.fromJson(cachedValues, type);
337 for (Map<String, String> metadata : metadataList)
338 {
339 metadataMap.put(metadata.get("name"), metadata.get("value"));
340 }
341 }
342 }
343
344 String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\"><DirectoryMetadata><FileSet>";
345 xmlString += "<FileName>.*</FileName><Description>";
346 for (String key : metadataMap.keySet())
347 {
348 xmlString += "<Metadata name=\"" + key.substring("MD___".length()) + "\" mode=\"accumulate\">" + metadataMap.get(key) + "</Metadata>";
349 }
350 xmlString += "</Description></FileSet></DirectoryMetadata>";
351
352 File metadataFile = new File(GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + this.config_params.get(GSConstants.SITE_NAME) + File.separator + "collect" + File.separator + collection + File.separator + "import" + File.separator + fileToAdd + File.separator + "metadata.xml");
353
354 try
355 {
356 BufferedWriter bw = new BufferedWriter(new FileWriter(metadataFile));
357 bw.write(xmlString);
358 bw.close();
359 }
360 catch (Exception ex)
361 {
362 ex.printStackTrace();
363 }
364
365 Element buildMessage = doc.createElement(GSXML.MESSAGE_ELEM);
366 Element buildRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_PROCESS, "ImportCollection", uc);
367 buildMessage.appendChild(buildRequest);
368
369 Element paramListElem = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
370 buildRequest.appendChild(paramListElem);
371
372 Element collectionParam = doc.createElement(GSXML.PARAM_ELEM);
373 paramListElem.appendChild(collectionParam);
374 collectionParam.setAttribute(GSXML.NAME_ATT, GSXML.COLLECTION_ATT);
375 collectionParam.setAttribute(GSXML.VALUE_ATT, collection);
376
377 Element documentsParam = doc.createElement(GSXML.PARAM_ELEM);
378 paramListElem.appendChild(documentsParam);
379 documentsParam.setAttribute(GSXML.NAME_ATT, "documents");
380 documentsParam.setAttribute(GSXML.VALUE_ATT, fileToAdd);
381
382 Element buildResponseMessage = (Element) this.mr.process(buildMessage);
383
384 response.appendChild(doc.importNode(buildResponseMessage, true));
385 }
386 }
387 else if (subaction.toLowerCase().equals(DE_CLEAR_CACHE))
388 {
389 database.clearUserDataWithPrefix(currentUsername, "DE___");
390 }
391 else if (subaction.toLowerCase().equals(DE_CLEAR_DATABASE))
392 {
393 database.clearUserData();
394 database.clearTrackerData();
395 }
396 else
397 {
398 Element depositorPage = doc.createElement("depositorPage");
399 response.appendChild(depositorPage);
400
401 Element collList = getCollectionsInSite(doc);
402 depositorPage.appendChild(doc.importNode(collList, true));
403 }
404
405 return responseMessage;
406 }
407
408 public Element getCollectionsInSite(Document doc)
409 {
410 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
411 Element request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
412 message.appendChild(request);
413 Element responseMessage = (Element) this.mr.process(message);
414
415 Element response = (Element) GSXML.getChildByTagName(responseMessage, GSXML.RESPONSE_ELEM);
416 Element collectionList = (Element) GSXML.getChildByTagName(response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
417
418 return collectionList;
419 }
420
421 // collection must be non-null and non-empty
422 protected boolean userHasCollectionEditPermissions(String collection, UserContext user_context) {
423
424 for (String group : user_context.getGroups()) {
425 // administrator always has permission
426 if (group.equals("administrator")) {
427 return true;
428 }
429 // all-collections-editor can edit any collection
430
431 if (group.equals("all-collections-editor")) {
432 return true;
433 }
434 if (group.equals(collection+"-collection-editor")) {
435 return true;
436 }
437 }
438
439 // haven't found a group with edit permissions
440 return false;
441
442 }
443}
Note: See TracBrowser for help on using the repository browser.