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

Last change on this file since 27617 was 27617, checked in by sjm84, 11 years ago

Various improvements and fixes mostly to do with adding depositor functionality

  • Property svn:executable set to *
File size: 8.9 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.Serializable;
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.Iterator;
9
10import javax.xml.parsers.DocumentBuilderFactory;
11import javax.xml.transform.Transformer;
12import javax.xml.transform.TransformerFactory;
13import javax.xml.transform.dom.DOMSource;
14import javax.xml.transform.stream.StreamResult;
15
16import org.greenstone.gsdl3.util.DerbyWrapper;
17import org.greenstone.gsdl3.util.GSConstants;
18import org.greenstone.gsdl3.util.GSXML;
19import org.greenstone.gsdl3.util.GSXSLT;
20import org.greenstone.gsdl3.util.UserContext;
21import org.greenstone.util.GlobalProperties;
22import org.w3c.dom.Document;
23import org.w3c.dom.Element;
24import org.w3c.dom.Node;
25
26public class DepositorAction extends Action
27{
28 //Sub actions
29 private final String DE_RETRIEVE_WIZARD = "getWizard";
30
31 public Node process(Node message)
32 {
33 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
34
35 UserContext uc = new UserContext((Element) request);
36 String currentUsername = uc.getUsername();
37
38 Element responseMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
39 Element response = GSXML.createBasicResponse(this.doc, this.getClass().getSimpleName());
40 responseMessage.appendChild(response);
41
42 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
43 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
44
45 String collection = (String) params.get("c");
46
47 int pageNum = -1;
48 boolean parseFail = false;
49 try
50 {
51 pageNum = Integer.parseInt(((String) params.get("dePage")));
52 }
53 catch (Exception ex)
54 {
55 parseFail = true;
56 ex.printStackTrace();
57 }
58
59 DerbyWrapper database = new DerbyWrapper();
60 database.connectDatabase(GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + this.config_params.get(GSConstants.SITE_NAME) + File.separatorChar + "etc" + File.separatorChar + "usersDB", false);
61 if (parseFail)
62 {
63 try
64 {
65 pageNum = Integer.parseInt(database.getUserData(currentUsername, "DE___" + collection + "___CACHED_PAGE"));
66 }
67 catch (Exception ex)
68 {
69 pageNum = 1;
70 }
71 }
72
73 String subaction = ((Element) request).getAttribute(GSXML.SUBACTION_ATT);
74 if (subaction.equals(DE_RETRIEVE_WIZARD))
75 {
76 //Save given metadata
77 StringBuilder saveString = new StringBuilder("[");
78 Iterator<String> paramIter = params.keySet().iterator();
79 while (paramIter.hasNext())
80 {
81 String paramName = paramIter.next();
82 if (paramName.startsWith("md___"))
83 {
84 Object paramValue = params.get(paramName);
85
86 if (paramValue instanceof String)
87 {
88 saveString.append("{name:\"" + paramName + "\", value:\"" + (String) paramValue + "\"},");
89 }
90 else if (paramValue instanceof HashMap)
91 {
92 HashMap<String, String> subMap = (HashMap<String, String>) paramValue;
93 Iterator<String> subKeyIter = subMap.keySet().iterator();
94 while (subKeyIter.hasNext())
95 {
96 String subName = subKeyIter.next();
97 saveString.append("{name:\"" + paramName + "." + subName + "\", value:\"" + subMap.get(subName) + "\"},");
98 }
99 }
100 }
101 }
102 if (saveString.length() > 2)
103 {
104 saveString.deleteCharAt(saveString.length() - 1);
105 saveString.append("]");
106
107 database.addUserData(currentUsername, "DE___" + collection + "___" + pageNum + "___CACHED_VALUES", saveString.toString());
108 }
109
110 //Construct the xsl
111 Document compiledDepositorFile = null;
112 try
113 {
114 compiledDepositorFile = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
115 }
116 catch (Exception ex)
117 {
118 ex.printStackTrace();
119 }
120 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);
121
122 Element numOfPagesElement = GSXML.getNamedElement(depositorBaseFile.getDocumentElement(), "xsl:variable", "name", "numOfPages");
123 int numberOfPages = Integer.parseInt(numOfPagesElement.getTextContent());
124
125 compiledDepositorFile.appendChild(compiledDepositorFile.importNode(depositorBaseFile.getDocumentElement(), true));
126
127 ArrayList<Document> pageDocs = new ArrayList<Document>();
128 ArrayList<String> pageNames = new ArrayList<String>();
129 for (int i = 1; i <= numberOfPages; i++)
130 {
131 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);
132 pageDocs.add(page);
133
134 Element pageTitleElem = (Element) GSXML.getNamedElement(page.getDocumentElement(), "xsl:variable", "name", "title");
135 pageNames.add(pageTitleElem.getTextContent());
136
137 Element wizardPageElem = (Element) GSXML.getNamedElement(page.getDocumentElement(), "xsl:template", "name", "wizardPage");
138 wizardPageElem.setAttribute("name", "wizardPage" + i);
139 compiledDepositorFile.getDocumentElement().appendChild(compiledDepositorFile.importNode(wizardPageElem, true));
140 }
141
142 //Create the wizard bar
143 Element wizardBarTemplate = GSXML.getNamedElement(compiledDepositorFile.getDocumentElement(), "xsl:template", "name", "wizardBar");
144 Element wizardBar = compiledDepositorFile.createElement("ul");
145 wizardBar.setAttribute("id", "wizardBar");
146 wizardBarTemplate.appendChild(wizardBar);
147
148 for (int i = 0; i < pageNames.size(); i++)
149 {
150 String pageName = pageNames.get(i);
151 Element pageLi = compiledDepositorFile.createElement("li");
152 if (pageNum == i + 1)
153 {
154 pageLi.setAttribute("class", "wizardStepLink ui-state-active ui-corner-all");
155 }
156 else
157 {
158 pageLi.setAttribute("class", "wizardStepLink ui-state-default ui-corner-all");
159 }
160 Element link = compiledDepositorFile.createElement("a");
161 pageLi.appendChild(link);
162
163 link.setAttribute(GSXML.HREF_ATT, "javascript:;");
164 link.setAttribute("page", "" + (i + 1));
165 link.appendChild(compiledDepositorFile.createTextNode(pageName));
166 wizardBar.appendChild(pageLi);
167 }
168
169 //Add a call-template call to the appropriate page in the xsl
170 Element mainDePageElem = GSXML.getNamedElement(compiledDepositorFile.getDocumentElement(), "xsl:template", "match", "/page");
171 Element wizardContainer = GSXML.getNamedElement(mainDePageElem, "div", "id", "wizardContainer");
172 Element formContainer = GSXML.getNamedElement(wizardContainer, "form", "name", "depositorform");
173 Element callToPage = compiledDepositorFile.createElement("xsl:call-template");
174 callToPage.setAttribute("name", "wizardPage" + pageNum);
175 formContainer.appendChild(callToPage);
176
177 Element cachedValueElement = this.doc.createElement("cachedValues");
178 response.appendChild(cachedValueElement);
179 try
180 {
181 for (int i = pageNum; i > 0; i--)
182 {
183 Element page = this.doc.createElement("pageCache");
184 page.setAttribute("pageNum", "" + pageNum);
185 String cachedValues = database.getUserData(currentUsername, "DE___" + collection + "___" + i + "___CACHED_VALUES");
186 page.appendChild(this.doc.createTextNode(cachedValues));
187 cachedValueElement.appendChild(page);
188 }
189 }
190 catch (Exception ex)
191 {
192 ex.printStackTrace();
193 }
194
195 try
196 {
197 Transformer transformer = TransformerFactory.newInstance().newTransformer();
198
199 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");
200 newFileDir.mkdirs();
201
202 File newFile = new File(newFileDir, File.separator + "compiledDepositor.xsl");
203
204 //initialize StreamResult with File object to save to file
205 StreamResult sresult = new StreamResult(new FileWriter(newFile));
206 DOMSource source = new DOMSource(compiledDepositorFile);
207 transformer.transform(source, sresult);
208 }
209 catch (Exception ex)
210 {
211 ex.printStackTrace();
212 }
213 database.closeDatabase();
214 }
215
216 return responseMessage;
217 }
218
219 public Element getCollectionsInSite()
220 {
221 Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
222 Element request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", new UserContext());
223 message.appendChild(request);
224 Element responseMessage = (Element) this.mr.process(message);
225
226 Element response = (Element) GSXML.getChildByTagName(responseMessage, GSXML.RESPONSE_ELEM);
227 Element collectionList = (Element) GSXML.getChildByTagName(response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
228
229 return collectionList;
230 }
231}
Note: See TracBrowser for help on using the repository browser.