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: package org.pentaho.designstudio.util;
014:
015: import java.io.File;
016: import java.io.FileFilter;
017:
018: import org.eclipse.core.resources.IFile;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.jface.dialogs.MessageDialog;
022: import org.eclipse.swt.widgets.Shell;
023: import org.pentaho.designstudio.messages.Messages;
024:
025: public class SolutionHelper {
026: IFile actionFile = null;
027:
028: String serverURL = ""; //$NON-NLS-1$
029: String baseUrl;
030:
031: String solutionName;
032: String actionPath;
033: String actionName;
034:
035: public SolutionHelper(IFile actionFile, Shell shell) {
036: this .actionFile = actionFile;
037:
038: solutionName = actionPath = actionName = ""; //$NON-NLS-1$
039: if (!setPathAndSolution(actionFile)) {
040: MessageDialog
041: .openError(
042: shell,
043: Messages
044: .getString("SolutionHelper.ERROR_0001_SOLUTION_NOT_FOUND_DLG_LABEL"), Messages.getString("SolutionHelper.ERROR_0001_SOLUTION_NOT_FOUND_DLG_MSG")); //$NON-NLS-1$ //$NON-NLS-2$
045: }
046:
047: }
048:
049: boolean setPathAndSolution(IFile file) {
050: IPath path = file.getRawLocation();
051: File theFile = path.toFile();
052: if (!theFile.isDirectory()) {
053: theFile = theFile.getParentFile();
054: }
055: path = new Path(theFile.getAbsolutePath());
056:
057: //String lastDirName = theFile.getName();
058: while (theFile != null) {
059: File fList[] = theFile.listFiles(new FileFilter() {
060: public boolean accept(File pathname) {
061: return (pathname.isDirectory() && "system".equalsIgnoreCase(pathname.getName())); //$NON-NLS-1$
062: }
063: });
064: if (fList.length > 0) {
065: for (int i = 0; i < fList.length; ++i) {
066: System.out.print(fList[i]);
067: File sysFile = fList[i];
068: File fSysList[] = sysFile
069: .listFiles(new FileFilter() {
070: public boolean accept(File pathname) {
071: return (!pathname.isDirectory() && "pentaho.xml".equalsIgnoreCase(pathname.getName())); //$NON-NLS-1$
072: }
073: });
074:
075: if (fSysList.length > 0) {
076: IPath documentPath = path.removeFirstSegments(
077: path.matchingFirstSegments(new Path(
078: theFile.getAbsolutePath())))
079: .setDevice(null);
080: String segments[] = documentPath.segments();
081: solutionName = (segments.length > 0) ? documentPath
082: .segment(0)
083: : ""; //$NON-NLS-1$
084: actionPath = (segments.length > 1) ? documentPath
085: .removeFirstSegments(1).toString()
086: : ""; //$NON-NLS-1$
087: actionName = file.getName();
088: return (true);
089: }
090: }
091: }
092: //lastDirName = theFile.getName();
093: theFile = theFile.getParentFile();
094: }
095:
096: return (false);
097: }
098:
099: public static String getSolutionRoot(String filePath) {
100: String solutionRoot = null;
101: File theFile = new File(filePath);
102: if (!theFile.isDirectory()) {
103: theFile = theFile.getParentFile();
104: }
105: Path path = new Path(theFile.getAbsolutePath());
106:
107: while (theFile != null) {
108: File fList[] = theFile.listFiles(new FileFilter() {
109: public boolean accept(File pathname) {
110: return (pathname.isDirectory() && "system".equalsIgnoreCase(pathname.getName())); //$NON-NLS-1$
111: }
112: });
113: if (fList.length > 0) {
114: for (int i = 0; i < fList.length; ++i) {
115: System.out.print(fList[i]);
116: File sysFile = fList[i];
117: File fSysList[] = sysFile
118: .listFiles(new FileFilter() {
119: public boolean accept(File pathname) {
120: return (!pathname.isDirectory() && "pentaho.xml".equalsIgnoreCase(pathname.getName())); //$NON-NLS-1$
121: }
122: });
123:
124: if (fSysList.length > 0) {
125: int matchingSegments = path
126: .matchingFirstSegments(new Path(theFile
127: .getAbsolutePath())) + 1;
128: solutionRoot = path.removeLastSegments(
129: path.segmentCount() - matchingSegments)
130: .toString();
131: break;
132: }
133: }
134: }
135: //lastDirName = theFile.getName();
136: theFile = theFile.getParentFile();
137: }
138:
139: return solutionRoot;
140: }
141:
142: public static String getSolutionName(String filePath) {
143: String solutionName = null;
144: File theFile = new File(filePath);
145: if (!theFile.isDirectory()) {
146: theFile = theFile.getParentFile();
147: }
148: Path path = new Path(theFile.getAbsolutePath());
149:
150: //String lastDirName = theFile.getName();
151: while (theFile != null) {
152: File fList[] = theFile.listFiles(new FileFilter() {
153: public boolean accept(File pathname) {
154: return (pathname.isDirectory() && "system".equalsIgnoreCase(pathname.getName())); //$NON-NLS-1$
155: }
156: });
157: if ((fList != null) && (fList.length > 0)) {
158: for (int i = 0; i < fList.length; ++i) {
159: System.out.print(fList[i]);
160: File sysFile = fList[i];
161: File fSysList[] = sysFile
162: .listFiles(new FileFilter() {
163: public boolean accept(File pathname) {
164: return (!pathname.isDirectory() && "pentaho.xml".equalsIgnoreCase(pathname.getName())); //$NON-NLS-1$
165: }
166: });
167:
168: if (fSysList.length > 0) {
169: IPath documentPath = path
170: .removeFirstSegments(path
171: .matchingFirstSegments(new Path(
172: theFile
173: .getAbsolutePath())));
174: solutionName = documentPath.segment(0);
175: break;
176: }
177: }
178: }
179: //lastDirName = theFile.getName();
180: theFile = theFile.getParentFile();
181: }
182:
183: return solutionName;
184: }
185:
186: public static String getRelativeSolutionPath(String filePath) {
187: String solutionPath = null;
188: File theFile = new File(filePath);
189: if (!theFile.isDirectory()) {
190: theFile = theFile.getParentFile();
191: }
192: Path path = new Path(theFile.getAbsolutePath());
193:
194: //String lastDirName = theFile.getName();
195: while (theFile != null) {
196: File fList[] = theFile.listFiles(new FileFilter() {
197: public boolean accept(File pathname) {
198: return (pathname.isDirectory() && "system".equalsIgnoreCase(pathname.getName())); //$NON-NLS-1$
199: }
200: });
201: if (fList.length > 0) {
202: for (int i = 0; i < fList.length; ++i) {
203: System.out.print(fList[i]);
204: File sysFile = fList[i];
205: File fSysList[] = sysFile
206: .listFiles(new FileFilter() {
207: public boolean accept(File pathname) {
208: return (!pathname.isDirectory() && "pentaho.xml".equalsIgnoreCase(pathname.getName())); //$NON-NLS-1$
209: }
210: });
211:
212: if (fSysList.length > 0) {
213: IPath documentPath = path
214: .removeFirstSegments(path
215: .matchingFirstSegments(new Path(
216: theFile
217: .getAbsolutePath())));
218: solutionPath = documentPath.lastSegment();
219: break;
220: }
221: }
222: }
223: //lastDirName = theFile.getName();
224: theFile = theFile.getParentFile();
225: }
226:
227: return solutionPath;
228: }
229:
230: public void setBaseUrl(String baseUrl) {
231: this .baseUrl = baseUrl;
232: }
233:
234: public String getSolutionName() {
235: return (solutionName);
236: }
237:
238: public String getActionPath() {
239: return (actionPath);
240: }
241:
242: public String getAction() {
243: return (actionName);
244: }
245:
246: public String getActionUrl() {
247: String theUrl = baseUrl;
248: theUrl += "/ViewAction?"; //$NON-NLS-1$
249: theUrl += "&solution=" + getSolutionName(); //$NON-NLS-1$
250: theUrl += "&path=" + getActionPath(); //$NON-NLS-1$
251: theUrl += "&action=" + getAction(); //$NON-NLS-1$
252:
253: return (theUrl);
254: }
255:
256: }
|