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 Jul 13, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.plugin.mql;
019:
020: import java.io.InputStream;
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.dom4j.Document;
028: import org.dom4j.Element;
029: import org.dom4j.Node;
030: import org.pentaho.core.publisher.BasePublisher;
031: import org.pentaho.core.repository.ISolutionRepository;
032: import org.pentaho.core.session.IPentahoSession;
033: import org.pentaho.core.system.PentahoSystem;
034: import org.pentaho.pms.core.CWM;
035: import org.pentaho.messages.Messages;
036:
037: public class MetadataPublisher extends BasePublisher {
038:
039: private static final long serialVersionUID = 1843038346011563927L;
040:
041: private static final Log logger = LogFactory
042: .getLog(MetadataPublisher.class);
043:
044: public static final int NO_ERROR = 0;
045: public static final int UNABLE_TO_DELETE = (int) Math.pow(2, 0);
046: public static final int UNABLE_TO_IMPORT = (int) Math.pow(2, 1);
047: public static final int NO_META = (int) Math.pow(2, 2);
048:
049: private static int numberUpdated = 0;
050:
051: public Log getLogger() {
052: return logger;
053: }
054:
055: public String getName() {
056: return Messages
057: .getString("MetadataPublisher.USER_PUBLISHER_NAME"); //$NON-NLS-1$
058: }
059:
060: public String getDescription() {
061: return Messages
062: .getString("MetadataPublisher.USER_PUBLISHER_DESCRIPTION"); //$NON-NLS-1$
063: }
064:
065: public String publish(IPentahoSession session) {
066: numberUpdated = 0;
067: List messages = new ArrayList();
068: int result = loadAllMetadata(session, true);
069: if (result == NO_ERROR) {
070: return Messages
071: .getString(
072: "MetadataPublisher.USER_METADATA_RELOADED", Integer.toString(numberUpdated)); //$NON-NLS-1$
073: }
074: if ((result & UNABLE_TO_DELETE) == UNABLE_TO_DELETE) {
075: messages
076: .add(Messages
077: .getString("MetadataPublisher.USER_DELETE_META_FAILED")); //$NON-NLS-1$
078: }
079: if ((result & UNABLE_TO_IMPORT) == UNABLE_TO_IMPORT) {
080: messages
081: .add(Messages
082: .getString("MetadataPublisher.USER_IMPORT_META_FAILED")); //$NON-NLS-1$
083: }
084: // if ((result & NO_META) == NO_META) {
085: // messages.add(Messages.getString("MetadataPublisher.USER_SOME_RELOAD_FAILED")); //$NON-NLS-1$
086: // }
087: StringBuffer buffer = new StringBuffer();
088: buffer.append("<small>"); //$NON-NLS-1$
089: Iterator iter = messages.iterator();
090: while (iter.hasNext()) {
091: buffer.append("<br/>" + iter.next().toString()); //$NON-NLS-1$
092: }
093: buffer
094: .append("<br/><b>" + Messages.getString("MetadataPublisher.INFO_0001_CHECK_LOG") + "</b></small>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
095: return buffer.toString();
096: }
097:
098: public static int loadAllMetadata(IPentahoSession session,
099: boolean forceLoad) {
100: ISolutionRepository repo = PentahoSystem
101: .getSolutionRepository(session);
102: Document doc = repo
103: .getSolutions(ISolutionRepository.ACTION_EXECUTE);
104: List nodes = doc
105: .selectNodes("/repository/file[@type='FILE.FOLDER']"); //$NON-NLS-1$
106: String solution;
107: Iterator it = nodes.iterator();
108: int allSuccess = NO_ERROR;
109: while (it.hasNext()) {
110: Node elem = ((Element) it.next())
111: .selectSingleNode("solution"); //$NON-NLS-1$
112: if (elem != null) {
113: solution = elem.getText();
114: allSuccess |= loadMetadata(solution, session, forceLoad);
115: }
116: }
117: return allSuccess;
118: }
119:
120: public static int loadMetadata(String solution,
121: IPentahoSession session, boolean forceLoad) {
122: int result = NO_ERROR;
123: String resourceName;
124: InputStream xmiInputStream;
125: resourceName = solution + "/metadata.xmi"; //$NON-NLS-1$
126: xmiInputStream = null;
127: ISolutionRepository repo = PentahoSystem
128: .getSolutionRepository(session);
129: if (repo.resourceExists(resourceName)) {
130: try {
131: // try to delete the old one...
132: String[] modelNames = CWM.getDomainNames();
133: for (int i = 0; i < modelNames.length; i++) {
134: if (modelNames[i].equals(solution) && forceLoad) {
135: // it already exists
136: logger
137: .info(Messages
138: .getString(
139: "MetadataPublisher.INFO_DELETING_METADATA", solution)); //$NON-NLS-1$
140: CWM delCwm = CWM.getInstance(solution);
141: delCwm.removeDomain();
142: }
143: }
144: } catch (Throwable t) {
145: logger
146: .error(
147: Messages
148: .getString(
149: "MetadataPublisher.ERROR_0001_COULD_NOT_DELETE", resourceName), t); //$NON-NLS-1$
150: result |= UNABLE_TO_DELETE;
151: }
152:
153: CWM cwm = CWM.getInstance(solution);
154: if (cwm.getSchemas().length > 0 && !forceLoad) {
155: logger
156: .debug(Messages
157: .getString(
158: "MetadataPublisher.DEBUG_ALREADY_LOADED", resourceName)); //$NON-NLS-1$
159: return result;
160: }
161: try {
162: logger
163: .info(Messages
164: .getString(
165: "MetadataPublisher.INFO_IMPORTING_METADATA", solution)); //$NON-NLS-1$
166: xmiInputStream = repo.getResourceInputStream(
167: resourceName, true);
168: cwm.importFromXMI(xmiInputStream);
169: return result;
170:
171: } catch (Throwable t) {
172: logger
173: .error(
174: Messages
175: .getString(
176: "MetadataPublisher.ERROR_0002_COULD_NOT_IMPORT", resourceName), t); //$NON-NLS-1$
177: } finally {
178: if (xmiInputStream != null) {
179: try {
180: xmiInputStream.close();
181: numberUpdated++;
182: return result;
183: } catch (Throwable t) {
184: logger
185: .error(
186: Messages
187: .getString(
188: "MetadataPublisher.ERROR_0002_COULD_NOT_IMPORT", resourceName), t); //$NON-NLS-1$
189: return result | UNABLE_TO_IMPORT;
190: }
191: }
192: }
193: } else {
194: return result;
195: }
196: return result;
197: }
198:
199: }
|