001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright � 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.session;
051:
052: import java.util.Collection;
053: import java.util.List;
054:
055: import org.apache.commons.collections.Closure;
056:
057: import com.projity.exchange.FileImporter;
058: import com.projity.grouping.core.model.DefaultNodeModel;
059: import com.projity.job.Job;
060: import com.projity.job.JobRunnable;
061: import com.projity.pm.resource.ResourcePool;
062: import com.projity.pm.resource.ResourcePoolFactory;
063: import com.projity.pm.task.Project;
064: import com.projity.pm.task.ProjectFactory;
065: import com.projity.strings.Messages;
066: import com.projity.undo.DataFactoryUndoController;
067: import com.projity.util.Alert;
068: import com.projity.util.ClassUtils;
069: import com.projity.util.Environment;
070:
071: public class LocalSession extends AbstractSession {
072: public static final String LOCAL_PROJECT_IMPORTER = "com.projity.exchange.LocalFileImporter";
073: public static final String SERVER_LOCAL_PROJECT_IMPORTER = "com.projity.exchange.ServerLocalFileImporter";
074: public static final String MICROSOFT_PROJECT_IMPORTER = "com.projity.exchange.MicrosoftImporter";
075:
076: protected long localSeed;
077:
078: public synchronized long getId() {
079: return localSeed++;
080: }
081:
082: public Job getCloseProjectsJob(final Collection projects) {
083: Job job = new Job(jobQueue, "closeProjects", "Closing...",
084: false);
085: job.addRunnable(new JobRunnable("LocalAccess: closeProjects",
086: 0.1f) {
087: public Object run() throws Exception {
088: setProgress(1.0f);
089: return null;
090: }
091: });
092: job.addExceptionRunnable(new JobRunnable("Local: exception") {
093: public Object run() throws Exception {
094: Alert.error(Messages.getString("Message.serverError"));
095: return null;
096: }
097: });
098: return job;
099: }
100:
101: public Job getLoadProjectJob(final LoadOptions opt) {
102: final Job job = new Job(jobQueue, "loadProject", "Loading...",
103: true);
104: job.setCancelMonitorClosure(new Closure() {
105: public void execute(Object o) {
106: System.out.println("Monitor Canceled");
107: jobQueue.endCriticalSection(job);
108: }
109: });
110: try {
111: final FileImporter importer = (FileImporter) ClassUtils
112: .forName(opt.getImporter()).newInstance();
113: importer.setFileName(opt.getFileName());
114: importer.setResourceMapping(opt.getResourceMapping());
115: importer.setProjectFactory(ProjectFactory.getInstance());//used?
116: importer.setJobQueue(jobQueue);
117:
118: job.addSwingRunnable(new JobRunnable(
119: "LocalAccess: loadProject.begin", 1.0f) {
120: public Object run() throws Exception {
121: ResourcePool resourcePool = null;
122: if (MICROSOFT_PROJECT_IMPORTER.equals(opt
123: .getImporter())) {
124: DataFactoryUndoController undoController = new DataFactoryUndoController();
125: resourcePool = ResourcePoolFactory
126: .getInstance().createResourcePool("",
127: undoController);
128: resourcePool.setLocal(importer
129: .getResourceMapping() == null);
130: Project project = Project.createProject(
131: resourcePool, undoController);
132:
133: ((DefaultNodeModel) project.getTaskOutline())
134: .setDataFactory(project);
135: importer.setProject(project);
136: }
137: setProgress(1.0f);
138: return null;
139: }
140: });
141: job.addJob(importer.getImportFileJob());
142: job.addRunnable(new JobRunnable(
143: "LocalAccess: loadProject.end", 1.0f) {
144: public Object run() throws Exception {
145: Project project = importer.getProject();
146: project.setFileName(opt.getFileName()); //overrides project name
147: if (MICROSOFT_PROJECT_IMPORTER.equals(opt
148: .getImporter()))
149: project.getResourcePool().setName(
150: project.getName());
151: if (Environment.getStandAlone()) { //force local in this case
152: project.setMaster(true); //local project is always master
153: project.setLocal(true);
154: }
155: setProgress(1.0f);
156: return project;
157:
158: }
159: });
160: } catch (InstantiationException e) {
161: // TODO Auto-generated catch block
162: e.printStackTrace();
163: } catch (IllegalAccessException e) {
164: // TODO Auto-generated catch block
165: e.printStackTrace();
166: } catch (ClassNotFoundException e) {
167: // TODO Auto-generated catch block
168: e.printStackTrace();
169: }
170: return job;
171: }
172:
173: public Job getSaveProjectJob(final List<Project> projs,
174: final SaveOptions opt) {
175: final String title = "Saving";
176: final Job job = new Job(jobQueue, "saveProject", title + "...",
177: true);
178: job.setCancelMonitorClosure(new Closure() {
179: public void execute(Object o) {
180: System.out.println("Monitor Canceled");
181: jobQueue.endCriticalSection(job);
182: }
183: });
184: FileImporter importer = null;
185: try {
186: importer = (FileImporter) ClassUtils.forName(
187: opt.getImporter()).newInstance();
188: } catch (InstantiationException e) {
189: // TODO Auto-generated catch block
190: e.printStackTrace();
191: } catch (IllegalAccessException e) {
192: // TODO Auto-generated catch block
193: e.printStackTrace();
194: } catch (ClassNotFoundException e) {
195: // TODO Auto-generated catch block
196: e.printStackTrace();
197: }
198: importer.setJobQueue(jobQueue);
199: importer.setProjectFactory(ProjectFactory.getInstance());//used?
200: int count = projs.size();
201: int i = 0;
202: for (final Project project : projs) {
203: //if projs.size()>1 opt.getFileName() must be null
204: String fileN = (opt.getFileName() == null) ? project
205: .getGuessedFileName() : opt.getFileName();//+(count>1?("("+i+")"):""));
206: if (!FileHelper.isFileNameAllowed(fileN, true)) {
207: fileN = SessionFactory.getInstance().getLocalSession()
208: .chooseFileName(
209: true,
210: FileHelper.changeFileExtension(fileN, /*project.getFileType()*/
211: FileHelper.PROJITY_FILE_TYPE));
212: }
213: final String fileName = fileN;
214: if (fileName == null)
215: continue;
216: importer.setFileName(fileName);
217: importer.setProject(project);
218: if (opt.getPreSaving() != null)
219: opt.getPreSaving().execute(project);
220:
221: job.addJob(importer.getExportFileJob());
222: job.addRunnable(new JobRunnable("Local: saveProject end") {
223: public Object run() throws Exception {
224: project.setFileName(fileName);
225: project.setGroupDirty(false);
226: if (opt.getPostSaving() != null)
227: opt.getPostSaving().execute(project);
228: return null;
229: }
230: });
231:
232: //setProgress(((float)++i)/((float)count));
233: }
234: job.addExceptionRunnable(new JobRunnable("Local: exception") {
235: public Object run() throws Exception {
236: Alert.error(Messages.getString("Message.serverError"));
237: return null;
238: }
239: });
240: return job;
241: }
242:
243: private FileHelper fileHelper = null;
244:
245: private FileHelper getFileHelper() {
246: if (fileHelper == null)
247: fileHelper = new FileHelper();
248: return fileHelper;
249: }
250:
251: public String chooseFileName(final boolean save,
252: String selectedFileName) {
253: return getFileHelper().chooseFileName(save, selectedFileName,
254: getJobQueue().getComponent());
255: }
256:
257: public static String getImporter(int fileType) {
258: switch (fileType) {
259: case FileHelper.PROJITY_FILE_TYPE:
260: return LOCAL_PROJECT_IMPORTER;
261: case FileHelper.MSP_FILE_TYPE:
262: return MICROSOFT_PROJECT_IMPORTER;
263: default:
264: return null;
265: }
266: }
267:
268: }
|