c91b513b1779c7919ad0309ff7a64289543a52a8
[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         @SuppressWarnings("unused")
97         private void fireInsertRemoved(Insert insert) {
98                 synchronized (insertListeners) {
99                         for (InsertListener insertListener : insertListeners) {
100                                 insertListener.insertRemoved(insert);
101                         }
102                 }
103         }
104
105         /**
106          * Notifies all listeners that an insert was started.
107          * 
108          * @param insert
109          *            The insert that was started
110          */
111         @SuppressWarnings("unused")
112         private void fireInsertStarted(Insert insert) {
113                 synchronized (insertListeners) {
114                         for (InsertListener insertListener : insertListeners) {
115                                 insertListener.insertStarted(insert);
116                         }
117                 }
118         }
119
120         /**
121          * Notifies all listeners that an insert made some progress
122          * 
123          * @param insert
124          *            The insert that made some progress
125          */
126         @SuppressWarnings("unused")
127         private void fireInsertProgressed(Insert insert) {
128                 synchronized (insertListeners) {
129                         for (InsertListener insertListener : insertListeners) {
130                                 insertListener.insertProgressed(insert);
131                         }
132                 }
133         }
134
135         /**
136          * Notifies all listeners that an insert generated a URI.
137          * 
138          * @param insert
139          *            The insert that generated a URI
140          * @param uri
141          *            The generated URI
142          */
143         @SuppressWarnings("unused")
144         private void fireInsertGeneratedURI(Insert insert, String uri) {
145                 synchronized (insertListeners) {
146                         for (InsertListener insertListener : insertListeners) {
147                                 insertListener.insertGeneratedURI(insert, uri);
148                         }
149                 }
150         }
151
152         /**
153          * Notifies all listeners that an insert has finished.
154          * 
155          * @param insert
156          *            The insert that has finished
157          */
158         @SuppressWarnings("unused")
159         private void fireInsertFinished(Insert insert) {
160                 synchronized (insertListeners) {
161                         for (InsertListener insertListener : insertListeners) {
162                                 insertListener.insertFinished(insert);
163                         }
164                 }
165         }
166
167         //
168         // ACTIONS
169         //
170
171         /**
172          * Starts to insert the given project. The insert will be made to the node
173          * stored in the project, and if no node is specified in the project, the
174          * given node will be used.
175          * 
176          * @param project
177          *            The project to insert
178          * @param node
179          *            The node to insert the project to if the project does not
180          *            specify a node
181          */
182         public void insertProject(Project project, Node node) {
183                 logger.log(Level.FINEST, "insertProject(project=" + project + ",node=" + node + ")");
184                 Node insertNode = project.getNode();
185                 if (insertNode == null) {
186                         insertNode = node;
187                         if (insertNode == null) {
188                                 throw new NullPointerException("node must not be null");
189                         }
190                 }
191                 String insertId = "insert-" + project.getId();
192                 Insert newInsert = new Insert(project, insertNode, insertId);
193                 inserts.put(insertId, newInsert);
194                 saveConfiguration();
195                 fireInsertAdded(newInsert);
196                 /* TODO - start insert */
197         }
198
199         /**
200          * Starts the insert manager.
201          */
202         public void start() {
203                 logger.log(Level.FINEST, "start()");
204                 loadConfiguration();
205         }
206
207         //
208         // PRIVATE METHODS
209         //
210
211         /**
212          * Loads the configuration.
213          */
214         private void loadConfiguration() {
215                 logger.log(Level.FINEST, "loadConfiguration()");
216         }
217
218         /**
219          * Saves the configuration.
220          */
221         private void saveConfiguration() {
222                 logger.log(Level.FINEST, "saveConfiguration()");
223         }
224
225 }