schema update framework
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / SingleTermUpdater.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.database.update;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.common.IProgressMonitor;
19 import eu.etaxonomy.cdm.database.ICdmDataSource;
20
21 /**
22 * @author a.mueller
23 * @date 10.09.2010
24 *
25 */
26 public class SingleTermUpdater extends SchemaUpdaterStepBase {
27 @SuppressWarnings("unused")
28 private static final Logger logger = Logger.getLogger(SingleTermUpdater.class);
29
30 public static final SingleTermUpdater NewInstance(String stepName, UUID uuidTerm, String description, String label, String abbrev, String dtype, Integer orderIndex, UUID uuidVocabulary){
31 return new SingleTermUpdater(stepName, uuidTerm, description, label, abbrev, dtype, orderIndex, uuidVocabulary);
32
33 }
34
35 // private ICdmDataSource datasource;
36 // private IProgressMonitor monitor;
37 private UUID uuidTerm ;
38 private String description;
39 private String label;
40 private String abbrev;
41 private String dtype;
42 private UUID uuidVocabulary;
43 private Integer orderIndex;
44
45
46
47 private SingleTermUpdater(String stepName, UUID uuidTerm, String description, String label, String abbrev, String dtype, Integer orderIndex, UUID uuidVocabulary) {
48 super(stepName);
49 this.abbrev = abbrev;
50 // this.datasource = datasource;
51 // this.monitor = monitor;
52 this.description = description;
53 this.dtype = dtype;
54 this.label = label;
55 this.orderIndex = orderIndex;
56 this.uuidTerm = uuidTerm;
57 this.uuidVocabulary = uuidVocabulary;
58 }
59
60
61
62 public boolean invoke(ICdmDataSource datasource, IProgressMonitor monitor) throws SQLException{
63
64 //vocabulary id
65 int vocId;
66 String sqlVocId = " SELECT id FROM TermVocabulary WHERE uuid = " + uuidVocabulary;
67 ResultSet rs = datasource.executeQuery(sqlVocId);
68 if (rs.next()){
69 vocId = rs.getInt("id");
70 }else{
71 String warning = "Vocabulary ( "+ uuidVocabulary +" ) for term does not exist!";
72 monitor.warning(warning);
73 return false;
74 }
75
76 int termId;
77 String sqlMaxId = " SELECT max(id)+1 as maxId FROM DefinedTermBase";
78 rs = datasource.executeQuery(sqlMaxId);
79 if (rs.next()){
80 termId = rs.getInt("maxId");
81 }else{
82 String warning = "No defined terms do exist yet. Can't update terms!";
83 monitor.warning(warning);
84 return false;
85 }
86
87 String id = Integer.toString(termId);
88 String created = "'2010-09-01 10:15:00'";
89 String defaultColor = "null";
90 String protectedTitleCache = "b'0'";
91 String titleCache = label != null ? label : (abbrev != null ? abbrev : description );
92 String sqlInsertTerm = " INSERT INTO DefinedTermBase (DTYPE, id, uuid, created, protectedtitlecache, titleCache, orderindex, defaultcolor, vocabulary_id)" +
93 "VALUES ('" + dtype + "', " + id + ", '" + uuidTerm + "', '" + created + "', " + protectedTitleCache + ", '" + titleCache + "', " + orderIndex + ", " + defaultColor + ", " + vocId + ")";
94 datasource.executeQuery(sqlInsertTerm);
95
96 //
97 // INSERT INTO DefinedTermBase (DTYPE, id, uuid, created, protectedtitlecache, titleCache, orderindex, defaultcolor, vocabulary_id)
98 // SELECT 'ReferenceSystem' , (@defTermId := max(id)+1) as maxId , '1bb67042-2814-4b09-9e76-c8c1e68aa281', '2010-06-01 10:15:00', b'0', 'Google Earth', null, null, @refSysVocId
99 // FROM DefinedTermBase ;
100 //
101
102 //language id
103 int langId;
104 String sqlLangId = " SELECT id FROM DefinedTermBase WHERE uuid = 'e9f8cdb7-6819-44e8-95d3-e2d0690c3523'";
105 rs = datasource.executeQuery(sqlLangId);
106 if (rs.next()){
107 langId = rs.getInt("id");
108 }else{
109 String warning = "Term for default language (English) not does not exist!";
110 monitor.warning(warning);
111 return false;
112 }
113
114 //representation
115 int repId;
116 sqlMaxId = " SELECT max(id)+1 as maxId FROM Representation";
117 rs = datasource.executeQuery(sqlMaxId);
118 if (rs.next()){
119 repId = rs.getInt("maxId");
120 }else{
121 String warning = "No representations do exist yet. Can't update terms!";
122 monitor.warning(warning);
123 return false;
124 }
125
126 UUID uuidRepresentation = UUID.randomUUID();
127 String sqlInsertRepresentation = " INSERT INTO Representation (id, created, uuid, text, abbreviatedlabel, label, language_id) " +
128 "VALUES (" + repId + ", '" + created + "', '" + uuidRepresentation + "', '" + description + "', '" + label + "', '" + abbrev + "', " + langId + ")";
129
130 datasource.executeQuery(sqlInsertRepresentation);
131
132
133 // -- representation
134 // INSERT INTO Representation (id, created, uuid, text, abbreviatedlabel, label, language_id)
135 // SELECT ( @repId := max(id)+1 ) AS maxId ,'2010-06-01 18:49:07','fadb1730-9936-44e7-8911-884a84662b08', 'Google Earth','Google','Google Earth', @langId
136 // FROM Representation;
137 // ;
138
139 //
140 // -- representation
141 // INSERT INTO Representation (id, created, uuid, text, abbreviatedlabel, label, language_id)
142 // SELECT ( @repId := max(id)+1 ) AS maxId ,'2010-06-01 18:49:07','fadb1730-9936-44e7-8911-884a84662b08', 'Google Earth','Google','Google Earth', @langId
143 // FROM Representation;
144 // ;
145
146 String sqlInsertMN = "INSERT INTO DefinedTermBase_Representation (DefinedTermBase_id, representations_id) " +
147 " VALUES ("+ termId +"," +repId+ " )";
148
149 datasource.executeUpdate(sqlInsertMN);
150
151 // -- defTerm <-> representation
152 // INSERT INTO DefinedTermBase_Representation (DefinedTermBase_id, representations_id)
153 // VALUES (@defTermId,@repId);
154 //
155 return true;
156 }
157
158 }