add insertProject()
[jSite2.git] / src / net / pterodactylus / jsite / core / InsertManager.java
1 /*
2  * jSite2 - InsertManager.java - Copyright © 2008 David Roden
3  * 
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  * 
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  * 
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package net.pterodactylus.jsite.core;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import net.pterodactylus.util.logging.Logging;
29
30 /**
31  * Manages all currently running and past inserts.
32  * 
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  */
35 public class InsertManager {
36
37         /** Logger. */
38         private static final Logger logger = Logging.getLogger(InsertManager.class.getName());
39
40         /** The insert listeners. */
41         private final List<InsertListener> insertListeners = new ArrayList<InsertListener>();
42
43         /** Mapping from insert IDs to inserts. */
44         private final Map<String, Insert> inserts = new HashMap<String, Insert>();
45
46         //
47         // EVENT MANAGEMENT
48         //
49
50         /**
51          * Adds an insert listener to the list of insert listeners.
52          * 
53          * @param insertListener
54          *            The insert listener to add
55          */
56         public void addInsertListener(InsertListener insertListener) {
57                 logger.log(Level.FINEST, "addInsertListener(insertListener=" + insertListener + ")");
58                 synchronized (insertListeners) {
59                         insertListeners.add(insertListener);
60                 }
61         }
62
63         /**
64          * Removes an insert listener from the list of insert listeners.
65          * 
66          * @param insertListener
67          *            The insert listener to remove
68          */
69         public void removeInsertListener(InsertListener insertListener) {
70                 logger.log(Level.FINEST, "removeInsertListener(insertListener=" + insertListener + ")");
71                 synchronized (insertListeners) {
72                         insertListeners.remove(insertListener);
73                 }
74         }
75
76         /**
77          * Notifies all listeners that an insert was added.
78          * 
79          * @param insert
80          *            The insert that was added
81          */
82         private void fireInsertAdded(Insert insert) {
83                 synchronized (insertListeners) {
84                         for (InsertListener insertListener : insertListeners) {
85                                 insertListener.insertAdded(insert);
86                         }
87                 }
88         }
89
90         /**
91          * Notifies all listeners that an insert was removed.
92          * 
93          * @param insert
94          *            The insert that was removes
95          */
96         private void fireInsertRemoved(Insert insert) {
97                 synchronized (insertListeners) {
98                         for (InsertListener insertListener : insertListeners) {
99                                 insertListener.insertRemoved(insert);
100                         }
101                 }
102         }
103
104         /**
105          * Notifies all listeners that an insert was started.
106          * 
107          * @param insert
108          *            The insert that was started
109          */
110         private void fireInsertStarted(Insert insert) {
111                 synchronized (insertListeners) {
112                         for (InsertListener insertListener : insertListeners) {
113                                 insertListener.insertStarted(insert);
114                         }
115                 }
116         }
117
118         /**
119          * Notifies all listeners that an insert made some progress
120          * 
121          * @param insert
122          *            The insert that made some progress
123          */
124         private void fireInsertProgressed(Insert insert) {
125                 synchronized (insertListeners) {
126                         for (InsertListener insertListener : insertListeners) {
127                                 insertListener.insertProgressed(insert);
128                         }
129                 }
130         }
131
132         /**
133          * Notifies all listeners that an insert generated a URI.
134          * 
135          * @param insert
136          *            The insert that generated a URI
137          */
138         private void fireInsertGeneratedURI(Insert insert, String uri) {
139                 synchronized (insertListeners) {
140                         for (InsertListener insertListener : insertListeners) {
141                                 insertListener.insertGeneratedURI(insert, uri);
142                         }
143                 }
144         }
145
146         /**
147          * Notifies all listeners that an insert has finished.
148          * 
149          * @param insert
150          *            The insert that has finished
151          */
152         private void fireInsertFinished(Insert insert) {
153                 synchronized (insertListeners) {
154                         for (InsertListener insertListener : insertListeners) {
155                                 insertListener.insertFinished(insert);
156                         }
157                 }
158         }
159
160         //
161         // ACTIONS
162         //
163
164         /**
165          * Starts to insert the given project. The insert will be made to the node
166          * stored in the project, and if no node is specified in the project, the
167          * given node will be used.
168          * 
169          * @param project
170          *            The project to insert
171          * @param node
172          *            The node to insert the project to if the project does not
173          *            specify a node
174          */
175         public void insertProject(Project project, Node node) {
176                 logger.log(Level.FINEST, "insertProject(project=" + project + ",node=" + node + ")");
177                 Node insertNode = project.getNode();
178                 if (insertNode == null) {
179                         insertNode = node;
180                         if (insertNode == null) {
181                                 throw new NullPointerException("node must not be null");
182                         }
183                 }
184                 String insertId = "insert-" + project.getId();
185                 Insert newInsert = new Insert(project, insertNode, insertId);
186                 inserts.put(insertId, newInsert);
187                 fireInsertAdded(newInsert);
188                 /* TODO - start insert */
189         }
190
191         /**
192          * Starts the insert manager.
193          */
194         public void start() {
195                 logger.log(Level.FINEST, "start()");
196                 loadConfiguration();
197         }
198
199         //
200         // PRIVATE METHODS
201         //
202
203         /**
204          * Loads the configuration.
205          */
206         private void loadConfiguration() {
207                 logger.log(Level.FINEST, "loadConfiguration()");
208         }
209
210 }