001: package org.enhydra.shark.webclient.presentation;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005:
006: import javax.xml.transform.Transformer;
007: import javax.xml.transform.TransformerConfigurationException;
008: import javax.xml.transform.TransformerFactory;
009: import javax.xml.transform.dom.DOMSource;
010: import javax.xml.transform.stream.StreamResult;
011:
012: import org.enhydra.shark.api.RootException;
013: import org.enhydra.shark.utilities.MiscUtilities;
014: import org.enhydra.shark.webclient.business.SharkUtils;
015: import org.enhydra.shark.webclient.business.XMLRepositoryBuilder;
016: import org.enhydra.shark.webclient.presentation.utils.NavigConsts;
017: import org.enhydra.shark.webclient.spec.MultipartResolver;
018: import org.enhydra.shark.webclient.spec.SharkParamConsts;
019: import org.enhydra.shark.webclient.spec.utils.ParamConsts;
020: import org.w3c.dom.Node;
021:
022: import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException;
023: import com.lutris.appserver.server.httpPresentation.HttpPresentationException;
024:
025: public class RepositoryHandlerPO extends BasePO {
026:
027: public Node handleDefault() throws HttpPresentationException {
028:
029: return showPage();
030: }
031:
032: public Node handleRefresh() throws HttpPresentationException {
033:
034: return showPage();
035: }
036:
037: public Node handleSort() throws HttpPresentationException {
038: return showPage();
039: }
040:
041: public Node handleDelete() throws HttpPresentationException {
042:
043: String name = myComms.request
044: .getParameter(ParamConsts.DEL_NAME);
045: String from_element = myComms.request.getParameter("from");
046: String sortCriterion = myComms.request
047: .getParameter("sortCriterion");
048: String sortOrder = myComms.request.getParameter("sortAsc");
049:
050: name = name.replace('/', '\\');
051: try {
052: if (sortCriterion == null) {
053: sortCriterion = "";
054: }
055: String sortAsc = "true";
056: if (sortOrder != null
057: && (sortOrder.equalsIgnoreCase("true") || sortOrder
058: .equalsIgnoreCase("false"))) {
059: sortAsc = sortOrder;
060: }
061:
062: String xpdlRepository;
063:
064: xpdlRepository = myComms.application.getConfig().getString(
065: "SharkWebClient."
066: + ParamConsts.PROPERTY_PREFIX_REPOSITORY
067: + ".XPDL_repository");
068: String path = xpdlRepository + File.separator + name;
069: File pkgFile = new File(path);
070: if (!pkgFile.exists() || pkgFile.isDirectory()) {
071: System.err
072: .println("RepositoryManager -> deleting of the file "
073: + path
074: + " failed because it does not exist, or it is a directory");
075: throw new Exception(
076: "RepositoryManager -> deleting of the file "
077: + path
078: + " failed because it does not exist, or it is a directory");
079: }
080: boolean isDeleted = false;
081: try {
082: isDeleted = pkgFile.delete();
083: } catch (Exception ex) {
084: System.err
085: .println("RepositoryManager -> deleting of the file "
086: + path + " failed - it might be locked");
087: throw new Exception(
088: "RepositoryManager -> deleting of the file "
089: + path + " failed - it might be locked");
090: }
091: if (!isDeleted) {
092: throw new Exception(
093: "RepositoryManager -> deleting of the file "
094: + path + " NOT deleted");
095: }
096: System.out.println("RepositoryManager -> file " + path
097: + " is successfully deleted from repository");
098:
099: throw new ClientPageRedirectException(
100: NavigConsts.REPOSITORY_HANDLER + "?from="
101: + from_element + "&sortCriterion="
102: + sortCriterion + "&sortAsc=" + sortAsc
103: + "&name=" + name);
104: } catch (Exception e) {
105: e.printStackTrace();
106: throw new PresentationException("Problems with delete ", e);
107: }
108: }
109:
110: public Node handleConvert() throws HttpPresentationException {
111:
112: String name = myComms.request
113: .getParameter(ParamConsts.CONV_NAME);
114: String from_element = myComms.request.getParameter("from");
115: String sortCriterion = myComms.request
116: .getParameter("sortCriterion");
117: String sortOrder = myComms.request.getParameter("sortAsc");
118:
119: name = name.replace('/', '\\');
120: try {
121: if (sortCriterion == null) {
122: sortCriterion = "";
123: }
124: String sortAsc = "true";
125: if (sortOrder != null
126: && (sortOrder.equalsIgnoreCase("true") || sortOrder
127: .equalsIgnoreCase("false"))) {
128: sortAsc = sortOrder;
129: }
130:
131: String xpdlRepository;
132:
133: xpdlRepository = myComms.application.getConfig().getString(
134: "SharkWebClient."
135: + ParamConsts.PROPERTY_PREFIX_REPOSITORY
136: + ".XPDL_repository");
137: String path = xpdlRepository + File.separator + name;
138: File arisFile = new File(path);
139: if (!arisFile.exists() || arisFile.isDirectory()) {
140: System.err
141: .println("RepositoryManager -> converting of the file "
142: + path
143: + " failed because it does not exist, or it is a directory");
144: throw new Exception(
145: "RepositoryManager -> converting of the file "
146: + path
147: + " failed because it does not exist, or it is a directory");
148: }
149:
150: String export2xml = myComms.application
151: .getConfig()
152: .getString(
153: "SharkWebClient."
154: + ParamConsts.PROPERTY_PREFIX_REPOSITORY
155: + ".ARIS_2_XPDL");
156:
157: File xsltfile = new File(export2xml);
158: if (!xsltfile.exists()) {
159: throw new Exception("Xslt file doesn't exists");
160: }
161: Node n = (Node) MiscUtilities.transform(arisFile,
162: export2xml, Node.class, null);
163:
164: File f = new File(arisFile.getAbsolutePath().replaceAll(
165: ParamConsts.ARIS_EXTENSION,
166: ParamConsts.XPDL_EXTENSION));
167:
168: convertNodeToFile(n, f);
169:
170: throw new ClientPageRedirectException(
171: NavigConsts.REPOSITORY_HANDLER + "?from="
172: + from_element + "&sortCriterion="
173: + sortCriterion + "&sortAsc=" + sortAsc
174: + "&name=" + name);
175: } catch (Exception e) {
176: e.printStackTrace();
177: throw new PresentationException("Problems with delete ", e);
178: }
179: }
180:
181: protected Node showPage() throws HttpPresentationException {
182:
183: try {
184:
185: String pagingFrom = myComms.request.getParameter("from");
186: String sortCriterion = myComms.request
187: .getParameter("sortCriterion");
188: String sortOrder = myComms.request.getParameter("sortAsc");
189:
190: if (sortCriterion == null) {
191: sortCriterion = "";
192: }
193: String sortAsc = "true";
194: if (sortOrder != null
195: && (sortOrder.equalsIgnoreCase("true") || sortOrder
196: .equalsIgnoreCase("false"))) {
197: sortAsc = sortOrder;
198: }
199:
200: XMLRepositoryBuilder xmlImp = new XMLRepositoryBuilder(
201: getUsername(), myComms.application.getConfig(),
202: pagingFrom, sortCriterion, sortAsc);
203: return xmlImp.getResult(xsltParamMap);
204: } catch (Exception ex) {
205: ex.printStackTrace();
206: throw new PresentationException("Can't show page", ex);
207: }
208: }
209:
210: public Node handleUpload() throws PresentationException {
211: try {
212:
213: String path = myComms.application.getConfig().getString(
214: "SharkWebClient."
215: + ParamConsts.PROPERTY_PREFIX_REPOSITORY
216: + ".XPDL_repository");
217: MultipartResolver mpr = new MultipartResolver(myComms,
218: path, "UTF-8", "");
219:
220: String newname = mpr.getParameter("newname").trim();
221:
222: boolean condition = !newname
223: .endsWith(ParamConsts.XPDL_EXTENSION)
224: && !newname.equalsIgnoreCase("");
225:
226: if (SharkUtils.sharkEdition
227: .equals(SharkParamConsts.SHARK_EDITION_PROFESSIONAL)) {
228: condition = condition
229: && !newname
230: .endsWith(ParamConsts.ARIS_EXTENSION);
231: }
232:
233: if (condition) {
234: newname += ParamConsts.XPDL_EXTENSION;
235: }
236: String fullPath = path + File.separator + newname;
237: File pkgFile = null;
238:
239: if (!newname.equalsIgnoreCase("")) {
240: pkgFile = new File(fullPath);
241: File parentFile = pkgFile.getParentFile();
242: // if the file exists, or the parent file exists and it is not directory ->
243: // can't
244: // create
245: if (pkgFile.exists()
246: || (parentFile.exists() && !parentFile
247: .isDirectory())) {
248: System.err
249: .println("RepositoryManager -> upload of the file "
250: + path
251: + " failed because it already exists or its parent is not directory");
252: throw new Exception(
253: "RepositoryManager -> upload of the file "
254: + path
255: + " failed because it already exists or its parent is not directory");
256: }
257:
258: if (!parentFile.exists()) {
259: try {
260: parentFile.mkdirs();
261: } catch (Exception ex) {
262: throw new RootException(ex);
263: }
264: }
265: }
266:
267: String[] filenames = mpr.getAllUploadFileNames();
268: File uploadedFile = null;
269: if (filenames != null && filenames.length > 0) {
270: uploadedFile = new File(filenames[0]);
271: if (pkgFile != null) {
272: uploadedFile.renameTo(pkgFile);
273: }
274:
275: }
276:
277: throw new ClientPageRedirectException(
278: NavigConsts.REPOSITORY_HANDLER);
279: } catch (Exception ex) {
280:
281: throw new PresentationException(
282: "Error while deploy product", ex);
283:
284: }
285:
286: }
287:
288: public String getXSLTransformation() throws Exception {
289: return getXSLTTransformation(ParamConsts.PROPERTY_PREFIX_REPOSITORY);
290: }
291:
292: private void convertNodeToFile(Node n, File f) throws Exception {
293:
294: try {
295:
296: if (!f.exists()) {
297: f.createNewFile();
298: }
299:
300: DOMSource source = new DOMSource(n);
301: FileOutputStream fos = new FileOutputStream(f);
302: Transformer transformer = null;
303: TransformerFactory transformerFactory = TransformerFactory
304: .newInstance();
305: try {
306: transformer = transformerFactory.newTransformer();
307: } catch (TransformerConfigurationException e) {
308: System.out.println("Transformer configuration error: "
309: + e.getMessage());
310:
311: }
312: StreamResult result = new StreamResult(fos);
313: transformer.transform(source, result);
314: fos.close();
315: } catch (Exception e) {
316: throw e;
317: }
318:
319: }
320: }
|