Mercurial > dive4elements > framework
comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 159:db0d20440b92
Added code to create collections.
artifacts/trunk@1384 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 03 Mar 2011 12:13:24 +0000 |
parents | d718a4d55662 |
children | e4a1562dfc21 |
comparison
equal
deleted
inserted
replaced
158:d718a4d55662 | 159:db0d20440b92 |
---|---|
132 public static final String SQL_OUTDATE_ARTIFACTS_USER = | 132 public static final String SQL_OUTDATE_ARTIFACTS_USER = |
133 SQL.get("outdate.artifacts.user"); | 133 SQL.get("outdate.artifacts.user"); |
134 | 134 |
135 public static final String SQL_DELETE_USER_COLLECTION_ITEMS = | 135 public static final String SQL_DELETE_USER_COLLECTION_ITEMS = |
136 SQL.get("delete.user.collection.items"); | 136 SQL.get("delete.user.collection.items"); |
137 | |
138 public static final String SQL_COLLECTIONS_NEXT_ID = | |
139 SQL.get("collections.id.nextval"); | |
140 | |
141 public static final String SQL_COLLECTIONS_INSERT = | |
142 SQL.get("collections.insert"); | |
137 | 143 |
138 /** The singleton.*/ | 144 /** The singleton.*/ |
139 protected static Backend instance; | 145 protected static Backend instance; |
140 | 146 |
141 /** | 147 /** |
1088 } | 1094 } |
1089 | 1095 |
1090 return null; | 1096 return null; |
1091 } | 1097 } |
1092 | 1098 |
1099 public ArtifactCollection createCollection( | |
1100 String ownerIdentifier, | |
1101 String name, | |
1102 ArtifactCollectionFactory factory, | |
1103 Document data, | |
1104 Object context | |
1105 ) { | |
1106 if (name == null) { | |
1107 logger.debug("Name is null"); | |
1108 return null; | |
1109 } | |
1110 | |
1111 if (!StringUtils.checkUUID(ownerIdentifier)) { | |
1112 logger.debug("Invalid owner id: '" + ownerIdentifier + "'"); | |
1113 return null; | |
1114 } | |
1115 | |
1116 Connection conn = null; | |
1117 ResultSet result = null; | |
1118 PreparedStatement stmnt = null; | |
1119 | |
1120 DataSource dataSource = DBConnection.getDataSource(); | |
1121 try { | |
1122 conn.setAutoCommit(false); | |
1123 try { | |
1124 conn = dataSource.getConnection(); | |
1125 | |
1126 // fetch owner id | |
1127 stmnt = conn.prepareStatement(SQL_USERS_SELECT_ID_BY_GID); | |
1128 stmnt.setString(1, ownerIdentifier); | |
1129 result = stmnt.executeQuery(); | |
1130 | |
1131 if (!result.next()) { // no such user | |
1132 return null; | |
1133 } | |
1134 | |
1135 int ownerId = result.getInt(1); | |
1136 result.close(); result = null; | |
1137 stmnt.close(); stmnt = null; | |
1138 | |
1139 // fetch new collection seq number. | |
1140 stmnt = conn.prepareStatement(SQL_COLLECTIONS_NEXT_ID); | |
1141 result = stmnt.executeQuery(); | |
1142 | |
1143 if (!result.next()) { // no identifier generated | |
1144 return null; | |
1145 } | |
1146 | |
1147 int id = result.getInt(1); | |
1148 result.close(); result = null; | |
1149 stmnt.close(); stmnt = null; | |
1150 | |
1151 String identifier = newIdentifier(); | |
1152 | |
1153 stmnt = conn.prepareStatement(SQL_COLLECTIONS_INSERT); | |
1154 | |
1155 stmnt.setInt(1, id); | |
1156 stmnt.setString(2, identifier); | |
1157 stmnt.setInt(3, ownerId); | |
1158 stmnt.setString(4, name); | |
1159 | |
1160 // XXX: A bit odd: we don't have a collection, yet. | |
1161 Long ttl = factory.timeToLiveUntouched(null, context); | |
1162 | |
1163 if (ttl == null) { | |
1164 stmnt.setNull(5, Types.BIGINT); | |
1165 } | |
1166 else { | |
1167 stmnt.setLong(5, ttl); | |
1168 } | |
1169 | |
1170 stmnt.execute(); | |
1171 conn.commit(); | |
1172 | |
1173 return factory.createCollection( | |
1174 identifier, name, data, context); | |
1175 } | |
1176 catch (SQLException sqle) { | |
1177 conn.rollback(); | |
1178 throw sqle; | |
1179 } | |
1180 } | |
1181 catch (SQLException sqle) { | |
1182 logger.error(sqle.getLocalizedMessage(), sqle); | |
1183 } | |
1184 finally { | |
1185 if (result != null) { | |
1186 try { result.close(); } | |
1187 catch (SQLException sqle) {} | |
1188 } | |
1189 if (stmnt != null) { | |
1190 try { stmnt.close(); } | |
1191 catch (SQLException sqle) {} | |
1192 } | |
1193 if (conn != null) { | |
1194 try { conn.close(); } | |
1195 catch (SQLException sqle) {} | |
1196 } | |
1197 } | |
1198 return null; | |
1199 } | |
1200 | |
1093 public ArtifactCollection [] listCollections(String userId) { | 1201 public ArtifactCollection [] listCollections(String userId) { |
1094 // TODO: Implement me! | |
1095 return null; | |
1096 } | |
1097 | |
1098 public ArtifactCollection createCollection( | |
1099 String ownerId, | |
1100 Document data, | |
1101 Object context, | |
1102 ArtifactCollectionFactory factory) | |
1103 { | |
1104 // TODO: Implement me! | 1202 // TODO: Implement me! |
1105 return null; | 1203 return null; |
1106 } | 1204 } |
1107 | 1205 |
1108 public boolean deleteCollection(String collectionId) { | 1206 public boolean deleteCollection(String collectionId) { |