001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * Created Dec 29, 2005
014: * @author wseyler
015: */
016:
017: package org.pentaho.ui.component;
018:
019: import java.io.File;
020: import java.util.Iterator;
021: import java.util.List;
022: import javax.servlet.http.HttpServletRequest;
023: import org.apache.commons.fileupload.DiskFileUpload;
024: import org.apache.commons.fileupload.FileItem;
025: import org.apache.commons.fileupload.FileUploadException;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.dom4j.Document;
029: import org.pentaho.ui.XmlComponent;
030: import org.pentaho.core.repository.ISolutionRepository;
031: import org.pentaho.core.session.IPentahoSession;
032: import org.pentaho.core.solution.HttpRequestParameterProvider;
033: import org.pentaho.core.system.PentahoSystem;
034: import org.pentaho.core.ui.IPentahoUrlFactory;
035: import org.pentaho.messages.Messages;
036:
037: public class SolutionManagerUIComponent extends XmlComponent {
038: /**
039: *
040: */
041: private static final long serialVersionUID = 5322450732426274853L;
042:
043: private static final Log logger = LogFactory
044: .getLog(SolutionManagerUIComponent.class);
045:
046: private static final String PATH_STR = "path"; //$NON-NLS-1$
047:
048: private static final String EMPTY_STR = ""; //$NON-NLS-1$
049:
050: private static final String BASE_URL_STR = "baseUrl"; //$NON-NLS-1$
051:
052: private IPentahoSession session = null;
053:
054: public SolutionManagerUIComponent(IPentahoUrlFactory urlFactory,
055: List messages, IPentahoSession session) {
056: super (urlFactory, messages, null);
057: this .session = session;
058: setXsl("text/xml", "copy.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
059: }
060:
061: public Document doGetSolutionStructure() {
062: ISolutionRepository repository = PentahoSystem
063: .getSolutionRepository(session);
064: Document doc = repository
065: .getSolutionStructure(ISolutionRepository.ACTION_ADMIN);
066: return doc;
067: }
068:
069: public Document doFileUpload() {
070: String baseUrl = PentahoSystem.getApplicationContext()
071: .getSolutionPath(EMPTY_STR);
072: ISolutionRepository repository = PentahoSystem
073: .getSolutionRepository(session);
074: String path = this .getParameter(PATH_STR, null);
075: HttpServletRequest request = ((HttpRequestParameterProvider) getParameterProviders()
076: .get(HttpRequestParameterProvider.SCOPE_REQUEST))
077: .getRequest();
078: String contentType = request.getContentType();
079: if ((contentType == null)
080: || (contentType.indexOf("multipart/form-data") < 0 && contentType.indexOf("multipart/mixed stream") < 0)) { //$NON-NLS-1$ //$NON-NLS-2$
081: return doGetSolutionStructure();
082: }
083: DiskFileUpload uploader = new DiskFileUpload();
084: try {
085: List fileList = uploader.parseRequest(request);
086: Iterator iter = fileList.iterator();
087: while (iter.hasNext()) {
088: FileItem fi = (FileItem) iter.next();
089:
090: // Check if not form field so as to only handle the file inputs
091: if (!fi.isFormField()) {
092: File tempFileRef = new File(fi.getName());
093: repository.addSolutionFile(baseUrl, path,
094: tempFileRef.getName(), fi.get(), true);
095: logger
096: .info(Messages
097: .getString("SolutionManagerUIComponent.INFO_0001_FILE_SAVED") + path + "/" + tempFileRef.getName()); //$NON-NLS-1$ //$NON-NLS-2$
098: }
099: }
100: } catch (FileUploadException e) {
101: logger.error(e.toString());
102: } catch (Exception e) {
103: // TODO Auto-generated catch block
104: e.printStackTrace();
105: }
106:
107: return doGetSolutionStructure();
108: }
109:
110: public Document getXmlContent() {
111: setXslProperty(BASE_URL_STR, urlFactory.getDisplayUrlBuilder()
112: .getUrl());
113: return doFileUpload();
114: }
115:
116: public Log getLogger() {
117: return logger;
118: }
119:
120: public boolean validate() {
121: return true;
122: }
123: }
|