comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 176:a0eff2227588

Added code to add artifacts to collections. artifacts/trunk@1402 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 04 Mar 2011 17:51:15 +0000
parents 16e6e661e6bf
children 77cd37a93fca
comparison
equal deleted inserted replaced
175:16e6e661e6bf 176:a0eff2227588
152 public static final String SQL_DELETE_COLLECTION_ITEMS = 152 public static final String SQL_DELETE_COLLECTION_ITEMS =
153 SQL.get("delete.collection.items"); 153 SQL.get("delete.collection.items");
154 154
155 public static final String SQL_DELETE_COLLECTION = 155 public static final String SQL_DELETE_COLLECTION =
156 SQL.get("delete.collection"); 156 SQL.get("delete.collection");
157
158 public static final String SQL_COLLECTION_CHECK_ARTIFACT =
159 SQL.get("collection.check.artifact");
160
161 public static final String SQL_COLLECTION_ITEMS_ID_NEXTVAL =
162 SQL.get("collection.check.artifact");
163
164 public static final String SQL_COLLECTION_ITEMS_INSERT =
165 SQL.get("collection.items.insert");
157 166
158 /** The singleton.*/ 167 /** The singleton.*/
159 protected static Backend instance; 168 protected static Backend instance;
160 169
161 /** 170 /**
1111 // TODO: Implement me! 1120 // TODO: Implement me!
1112 return false; 1121 return false;
1113 } 1122 }
1114 1123
1115 public boolean addCollectionArtifact( 1124 public boolean addCollectionArtifact(
1116 String collectionId, 1125 final String collectionId,
1117 String artifactId 1126 final String artifactId,
1127 final Document attribute
1118 ) { 1128 ) {
1119 // TODO: Implement me! 1129 if (!StringUtils.checkUUID(collectionId)) {
1120 return false; 1130 logger.debug("Invalid collection id: '" + collectionId + "'");
1131 return false;
1132 }
1133
1134 if (!StringUtils.checkUUID(artifactId)) {
1135 logger.debug("Invalid artifact id: '" + artifactId + "'");
1136 return false;
1137 }
1138
1139 SQLExecutor exec = new SQLExecutor() {
1140 public boolean doIt() throws SQLException {
1141 // fetch artifact id
1142 prepareStatement(SQL_GET_ID);
1143 stmnt.setString(1, artifactId);
1144 result = stmnt.executeQuery();
1145 if (!result.next()) {
1146 logger.debug("No such artifact: " + artifactId);
1147 return false;
1148 }
1149 int aid = result.getInt(1);
1150 reset();
1151
1152 // fetch collection id
1153 prepareStatement(SQL_COLLECTIONS_ID_BY_GID);
1154 stmnt.setString(1, collectionId);
1155 result = stmnt.executeQuery();
1156 if (!result.next()) {
1157 logger.debug("No such collection: " + collectionId);
1158 }
1159 int cid = result.getInt(1);
1160 reset();
1161
1162 // check if artifact is already in collection
1163 prepareStatement(SQL_COLLECTION_CHECK_ARTIFACT);
1164 stmnt.setInt(1, aid);
1165 stmnt.setInt(2, cid);
1166 result = stmnt.executeQuery();
1167 if (result.next()) {
1168 logger.debug("artifact already in collection");
1169 return false;
1170 }
1171 reset();
1172
1173 // fetch fresh id for new collection item
1174 prepareStatement(SQL_COLLECTION_ITEMS_ID_NEXTVAL);
1175 result = stmnt.executeQuery();
1176 if (!result.next()) {
1177 logger.debug("no collection item id generated");
1178 return false;
1179 }
1180 int ci_id = result.getInt(1);
1181 reset();
1182
1183 // insert new collection item
1184 prepareStatement(SQL_COLLECTION_ITEMS_INSERT);
1185 stmnt.setInt(1, ci_id);
1186 stmnt.setInt(2, cid);
1187 stmnt.setInt(3, aid);
1188
1189 byte [] data = attribute != null
1190 ? XMLUtils.toByteArray(attribute)
1191 : null;
1192
1193 if (data == null) {
1194 stmnt.setNull(4, Types.BINARY);
1195 }
1196 else {
1197 stmnt.setBytes(4, data);
1198 }
1199 stmnt.execute();
1200 conn.commit();
1201
1202 return true;
1203 }
1204 };
1205 return exec.runWrite();
1121 } 1206 }
1122 1207
1123 public boolean removeCollectionArtifact( 1208 public boolean removeCollectionArtifact(
1124 String collectionId, 1209 String collectionId,
1125 String artifactId 1210 String artifactId

http://dive4elements.wald.intevation.org