View Javadoc

1   package org.riverock.dbrevision.manager;
2   
3   import org.riverock.dbrevision.db.Database;
4   import org.riverock.dbrevision.exception.FirstVersionWithPatchdException;
5   import org.riverock.dbrevision.exception.ModulePathNotFoundException;
6   import org.riverock.dbrevision.exception.CurrentVersionNotDefinedException;
7   import org.riverock.dbrevision.utils.DbUtils;
8   
9   import java.io.File;
10  import java.io.Serializable;
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  /**
15   * User: SergeMaslyukov
16   * Date: 28.07.2007
17   * Time: 20:03:59
18   */
19  public class Module implements Serializable {
20  
21      List<Version> versions=new ArrayList<Version>();
22  
23      Database database =null;
24  
25      private String description=null;
26  
27      private String name =null;
28  
29      private File modulePath=null;
30  
31      private boolean isComplete=false;
32  
33      public Module(Database database, File dbRevisionPath, ModuleConfig moduleConfig) {
34          this.database = database;
35          this.description = moduleConfig.getDescription();
36          this.name = moduleConfig.getName();
37  
38          this.modulePath = new File(dbRevisionPath, name);
39          if (!modulePath.exists()) {
40              throw new ModulePathNotFoundException("Module path not found: " + modulePath.getAbsolutePath() );
41          }
42          for (String versionName : moduleConfig.getVersions()) {
43              Version version = new Version(database, modulePath, versionName);
44              versions.add(version);
45          }
46          if (versions.size()>0) {
47              File patchPath = versions.get(0).getPatchPath();
48              if (patchPath.exists()) {
49                  throw new FirstVersionWithPatchdException("First version contains patch directory: " + patchPath.getAbsolutePath());
50              }
51          }
52      }
53  
54      public void destroy() {
55          if (database!=null) {
56              if (database.getConnection()!=null) {
57                  DbUtils.close(database.getConnection());
58                  database.setConnection(null);
59              }
60              database=null;
61          }
62          if (versions!=null) {
63              for (Version version : versions) {
64                  version.destroy();
65              }
66              versions.clear();
67              versions=null;
68          }
69      }
70  
71      public void apply() {
72          if (versions.isEmpty()) {
73              return;
74          }
75          if (DbRevisionChecker.isModuleReleased(database, name)) { 
76              for (Version version : versions) {
77                  if (version.isComplete()) {
78                      continue;
79                  }
80                  version.apply();
81              }
82          }
83          else {
84              Version version = versions.get(versions.size()-1);
85              version.applyInitStructure();
86          }
87      }
88  
89      public String getName() {
90          return name;
91      }
92  
93      public String getDescription() {
94          return description;
95      }
96  
97      public List<Version> getVersions() {
98          return versions;
99      }
100 
101     public Version getCurrentVersion() {
102         Version v=null;
103         for (Version version : versions) {
104             if (version.isComplete()) {
105                 v = version;
106             }
107         }
108         if (v==null) {
109             throw new CurrentVersionNotDefinedException();
110         }
111         return v;
112     }
113 
114     public Version getFirstVersion() {
115         if (versions.isEmpty()) {
116             return null;
117         }
118         return versions.get(0);
119     }
120 
121     public Version getLastVersion() {
122         if (versions.isEmpty()) {
123             return null;
124         }
125         return versions.get(versions.size()-1);
126     }
127 
128     public boolean isComplete() {
129         return isComplete;
130     }
131 
132     public void setComplete(boolean complete) {
133         isComplete = complete;
134     }
135 
136     public String toString() {
137         return "["+name+";"+description+";["+versions+"]]";
138     }
139 }