2bccf31c0a3a311601255290c3f784aa6e95a545
[jSite.git] / src / main / java / de / todesbaum / jsite / application / ProjectInsertListeners.java
1 /*
2  * jSite - ProjectInsertListeners.java - Copyright © 2013–2014 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package de.todesbaum.jsite.application;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24 /**
25  * Manages {@link InsertListener}s for the {@link ProjectInserter}.
26  *
27  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
28  */
29 class ProjectInsertListeners {
30
31         /** The list of insert listeners. */
32         private final List<InsertListener> insertListeners = new ArrayList<InsertListener>();
33
34         /**
35          * Adds a listener to the list of registered listeners.
36          *
37          * @param insertListener
38          *              The listener to add
39          */
40         void addInsertListener(InsertListener insertListener) {
41                 insertListeners.add(insertListener);
42         }
43
44         /**
45          * Removes a listener from the list of registered listeners.
46          *
47          * @param insertListener
48          *              The listener to remove
49          */
50         void removeInsertListener(InsertListener insertListener) {
51                 insertListeners.remove(insertListener);
52         }
53
54         /**
55          * Notifies all listeners that the project insert has started.
56          *
57          * @param project
58          * @see InsertListener#projectInsertStarted(Project)
59          */
60         void fireProjectInsertStarted(Project project) {
61                 for (InsertListener insertListener : insertListeners) {
62                         insertListener.projectInsertStarted(project);
63                 }
64         }
65
66         /**
67          * Notifies all listeners that the insert has generated a URI.
68          *
69          * @param project
70          * @param uri
71          *              The generated URI
72          * @see InsertListener#projectURIGenerated(Project, String)
73          */
74         void fireProjectURIGenerated(Project project, String uri) {
75                 for (InsertListener insertListener : insertListeners) {
76                         insertListener.projectURIGenerated(project, uri);
77                 }
78         }
79
80         /**
81          * Notifies all listeners that the insert has made some progress.
82          *
83          * @param project
84          * @see InsertListener#projectUploadFinished(Project)
85          */
86         void fireProjectUploadFinished(Project project) {
87                 for (InsertListener insertListener : insertListeners) {
88                         insertListener.projectUploadFinished(project);
89                 }
90         }
91
92         /**
93          * Notifies all listeners that the insert has made some progress.
94          *
95          * @param project
96          * @param succeeded
97          *              The number of succeeded blocks
98          * @param failed
99          *              The number of failed blocks
100          * @param fatal
101          *              The number of fatally failed blocks
102          * @param total
103          *              The total number of blocks
104          * @param finalized
105          *              <code>true</code> if the total number of blocks has already been
106          *              finalized, <code>false</code> otherwise
107          * @see InsertListener#projectInsertProgress(Project, int, int, int, int,
108          *      boolean)
109          */
110         void fireProjectInsertProgress(Project project, int succeeded, int failed, int fatal, int total, boolean finalized) {
111                 for (InsertListener insertListener : insertListeners) {
112                         insertListener.projectInsertProgress(project, succeeded, failed, fatal, total, finalized);
113                 }
114         }
115
116         /**
117          * Notifies all listeners the project insert has finished.
118          *
119          * @param project
120          * @param success
121          *              <code>true</code> if the project was inserted successfully,
122          *              <code>false</code> if it failed
123          * @param cause
124          *              The cause of the failure, if any
125          * @see InsertListener#projectInsertFinished(Project, boolean, Throwable)
126          */
127         void fireProjectInsertFinished(Project project, boolean success, Throwable cause) {
128                 for (InsertListener insertListener : insertListeners) {
129                         insertListener.projectInsertFinished(project, success, cause);
130                 }
131         }
132
133 }