001: /*
002: * Copyright 2005 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 Mar 9, 2006
014: * @author wseyler
015: */
016:
017: package org.pentaho.ui.component;
018:
019: import java.util.Iterator;
020: import java.util.List;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.dom4j.Document;
025: import org.dom4j.DocumentHelper;
026: import org.dom4j.Element;
027: import org.pentaho.core.repository.ISolutionRepository;
028: import org.pentaho.core.session.IPentahoSession;
029: import org.pentaho.core.system.PentahoSystem;
030: import org.pentaho.core.ui.IPentahoUrlFactory;
031: import org.pentaho.messages.Messages;
032: import org.pentaho.ui.XmlComponent;
033:
034: import com.pentaho.repository.dbbased.solution.SolutionRepository;
035:
036: public class LoadDBRepositoryUIComponent extends XmlComponent {
037: private static final String PATH_STR = "path"; //$NON-NLS-1$
038:
039: private static final String ROOT = "root"; //$NON-NLS-1$
040:
041: private static final String RESULT = "result"; //$NON-NLS-1$
042:
043: private static final String TYPE_ATTRIBUTE = "result-type"; //$NON-NLS-1$
044:
045: private static final String SUCCESS = "success"; //$NON-NLS-1$
046:
047: private static final String FAILURE = "failed"; //$NON-NLS-1$
048:
049: private static final String SHOW_INPUT = "show-input"; //$NON-NLS-1$
050:
051: private static final String ORPHANED = "orphaned-files"; //$NON-NLS-1$
052:
053: private static final String FILENAME = "file-name"; //$NON-NLS-1$
054:
055: private static final String ORPHANHANDLING = "orphan-handling"; //$NON-NLS-1$
056:
057: private static final String PATHTITLE = "path-title"; //$NON-NLS-1$
058:
059: private static final String DELETETITLE = "delete-title"; //$NON-NLS-1$
060:
061: IPentahoSession session = null;
062:
063: private static final Log logger = LogFactory
064: .getLog(LoadDBRepositoryUIComponent.class);
065:
066: private static final long serialVersionUID = 1L;
067:
068: public LoadDBRepositoryUIComponent(IPentahoUrlFactory urlFactory,
069: List messages, IPentahoSession session) {
070: super (urlFactory, messages, null);
071: this .session = session;
072: setXsl("text/html", "LoadDBRepository.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
073: setXslProperty(
074: "baseUrl", urlFactory.getDisplayUrlBuilder().getUrl()); //$NON-NLS-1$
075: }
076:
077: private Document doLoad(String solutionRoot, boolean deleteOrphans) {
078:
079: Document document = DocumentHelper.createDocument();
080: document.setName(PATH_STR);
081: Element root = document.addElement(ROOT);
082: Element result = root.addElement(RESULT);
083: boolean usingDbRepository = true;
084: try {
085: ISolutionRepository repository = PentahoSystem
086: .getSolutionRepository(session);
087: if (!(repository instanceof SolutionRepository)) {
088: usingDbRepository = false;
089: repository = new SolutionRepository();
090: }
091: List orphanedFiles = ((SolutionRepository) repository)
092: .loadSolutionFromFileSystem(this .session,
093: solutionRoot, deleteOrphans);
094: result.addAttribute(TYPE_ATTRIBUTE, SUCCESS);
095: if (usingDbRepository) {
096: result
097: .addText(Messages
098: .getString("LoadDBRepositoryUIComponent.INFO_0001_SUCCESS")); //$NON-NLS-1$
099: } else {
100: result
101: .addText(Messages
102: .getString("LoadDBRepositoryUIComponent.INFO_0002_SUCCESS_NEED_CONFIG")); //$NON-NLS-1$
103: }
104: if (orphanedFiles != null && orphanedFiles.size() > 0) {
105: Iterator iter = orphanedFiles.iterator();
106: Element orphans = result.addElement(ORPHANED);
107: orphans
108: .addElement(ORPHANHANDLING)
109: .addText(
110: deleteOrphans ? Messages
111: .getString("LoadDBRepositoryUIComponent.INFO_0004_ORPHANED_DELETED") : Messages.getString("LoadDBRepositoryUIComponent.INFO_0005_ORPHANED_IGNORED")); //$NON-NLS-1$ //$NON-NLS-2$
112: while (iter.hasNext()) {
113: orphans.addElement(FILENAME).addText(
114: ((String) iter.next()));
115: }
116: }
117: } catch (Exception e) {
118: result.addAttribute(TYPE_ATTRIBUTE, FAILURE);
119: result
120: .addText(Messages
121: .getString("LoadDBRepositoryUIComponent.ERROR_0001_LOAD_ERROR") + solutionRoot); //$NON-NLS-1$
122: e.printStackTrace();
123: }
124: return document;
125: }
126:
127: protected Document showInputPage() {
128: Document document = DocumentHelper.createDocument();
129: document.setName(PATH_STR);
130: Element root = document.addElement(RESULT);
131: root.addAttribute(TYPE_ATTRIBUTE, SHOW_INPUT);
132: root
133: .addElement(PATHTITLE)
134: .addText(
135: Messages
136: .getString("LoadDBRepositoryUIComponent.INFO_0003_ENTER_PATH")); //$NON-NLS-1$
137: root
138: .addElement(DELETETITLE)
139: .addText(
140: Messages
141: .getString("LoadDBRepositoryUIComponent.INFO_0006_DELETE_ORPHANS_TITLE")); //$NON-NLS-1$
142: return document;
143: }
144:
145: public Document getXmlContent() {
146:
147: String solutionRoot = getParameter(PATH_STR, null);
148: if (solutionRoot == null || solutionRoot.length() < 1) {
149: return showInputPage();
150: } else {
151: boolean deleteOrphans = "on".equalsIgnoreCase(getParameter("delete", "off")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
152: return doLoad(solutionRoot, deleteOrphans);
153: }
154: }
155:
156: public Log getLogger() {
157: return logger;
158: }
159:
160: public boolean validate() {
161: return true;
162: }
163: }
|