View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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   * @version $Id$
46   */
47  public class DeltaBuilder implements Delta {
48  
49      /**
50       * Convenience factory method for an Update Delta with a single task.
51       */
52      public static DeltaBuilder update(Version toVersion, String description, Task task) {
53          return update(toVersion, description).addTask(task);
54      }
55  
56      public static DeltaBuilder update(String toVersionString, String description) {
57          return update(Version.parseVersion(toVersionString), description);
58      }
59  
60      public static DeltaBuilder update(Version toVersion, String description) {
61          return new DeltaBuilder(toVersion, description, DeltaType.update);
62      }
63  
64      public static DeltaBuilder install(Version version, String description) {
65          return new DeltaBuilder(version, description, DeltaType.install);
66      }
67  
68      public static DeltaBuilder startup(ModuleDefinition moduleDef, List<Task> tasks) {
69          final String description = "Tasks executed before starting up module " + moduleDef.getDescription();
70          return startup(moduleDef.getVersion(), description).addTasks(tasks);
71      }
72  
73      public static DeltaBuilder checkPrecondition(String minimalVersion, String newVersion) {
74          return update(minimalVersion, "minimal version required for updating to " + newVersion).addCondition(
75                  new FalseCondition("checkPrerequisite", "Updating to " + newVersion + " is only supported from "
76                          + minimalVersion + " or higher."));
77      }
78  
79      /**
80       * TODO : it seems irrelevant to have a Version in startup tasks. These should probably be moved to ModuleLifecycle.
81       */
82      public static DeltaBuilder startup(Version version, String description) {
83          return new DeltaBuilder(version, description, DeltaType.startup);
84      }
85  
86      private final Version version;
87      private final String description;
88      private final DeltaType type;
89      private final List<Task> tasks;
90      private final List<Condition> conditions;
91  
92      private DeltaBuilder(Version version, String description, DeltaType type) {
93          this.version = version;
94          this.description = description;
95          this.type = type;
96          this.tasks = new ArrayList<Task>();
97          this.conditions = new ArrayList<Condition>();
98      }
99  
100     public DeltaBuilder addTask(Task t) {
101         tasks.add(t);
102         return this;
103     }
104 
105     public DeltaBuilder addTasks(List<Task> tasks) {
106         this.tasks.addAll(tasks);
107         return this;
108     }
109 
110     public DeltaBuilder addCondition(Condition c) {
111         conditions.add(c);
112         return this;
113     }
114 
115     public DeltaBuilder addConditions(List<Condition> conditions) {
116         this.conditions.addAll(conditions);
117         return this;
118     }
119 
120     @Override
121     public Version getVersion() {
122         return version;
123     }
124 
125     @Override
126     public String getDescription() {
127         return description;
128     }
129 
130     @Override
131     public List<Condition> getConditions() {
132         return conditions;
133     }
134 
135     @Override
136     public List<Task> getTasks() {
137         return tasks;
138     }
139 
140     @Override
141     public DeltaType getType() {
142         return type;
143     }
144 
145 }