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.pm.task;
051:
052: import java.util.ArrayList;
053: import java.util.Collection;
054: import java.util.Comparator;
055: import java.util.Iterator;
056:
057: import org.apache.commons.collections.Closure;
058:
059: import com.projity.document.Document;
060: import com.projity.document.ObjectEventManager;
061: import com.projity.document.ObjectEvent.Listener;
062: import com.projity.grouping.core.Node;
063: import com.projity.grouping.core.NodeFactory;
064: import com.projity.grouping.core.model.NodeModel;
065: import com.projity.grouping.core.model.NodeModelDataFactory;
066: import com.projity.grouping.core.model.NodeModelFactory;
067: import com.projity.grouping.core.summaries.DeepChildSearcher;
068: import com.projity.job.Job;
069: import com.projity.job.JobRunnable;
070: import com.projity.pm.calendar.WorkCalendar;
071: import com.projity.pm.calendar.WorkingCalendar;
072: import com.projity.session.SessionFactory;
073: import com.projity.strings.Messages;
074: import com.projity.undo.DataFactoryUndoController;
075: import com.projity.util.Alert;
076:
077: /**
078: *
079: */
080: public class Portfolio implements Document, NodeModelDataFactory {
081: NodeModel nodeModel;
082: ObjectEventManager objectEventManager;
083: boolean creating = false;
084: private transient boolean dirty;
085: ProjectFactory projectFactory = null;
086:
087: /**
088: *
089: */
090: public Portfolio(ProjectFactory projectFactory) {
091: super ();
092: this .projectFactory = projectFactory;
093: objectEventManager = new ObjectEventManager();
094: nodeModel = NodeModelFactory.getInstance()
095: .createNodeModel(this );
096: nodeModel.getHierarchy().setNbEndVoidNodes(0);
097: }
098:
099: public Project findByUniqueId(long uniqueId) {
100: return (Project) DeepChildSearcher.searchForUniqueId(nodeModel,
101: uniqueId);
102: }
103:
104: void addProject(final Project project, boolean createJob,
105: boolean verify) {
106: if (!verify) {
107: _addProject(project);
108: return;
109: }
110: // if (Environment.getStandAlone())
111: // createJob = false;
112: Job job = null;
113: if (creating)
114: return;
115: Node oldNode = nodeModel.search(project, comparator);
116: if (oldNode != null) {
117: if (Alert.confirm(Messages
118: .getString("Message.projectAlreadyExists")) == 1) {
119: //TODO be sure all references are removed
120: return;
121: } else {
122: //removeProject((Project)oldNode.getImpl());
123: job = getRemoveProjectJob((Project) oldNode.getImpl());
124: if (!createJob) {
125: job.addSync();
126: SessionFactory.getInstance().getSession(
127: project.isLocal()).schedule(job);
128: }
129: }
130: }
131:
132: if (!createJob) {
133: _addProject(project);
134: return;
135: }
136:
137: Job addProjectJob = new Job(SessionFactory.getInstance()
138: .getSession(project.isLocal()).getJobQueue(),
139: "addProject", "Adding project...", false);
140: addProjectJob.addSwingRunnable(new JobRunnable(
141: "Local: addProject", 1.0f) {
142: public Object run() throws Exception {
143: _addProject(project);
144: setProgress(1.0f);
145: return null;
146: }
147: });
148: // job.addExceptionRunnable(new JobRunnable("Local: exception"){
149: // public Object run() throws Exception{
150: // Alert.error(Messages.getString("Message.serverError"));
151: // return null;
152: // }
153: // });
154: if (job == null)
155: job = addProjectJob;
156: else
157: job.addJob(addProjectJob);
158: SessionFactory.getInstance().getSession(project.isLocal())
159: .schedule(job);
160:
161: }
162:
163: private void _addProject(Project project) {
164: nodeModel.add(NodeFactory.getInstance().createNode(project),
165: NodeModel.SILENT);
166:
167: handleExternalTasks(project, true, false); // external link handling
168:
169: objectEventManager.fireCreateEvent(this , project);
170: project.getResourcePool().addProject(project);
171: }
172:
173: void handleExternalTasks(final Project project,
174: final boolean opening, final boolean saving) {
175: // external link handling
176: forProjects(new Closure() {
177: public void execute(Object arg0) {
178: Project p = (Project) arg0;
179: if (p != project)
180: p.handleExternalTasks(project, opening, saving);
181: }
182: });
183:
184: }
185:
186: public void addSubproject(Project child, Project parent) {
187: Node childNode = nodeModel.search(child);
188: if (childNode == null) {
189: addProject(child, false, true);
190: childNode = nodeModel.search(child);
191: } else
192: objectEventManager.fireCreateEvent(this , child); // for mainframe to get rid of any open one
193:
194: Node parentNode = nodeModel.search(parent);
195: nodeModel.getHierarchy().move(childNode, parentNode,
196: NodeModel.SILENT);
197: }
198:
199: public static ImplComparator comparator = new ImplComparator();
200:
201: public static class ImplComparator implements Comparator {
202: ImplComparator() {
203: }
204:
205: public int compare(Object node, Object impl) {
206: if (((Node) node).getImpl().equals(impl))
207: return 0;
208: else
209: return 1;
210: }
211: }
212:
213: public Job getRemoveProjectJob(final Project project) {
214: return projectFactory.getRemoveProjectJob(project, true, true);
215: }
216:
217: public Job getRemoveAllProjectsJob(JobRunnable exitRunnable) {
218: boolean exitOnClose = true;
219: final Job job = new Job(SessionFactory.getInstance()
220: .getLocalSession().getJobQueue(), "removeAllProjects",
221: "Removing projects...", true);
222: ArrayList toRemove = new ArrayList();
223: for (Iterator i = nodeModel.iterator(); i.hasNext();) {
224: Node node = (Node) i.next();
225: if (!node.isRoot())
226: toRemove.add(node);
227: }
228: Project p;
229: for (Iterator i = toRemove.iterator(); i.hasNext();) {
230: p = (Project) ((Node) i.next()).getImpl();
231: if (p.isOpenedAsSubproject()) // subprojects are saved with their parents
232: continue;
233: Job rJob = getRemoveProjectJob(p);
234: if (rJob == null)
235: exitOnClose = false; //close cancelled
236: else
237: job.addJob(rJob);
238: }
239: if (exitOnClose)
240: job.addSwingRunnable(exitRunnable, false);
241: return job;
242: }
243:
244: public void addObjectListener(Listener listener) {
245: objectEventManager.addListener(listener);
246: }
247:
248: public void removeObjectListener(Listener listener) {
249: objectEventManager.removeListener(listener);
250: }
251:
252: public ObjectEventManager getObjectEventManager() {
253: return objectEventManager;
254: }
255:
256: /* (non-Javadoc)
257: * @see com.projity.grouping.core.NodeModelDataFactory#createUnvalidatedObject(com.projity.grouping.core.NodeModel, java.lang.Object, java.lang.Object)
258: */
259: public Object createUnvalidatedObject(NodeModel nodeModel,
260: Object parent) {
261: creating = true;
262: Project project = projectFactory.createProject();
263: creating = false;
264: return project;
265: }
266:
267: public void addUnvalidatedObject(Object object,
268: NodeModel nodeModel, Object parent) {
269: }
270:
271: /* (non-Javadoc)
272: * @see com.projity.grouping.core.NodeModelDataFactory#validateObject(java.lang.Object, com.projity.grouping.core.NodeModel, java.lang.Object, java.lang.Object)
273: */
274: public void validateObject(Object newlyCreated,
275: NodeModel nodeModel, Object eventSource,
276: Object hierarchyInfo, boolean isNew) {
277: //objectEventManager.fireCreateEvent(this,(Project)newlyCreated);
278: }
279:
280: // public void fireCreated(Object newlyCreated){
281: // //objectEventManager.fireCreateEvent(this,newlyCreated);
282: // }
283: /* (non-Javadoc)
284: * @see com.projity.grouping.core.NodeModelDataFactory#remove(java.lang.Object, com.projity.grouping.core.NodeModel)
285: */
286: public void remove(Object toRemove, NodeModel nodeModel,
287: boolean deep, boolean undo, boolean removeDependencies) {
288: //removeProject((Project) toRemove);
289: }
290:
291: /**
292: * @return Returns the nodeModel.
293: */
294: public NodeModel getNodeModel() {
295: return nodeModel;
296: }
297:
298: public void forProjects(Closure c) {
299: Object impl;
300: for (Iterator i = getNodeModel().iterator(); i.hasNext();) {
301: impl = ((Node) i.next()).getImpl();
302: if (!(impl instanceof Project))
303: continue;
304: c.execute(impl);
305: }
306: }
307:
308: public Collection getDirtyProjectList() {
309: final ArrayList list = new ArrayList();
310: forProjects(new Closure() {
311: public void execute(Object arg0) {
312: if (((Project) arg0).needsSaving())
313: list.add(arg0);
314: }
315: });
316: return list;
317: }
318:
319: public void fireUpdateEvent(Object source, Object object) {
320: objectEventManager.fireUpdateEvent(source, object);
321: }
322:
323: /* (non-Javadoc)
324: * @see com.projity.document.Document#fireMultipleTransaction(int, boolean)
325: */
326: public int fireMultipleTransaction(int id, boolean begin) {
327: // TODO Auto-generated method stub
328: return 0;
329: }
330:
331: /* (non-Javadoc)
332: * @see com.projity.document.Document#getDefaultCalendar()
333: */
334: public WorkCalendar getDefaultCalendar() {
335: // TODO Auto-generated method stub
336: return null;
337: }
338:
339: public final boolean isGroupDirty() {
340: return dirty;
341: }
342:
343: public final void setGroupDirty(boolean dirty) {
344: this .dirty = dirty;
345: }
346:
347: public ArrayList extractCalendars() {
348: return WorkingCalendar.extractCalendars(nodeModel
349: .getHierarchy());
350: }
351:
352: public DataFactoryUndoController getUndoController() {
353: return null;
354: }
355:
356: public void rollbackUnvalidated(NodeModel nodeModel, Object object) {
357: }
358:
359: public void initOutline(NodeModel nodeModel) {
360: }
361:
362: public NodeModelDataFactory getFactoryToUseForChildOfParent(
363: Object impl) {
364: return this ;
365: }
366:
367: public void setAllChildrenDirty(boolean dirty) {
368: }
369:
370: public boolean containsAssignments() {
371: return false;
372: }
373:
374: }
|