001: /**
002: * ProjectManager.java
003: * Created on 11.02.2003, 17:50:27 Alex
004: * Package: net.sf.memoranda
005: *
006: * @author Alex V. Alishevskikh, alex@openmechanics.net
007: * Copyright (c) 2003 Memoranda Team. http://memoranda.sf.net
008: */package net.sf.memoranda;
009:
010: import java.util.Vector;
011:
012: import net.sf.memoranda.date.CalendarDate;
013: import net.sf.memoranda.util.CurrentStorage;
014: import net.sf.memoranda.util.Local;
015: import net.sf.memoranda.util.Util;
016: import nu.xom.Attribute;
017: import nu.xom.Document;
018: import nu.xom.Element;
019: import nu.xom.Elements;
020:
021: /**
022: *
023: */
024: /*$Id: ProjectManager.java,v 1.9 2005/12/01 08:12:26 alexeya Exp $*/
025: public class ProjectManager {
026: // public static final String NS_JNPROJECT = "http://www.openmechanics.org/2003/jnotes-projects-file";
027:
028: public static Document _doc = null;
029: static Element _root = null;
030:
031: static {
032: init();
033: }
034:
035: public static void init() {
036: CurrentStorage.get().openProjectManager();
037: if (_doc == null) {
038: _root = new Element("projects-list");
039: // _root.addNamespaceDeclaration("jnotes", NS_JNPROJECT);
040: // _root.appendChild(new Comment("This is JNotes 2 data file. Do not modify."));
041: _doc = new Document(_root);
042: createProject("__default", Local
043: .getString("Default project"),
044: CalendarDate.today(), null);
045: } else
046: _root = _doc.getRootElement();
047: }
048:
049: public static Project getProject(String id) {
050: Elements prjs = _root.getChildElements("project");
051: for (int i = 0; i < prjs.size(); i++) {
052: String pid = ((Element) prjs.get(i)).getAttribute("id")
053: .getValue();
054: if (pid.equals(id)) {
055: return new ProjectImpl((Element) prjs.get(i));
056: }
057: }
058: return null;
059: }
060:
061: public static Vector getAllProjects() {
062: Elements prjs = _root.getChildElements("project");
063: Vector v = new Vector();
064: for (int i = 0; i < prjs.size(); i++)
065: v.add(new ProjectImpl((Element) prjs.get(i)));
066: return v;
067: }
068:
069: public static int getAllProjectsNumber() {
070: int i;
071: try {
072: i = ((Elements) _root.getChildElements("project")).size();
073: } catch (NullPointerException e) {
074: i = 1;
075: }
076: return i;
077: }
078:
079: public static Vector getActiveProjects() {
080: Elements prjs = _root.getChildElements("project");
081: Vector v = new Vector();
082: for (int i = 0; i < prjs.size(); i++) {
083: Project prj = new ProjectImpl((Element) prjs.get(i));
084: if (prj.getStatus() == Project.ACTIVE)
085: v.add(prj);
086: }
087: return v;
088: }
089:
090: public static int getActiveProjectsNumber() {
091: Elements prjs = _root.getChildElements("project");
092: int count = 0;
093: for (int i = 0; i < prjs.size(); i++) {
094: Project prj = new ProjectImpl((Element) prjs.get(i));
095: if (prj.getStatus() == Project.ACTIVE)
096: count++;
097: }
098: return count;
099: }
100:
101: public static Project createProject(String id, String title,
102: CalendarDate startDate, CalendarDate endDate) {
103: Element el = new Element("project");
104: el.addAttribute(new Attribute("id", id));
105: _root.appendChild(el);
106: Project prj = new ProjectImpl(el);
107: prj.setTitle(title);
108: prj.setStartDate(startDate);
109: prj.setEndDate(endDate);
110: CurrentStorage.get().createProjectStorage(prj);
111: return prj;
112: }
113:
114: public static Project createProject(String title,
115: CalendarDate startDate, CalendarDate endDate) {
116: return createProject(Util.generateId(), title, startDate,
117: endDate);
118: }
119:
120: public static void removeProject(String id) {
121: Project prj = getProject(id);
122: if (prj == null)
123: return;
124: History.removeProjectHistory(prj);
125: CurrentStorage.get().removeProjectStorage(prj);
126: Elements prjs = _root.getChildElements("project");
127: for (int i = 0; i < prjs.size(); i++) {
128: String pid = ((Element) prjs.get(i)).getAttribute("id")
129: .getValue();
130: if (pid.equals(id)) {
131: _root.removeChild(prjs.get(i));
132: return;
133: }
134: }
135: }
136:
137: }
|