View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.delta;
35  
36  import info.magnolia.module.model.ModuleDefinition;
37  import info.magnolia.module.model.Version;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * Helper class to build Delta instances.
44   * 
45   * @author gjoseph
46   * @version $Revision: $ ($Author: $)
47   */
48  public class DeltaBuilder implements Delta {
49  
50      /**
51       * Convenience factory method for an Update Delta with a single task.
52       */
53      public static DeltaBuilder update(Version v, String description, Task task) {
54          return update(v, description).addTask(task);
55      }
56  
57      public static DeltaBuilder update(String versionStr, String description) {
58          return update(Version.parseVersion(versionStr), description);
59      }
60  
61      public static DeltaBuilder update(Version version, String description) {
62          return new DeltaBuilder(version, description, DeltaType.update);
63      }
64  
65      public static DeltaBuilder install(Version version, String description) {
66          return new DeltaBuilder(version, description, DeltaType.install);
67      }
68  
69      public static DeltaBuilder startup(ModuleDefinition moduleDef, List<Task> tasks) {
70          final String description = "Tasks executed before starting up module " + moduleDef.getDescription();
71          return startup(moduleDef.getVersion(), description).addTasks(tasks);
72      }
73  
74      /**
75       * TODO : it seems irrelevant to have a Version in startup tasks. These should probably be moved to ModuleLifecycle.
76       */
77      public static DeltaBuilder startup(Version version, String description) {
78          return new DeltaBuilder(version, description, DeltaType.startup);
79      }
80  
81      private final Version version;
82      private final String description;
83      private final DeltaType type;
84      private final List<Task> tasks;
85      private final List<Condition> conditions;
86  
87      private DeltaBuilder(Version version, String description, DeltaType type) {
88          this.version = version;
89          this.description = description;
90          this.type = type;
91          this.tasks = new ArrayList<Task>();
92          this.conditions = new ArrayList<Condition>();
93      }
94  
95      public DeltaBuilder addTask(Task t) {
96          tasks.add(t);
97          return this;
98      }
99  
100     public DeltaBuilder addTasks(List<Task> tasks) {
101         this.tasks.addAll(tasks);
102         return this;
103     }
104 
105     public DeltaBuilder addCondition(Condition c) {
106         conditions.add(c);
107         return this;
108     }
109 
110     public DeltaBuilder addConditions(List<Condition> conditions) {
111         this.conditions.addAll(conditions);
112         return this;
113     }
114 
115     public Version getVersion() {
116         return version;
117     }
118 
119     public String getDescription() {
120         return description;
121     }
122 
123     public List<Condition> getConditions() {
124         return conditions;
125     }
126 
127     public List<Task> getTasks() {
128         return tasks;
129     }
130 
131     public DeltaType getType() {
132         return type;
133     }
134 
135 }