View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * @author mchyzer
18   * $Id: SubjectDdl.java,v 1.12 2009-01-31 16:46:41 mchyzer Exp $
19   */
20  package edu.internet2.middleware.grouper.ddl;
21  
22  import java.sql.Types;
23  
24  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.model.Database;
25  import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.model.Table;
26  
27  
28  
29  /**
30   * ddl versions and stuff for grouper.  All ddl classes must have a currentVersion method that
31   * returns the current version
32   */
33  public enum SubjectDdl implements DdlVersionable {
34  
35    /** first version of grouper, make sure the ddl table is there */
36    V1 {
37      /**
38       * add the subject table
39       * @see SubjectDdl#updateVersionFromPrevious(org.apache.ddlutils.model.Database, 
40        DdlVersionBean)
41       */
42      @Override
43      public void updateVersionFromPrevious(Database database, DdlVersionBean ddlVersionBean) {
44  
45        {
46          Table subjectTable = GrouperDdlUtils.ddlutilsFindOrCreateTable(database,
47              "subject");
48    
49          GrouperDdlUtils.ddlutilsFindOrCreateColumn(subjectTable, "subjectId", 
50              Types.VARCHAR, "255", true, true);
51    
52          GrouperDdlUtils.ddlutilsFindOrCreateColumn(subjectTable, "subjectTypeId", 
53              Types.VARCHAR, "32", false, true);
54    
55          GrouperDdlUtils.ddlutilsFindOrCreateColumn(subjectTable, "name", 
56              Types.VARCHAR, "255", false, false);
57    
58        }
59        {
60          Table subjectTable = GrouperDdlUtils.ddlutilsFindOrCreateTable(database,
61              "subjectattribute");
62    
63          GrouperDdlUtils.ddlutilsFindOrCreateColumn(subjectTable, "subjectId", 
64              Types.VARCHAR, "255", true, true);
65    
66          GrouperDdlUtils.ddlutilsFindOrCreateColumn(subjectTable, "name", 
67              Types.VARCHAR, "255", true, true);
68    
69          GrouperDdlUtils.ddlutilsFindOrCreateColumn(subjectTable, "value", 
70              Types.VARCHAR, "255", true, true);
71    
72          GrouperDdlUtils.ddlutilsFindOrCreateColumn(subjectTable, "searchValue", 
73              Types.VARCHAR, "255", false, false);
74    
75          GrouperDdlUtils.ddlutilsFindOrCreateIndex(database, subjectTable.getName(), 
76              "searchattribute_value_idx", false, "value");
77  
78          GrouperDdlUtils.ddlutilsFindOrCreateIndex(database, subjectTable.getName(), 
79              "searchattribute_id_name_idx", true, "subjectId", "name");
80          
81          GrouperDdlUtils.ddlutilsFindOrCreateIndex(database, subjectTable.getName(), 
82              "searchattribute_name_idx", false, "name");
83  
84        }
85        
86      }
87  
88      @Override
89      public String getGrouperVersion() {
90        return "1.4.0";
91      }
92    };
93  
94    @Override
95    public boolean requiresEmptyChangelog() {
96      // TODO Auto-generated method stub
97      return false;
98    }
99  
100   /**
101    * @see edu.internet2.middleware.grouper.ddl.DdlVersionable#getVersion()
102    */
103   public int getVersion() {
104     return GrouperDdlUtils.versionIntFromEnum(this);
105   }
106 
107   /**
108    * cache this
109    */
110   private static int currentVersion = -1;
111   
112   /**
113    * keep the current version here, increment as things change
114    * @return the current version
115    */
116   public static int currentVersion() {
117     if (currentVersion == -1) {
118       int max = -1;
119       for (SubjectDdl grouperDdl : SubjectDdl.values()) {
120         String number = grouperDdl.name().substring(1);
121         int theInt = Integer.parseInt(number);
122         max = Math.max(max, theInt);
123       }
124       currentVersion = max;
125     }
126     return currentVersion;
127   }
128 
129   /**
130    * @see edu.internet2.middleware.grouper.ddl.DdlVersionable#getObjectName()
131    */
132   public String getObjectName() {
133     return GrouperDdlUtils.objectName(this);
134   }
135 
136   /**
137    * @see edu.internet2.middleware.grouper.ddl.DdlVersionable#getDefaultTablePattern()
138    */
139   public String getDefaultTablePattern() {
140     return "SUBJECT%";
141   }
142   
143   /**
144    * @see edu.internet2.middleware.grouper.ddl.DdlVersionable#updateVersionFromPrevious(org.apache.ddlutils.model.Database, DdlVersionBean)
145    */
146   public abstract void updateVersionFromPrevious(Database database, 
147       DdlVersionBean ddlVersionBean);  
148   
149   /**
150    * drop all views
151    * @param ddlVersionBean 
152    */
153   public void dropAllViews(DdlVersionBean ddlVersionBean) {
154     
155   }
156   
157   /**
158    * add all foreign keys
159    * @param ddlVersionBean 
160    */
161   public void addAllForeignKeysViewsEtc(DdlVersionBean ddlVersionBean) {
162     Database database = ddlVersionBean.getDatabase();
163 
164     
165     GrouperDdlUtils.ddlutilsFindOrCreateForeignKey(database, "subjectattribute", 
166         "fk_subjectattr_subjectid", "subject", "subjectId", "subjectId");
167 
168     GrouperDdlUtils.ddlutilsTableComment(ddlVersionBean, 
169         "subject", "sample subject table for grouper unit tests");
170 
171     GrouperDdlUtils.ddlutilsColumnComment(ddlVersionBean, "subject", "subjectId", 
172         "subject id of row");
173 
174     GrouperDdlUtils.ddlutilsColumnComment(ddlVersionBean, "subject", "subjectTypeId", 
175         "subject type e.g. person");
176 
177     GrouperDdlUtils.ddlutilsColumnComment(ddlVersionBean, "subject", "name", 
178         "name of this subject");
179 
180     
181     GrouperDdlUtils.ddlutilsTableComment(ddlVersionBean, 
182         "subjectattribute", "attribute data for each subject");
183 
184     GrouperDdlUtils.ddlutilsColumnComment(ddlVersionBean, 
185         "subjectattribute",  "subjectId", 
186         "subject id of row");
187 
188     GrouperDdlUtils.ddlutilsColumnComment(ddlVersionBean, 
189         "subjectattribute",  "name", 
190         "name of attribute");
191 
192     GrouperDdlUtils.ddlutilsColumnComment(ddlVersionBean, 
193         "subjectattribute",  "value", 
194         "value of attribute");
195 
196     GrouperDdlUtils.ddlutilsColumnComment(ddlVersionBean, 
197         "subjectattribute",  "searchValue", 
198         "search value (e.g. all lower)");
199 
200   }
201   
202   /**
203    * an example table name so we can hone in on the exact metadata
204    * @return the table name
205    */
206   public String[] getSampleTablenames() {
207     return new String[]{"subject","subjectattribute"};
208   }
209 
210   /**
211    * @see edu.internet2.middleware.grouper.ddl.DdlVersionable#recreateViewsAndForeignKeys()
212    */
213   public boolean recreateViewsAndForeignKeys() {
214     
215     // assume true
216     return true;
217   }
218 }