add project manager
[jSite2.git] / src / net / pterodactylus / jsite / core / ProjectManager.java
1 /*
2  * jSite2 - ProjectManager.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.jsite.core;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Properties;
33
34 import net.pterodactylus.util.io.Closer;
35
36 /**
37  * Manages projects, taking care of persistence, lifetime statistics, and other
38  * things.
39  * 
40  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
41  * @version $Id$
42  */
43 public class ProjectManager {
44
45         /** The directory the projects are stored in. */
46         private final String directory;
47
48         /** The list of project names. */
49         private final List<String> projectNames = new ArrayList<String>();
50
51         /** Mapping from project name to project. */
52         private final Map<String, Project> projects = new HashMap<String, Project>();
53
54         /** Mapping from project to lifetime statistics. */
55         @SuppressWarnings("unused")
56         private final Map<Project, ProjectLifetime> projectLifetimes = new HashMap<Project, ProjectLifetime>();
57
58         /**
59          * Creates a new project manager that saves and restores its state to/from
60          * the given directory.
61          * 
62          * @param directory
63          *            The directory to save and restore states to/from
64          */
65         public ProjectManager(String directory) {
66                 this.directory = directory;
67         }
68
69         //
70         // ACCESSORS
71         //
72
73         /**
74          * Returns the directory the projects are loaded from and saved to.
75          * 
76          * @return The directory for storing the projects
77          */
78         public String getDirectory() {
79                 return directory;
80         }
81
82         /**
83          * Returns a list of all projects, sorted by their name.
84          * 
85          * @return A list of all projects
86          */
87         public List<Project> getProjects() {
88                 List<Project> projects = new ArrayList<Project>();
89                 for (String projectName: projectNames) {
90                         projects.add(this.projects.get(projectName));
91                 }
92                 return projects;
93         }
94
95         //
96         // ACTIONS
97         //
98
99         /**
100          * Loads projects and statistics.
101          * 
102          * @throws IOException
103          *             if an I/O error occurs
104          */
105         public void load() throws IOException {
106                 File directoryFile = new File(directory);
107                 File projectFile = new File(directoryFile, "projects.properties");
108                 if (!projectFile.exists() || !projectFile.isFile() || !projectFile.canRead()) {
109                         return;
110                 }
111                 Properties projectProperties = new Properties();
112                 InputStream projectInputStream = null;
113                 try {
114                         projectInputStream = new FileInputStream(projectFile);
115                         projectProperties.load(projectInputStream);
116                 } finally {
117                         Closer.close(projectInputStream);
118                 }
119                 int projectIndex = 0;
120                 while (projectProperties.containsKey("projects." + projectIndex + ".name")) {
121                         String projectPrefix = "projects." + projectIndex;
122                         String projectName = projectProperties.getProperty(projectPrefix + ".name");
123                         Project project = new Project();
124                         project.setName(projectName);
125                         projectNames.add(projectName);
126                         projects.put(projectName, project);
127                         projectIndex++;
128                 }
129         }
130
131         /**
132          * Saves projects and statistics.
133          * 
134          * @throws IOException
135          *             if an I/O error occurs
136          */
137         public void save() throws IOException {
138                 File directoryFile = new File(directory);
139                 if (!directoryFile.exists()) {
140                         if (!directoryFile.mkdirs()) {
141                                 throw new IOException("could not create directory: " + directory);
142                         }
143                 }
144                 Properties projectProperties = new Properties();
145                 int projectIndex = 0;
146                 for (String projectName: projectNames) {
147                         String projectPrefix = "projects." + projectIndex;
148                         Project project = projects.get(projectName);
149                         projectProperties.setProperty("projects." + projectPrefix + ".name", project.getName());
150                         projectIndex++;
151                 }
152                 File projectFile = new File(directoryFile, "projects.properties");
153                 OutputStream projectOutputStream = null;
154                 try {
155                         projectOutputStream = new FileOutputStream(projectFile);
156                         projectProperties.store(projectOutputStream, "jSite projects");
157                 } finally {
158                         Closer.close(projectOutputStream);
159                 }
160         }
161
162 }