001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.batch.web;
018:
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.log4j.Logger;
031: import org.apache.struts.action.Action;
032: import org.apache.struts.action.ActionForm;
033: import org.apache.struts.action.ActionForward;
034: import org.apache.struts.action.ActionMapping;
035: import org.apache.struts.upload.FormFile;
036:
037: import edu.iu.uis.eden.KEWServiceLocator;
038: import edu.iu.uis.eden.batch.CompositeXmlDocCollection;
039: import edu.iu.uis.eden.batch.FileXmlDocCollection;
040: import edu.iu.uis.eden.batch.XmlDoc;
041: import edu.iu.uis.eden.batch.XmlDocCollection;
042: import edu.iu.uis.eden.batch.ZipXmlDocCollection;
043: import edu.iu.uis.eden.util.Utilities;
044: import edu.iu.uis.eden.web.UserLoginFilter;
045:
046: /**
047: * Struts action that accepts uploaded files and feeds them to the XmlIngesterService
048: * @see edu.iu.uis.eden.batch.XmlIngesterService
049: * @see edu.iu.uis.eden.batch.web.IngesterForm
050: * @author Aaron Hamid (arh14 at cornell dot edu)
051: */
052: public class IngesterAction extends Action {
053: private static final Logger LOG = Logger
054: .getLogger(IngesterAction.class);
055:
056: public ActionForward execute(ActionMapping mapping,
057: ActionForm form, HttpServletRequest request,
058: HttpServletResponse response) throws Exception {
059:
060: LOG.debug(request.getMethod());
061: if (!"post".equals(request.getMethod().toLowerCase())) {
062: LOG.debug("returning to view");
063: return mapping.findForward("view");
064: }
065:
066: IngesterForm iform = (IngesterForm) form;
067:
068: List messages = new ArrayList();
069: List tempFiles = new ArrayList();
070: try {
071: Collection files = iform.getFiles();
072: List collections = new ArrayList(files.size());
073: LOG.debug(files);
074: LOG.debug("" + files.size());
075:
076: Iterator it = files.iterator();
077: while (it.hasNext()) {
078: FormFile file = (FormFile) it.next();
079: if (file.getFileName() == null
080: || file.getFileName().length() == 0)
081: continue;
082: if (file.getFileData() == null) {
083: messages.add("File '" + file.getFileName()
084: + "' contained no data");
085: continue;
086: }
087: LOG.debug("Processing file: " + file.getFileName());
088: // ok, we have to copy it to *another* file because Struts doesn't give us a File
089: // reference (which itself is not a bad abstraction) and XmlDocs based on ZipFile
090: // can't be constructed without a file reference.
091: FileOutputStream fos = null;
092: File temp = null;
093: try {
094: temp = File.createTempFile("ingester", null);
095: tempFiles.add(temp);
096: fos = new FileOutputStream(temp);
097: fos.write(file.getFileData());
098: } catch (IOException ioe) {
099: messages.add("Error copying file data for '"
100: + file.getFileName() + "': " + ioe);
101: continue;
102: } finally {
103: if (fos != null)
104: try {
105: fos.close();
106: } catch (IOException ioe) {
107: LOG.error(
108: "Error closing temp file output stream: "
109: + temp, ioe);
110: }
111: }
112: if (file.getFileName().toLowerCase().endsWith(".zip")) {
113: try {
114: collections.add(new ZipXmlDocCollection(temp));
115: } catch (IOException ioe) {
116: String message = "Unable to load file: " + file;
117: LOG.error(message);
118: messages.add(message);
119: }
120: } else if (file.getFileName().endsWith(".xml")) {
121: collections.add(new FileXmlDocCollection(temp, file
122: .getFileName()));
123: } else {
124: messages.add("Ignoring extraneous file: "
125: + file.getFileName());
126: }
127: }
128:
129: if (collections.size() == 0) {
130: String message = "No valid files to ingest";
131: LOG.debug(message);
132: messages.add(message);
133: } else {
134: // wrap in composite collection to make transactional
135: CompositeXmlDocCollection compositeCollection = new CompositeXmlDocCollection(
136: collections);
137: int totalProcessed = 0;
138: List<XmlDocCollection> c = new ArrayList<XmlDocCollection>(
139: 1);
140: c.add(compositeCollection);
141: try {
142: Collection failed = KEWServiceLocator
143: .getXmlIngesterService().ingest(
144: c,
145: UserLoginFilter.getUserSession(
146: request).getWorkflowUser());
147: boolean txFailed = failed.size() > 0;
148: if (txFailed) {
149: messages.add("Ingestion failed");
150: }
151: it = collections.iterator();
152: while (it.hasNext()) {
153: XmlDocCollection collection = (XmlDocCollection) it
154: .next();
155: List docs = collection.getXmlDocs();
156: Iterator docIt = docs.iterator();
157: while (docIt.hasNext()) {
158: XmlDoc doc = (XmlDoc) docIt.next();
159: if (doc.isProcessed()) {
160: if (!txFailed) {
161: totalProcessed++;
162: messages
163: .add("Ingested xml doc: "
164: + doc.getName()
165: + (doc
166: .getProcessingMessage() == null ? ""
167: : "\n"
168: + doc
169: .getProcessingMessage()));
170: } else {
171: messages
172: .add("Rolled back doc: "
173: + doc.getName()
174: + (doc
175: .getProcessingMessage() == null ? ""
176: : "\n"
177: + doc
178: .getProcessingMessage()));
179: }
180: } else {
181: messages
182: .add("Failed to ingest xml doc: "
183: + doc.getName()
184: + (doc
185: .getProcessingMessage() == null ? ""
186: : "\n"
187: + doc
188: .getProcessingMessage()));
189: }
190: }
191: }
192: } catch (Exception e) {
193: String message = "Error during ingest";
194: LOG.error(message, e);
195: messages.add(message + ": " + e + ":\n"
196: + Utilities.collectStackTrace(e));
197: }
198: if (totalProcessed == 0) {
199: String message = "No xml docs ingested";
200: LOG.debug(message);
201: messages.add(message);
202: }
203: }
204: } finally {
205: if (tempFiles.size() > 0) {
206: Iterator it = tempFiles.iterator();
207: while (it.hasNext()) {
208: File temp = (File) it.next();
209: if (!temp.delete()) {
210: LOG.warn("Error deleting temp file: " + temp);
211: }
212: }
213: }
214: }
215:
216: request.setAttribute("messages", messages);
217: return mapping.findForward("view");
218: }
219: }
|