Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java @ 80:4c9ceb28cfd0
fix some NPEs with refreshRelations
| author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
|---|---|
| date | Fri, 16 Jun 2017 15:41:39 +0200 |
| parents | e0be7c0030f5 |
| children | 4845dff46ad9 |
| rev | line source |
|---|---|
| 1 | 1 package org.mpi.openmind.repository.services; |
| 2 | |
| 3 import java.text.DecimalFormat; | |
| 4 import java.util.ArrayList; | |
| 5 import java.util.List; | |
| 6 import java.util.Map; | |
| 7 | |
| 8 import org.apache.commons.lang.StringUtils; | |
| 9 import org.apache.log4j.Logger; | |
| 10 import org.hibernate.Query; | |
| 11 import org.hibernate.Session; | |
| 12 import org.mpi.openmind.configuration.ConfigurationService; | |
| 13 import org.mpi.openmind.repository.bo.Attribute; | |
| 14 import org.mpi.openmind.repository.bo.Entity; | |
| 15 import org.mpi.openmind.repository.bo.Node; | |
| 16 import org.mpi.openmind.repository.bo.Relation; | |
| 17 import org.mpi.openmind.repository.bo.View; | |
| 18 import org.mpi.openmind.repository.bo.utils.Sequence; | |
| 19 import org.mpi.openmind.repository.utils.HibernateUtil; | |
| 20 import org.mpi.openmind.repository.utils.OwnValueGenerator; | |
| 21 | |
| 22 /** | |
| 23 * | |
| 24 * @author jurzua | |
| 25 */ | |
| 26 public abstract class AbstractPersistenceService { | |
| 27 | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
28 private ConfigurationService configurationService; |
| 1 | 29 |
| 30 private OwnValueGenerator ownValueGenerator; | |
| 31 | |
| 32 private final static String NODE_SEQUENCE = "nodeSequence"; | |
| 33 | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
34 private static Logger logger = Logger.getLogger(AbstractPersistenceService.class); |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
35 |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
36 public static final String TRANSACTION_LOGGER = "openmind.transactionlog"; |
| 1 | 37 |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
38 private static Logger txLog = Logger.getLogger(TRANSACTION_LOGGER); |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
39 |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
40 private boolean importMode = false; |
| 1 | 41 |
| 42 public AbstractPersistenceService() { | |
| 43 } | |
| 44 | |
| 45 public String generateOwnValue(Entity entity) { | |
| 46 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 47 String ownValue = null; | |
| 48 | |
| 49 try { | |
| 50 session.getTransaction().begin(); | |
| 51 ownValue = this.ownValueGenerator.generateOwnValue(entity, session); | |
| 52 } catch (Exception e) { | |
| 53 logger.error(e.getMessage(), e); | |
| 54 // e.printStackTrace(); | |
| 55 } finally { | |
| 56 session.getTransaction().commit(); | |
| 57 } | |
| 58 | |
| 59 return ownValue; | |
| 60 } | |
| 61 | |
| 62 public int dropAssertions() { | |
| 63 int row = 0; | |
| 64 try { | |
| 65 Session session = HibernateUtil.getSessionFactory() | |
| 66 .getCurrentSession(); | |
| 67 session.beginTransaction(); | |
| 68 String sql = "DELETE FROM Node WHERE type = 'ABox'"; | |
| 69 Query query = session.createQuery(sql); | |
| 70 row = query.executeUpdate(); | |
| 71 | |
| 72 logger.info("Drop assertions - rows deleted= " + row); | |
| 73 | |
| 74 session.getTransaction().commit(); | |
| 75 } catch (Exception e) { | |
| 76 logger.error(e.getMessage(), e); | |
| 77 // e.printStackTrace(); | |
| 78 } | |
| 79 return row; | |
| 80 } | |
| 81 | |
| 82 public int dropDefinitions() { | |
| 83 int row = 0; | |
| 84 try { | |
| 85 Session session = HibernateUtil.getSessionFactory() | |
| 86 .getCurrentSession(); | |
| 87 session.beginTransaction(); | |
| 88 String sql = "DELETE FROM Node WHERE type = 'TBox'"; | |
| 89 Query query = session.createQuery(sql); | |
| 90 row = query.executeUpdate(); | |
| 91 | |
| 92 logger.info("Drop definitions - rows deleted= " + row); | |
| 93 | |
| 94 session.getTransaction().commit(); | |
| 95 } catch (Exception e) { | |
| 96 logger.error(e.getMessage(), e); | |
| 97 // e.printStackTrace(); | |
| 98 } | |
| 99 return row; | |
| 100 } | |
| 101 | |
| 102 public void saveOrUpdateObject(Object obj) { | |
| 103 try { | |
| 104 Session session = HibernateUtil.getSessionFactory() | |
| 105 .getCurrentSession(); | |
| 106 session.getTransaction().begin(); | |
| 107 session.saveOrUpdate(obj); | |
| 108 session.getTransaction().commit(); | |
| 109 } catch (Exception e) { | |
| 110 logger.error(e.getMessage(), e); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 private void saveNode0(Session session, Node node, Sequence idSequence){ | |
| 115 node.autoNormalize(); | |
| 116 node.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
| 117 | |
| 118 if(node.getModificationTime() == null){ | |
| 119 node.setModificationTime(System.currentTimeMillis()); | |
| 120 } | |
| 121 if(node.getVersion() == null){ | |
| 122 node.increaseVersion(); | |
| 123 } | |
| 124 | |
| 125 if (node.getId() == null){ | |
| 126 node.setId(idSequence.generateId()); | |
| 127 } | |
| 128 | |
| 129 if (node.getRowId() == null){ | |
|
34
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
130 logger.debug("Save node: "+node.toString()); |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
131 txLog.info("save node: "+node.toEncString()); |
| 1 | 132 session.save(node); |
| 133 }else{ | |
|
34
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
134 txLog.info("merge node: "+node.toEncString()); |
| 1 | 135 session.merge(node); |
| 136 } | |
| 137 | |
| 138 logger.info("Saved node\t" + node); | |
| 139 } | |
| 140 | |
| 141 public void saveNode(Node node) throws Exception { | |
| 142 | |
| 143 logger.info("Saving node\t" + node); | |
| 144 | |
| 145 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 146 session.getTransaction().begin(); | |
| 147 | |
| 148 Sequence idSequence = this.getIdSequence(session); | |
| 149 | |
| 150 this.saveNode0(session, node, idSequence); | |
| 151 | |
| 152 session.save(idSequence); | |
| 153 | |
| 154 session.getTransaction().commit(); | |
| 155 logger.info("Node saved\t" + node); | |
| 156 } | |
| 157 | |
| 158 public void saveNodeList(List<Node> nodeList) throws Exception{ | |
| 159 | |
| 160 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 161 session.getTransaction().begin(); | |
| 162 | |
| 163 Sequence idSequence = this.getIdSequence(session); | |
| 164 /* | |
| 165 long start = System.currentTimeMillis(); | |
| 166 DecimalFormat df = new DecimalFormat("#.##"); | |
| 167 int counter = 0; | |
| 168 */ | |
| 169 logger.info("##### Saving Node List: " + nodeList.size() + " #####"); | |
| 170 | |
| 171 for (Node node : nodeList) { | |
| 172 this.saveNode0(session, node, idSequence); | |
| 173 /* | |
| 174 double percent = ((double) counter / (double) nodeList.size()) * 100.0; | |
| 175 long diff = System.currentTimeMillis() - start; | |
| 176 double min = (double) diff / (double) (60 * 1000); | |
| 177 | |
| 178 if ((percent % 10) < 0.005) { | |
| 179 logger.debug("\n[" + df.format(percent) + " %] counter: " | |
| 180 + counter + " / " + nodeList.size()); | |
| 181 | |
| 182 logger.debug("Tempo: " + (double) counter / min); | |
| 183 } | |
| 184 counter++; | |
| 185 */ | |
| 186 } | |
| 187 | |
| 188 session.save(idSequence); | |
| 189 session.getTransaction().commit(); | |
| 190 } | |
| 191 | |
| 192 /** | |
| 193 * This method delete the nodes and its history. | |
| 194 * | |
| 195 * @param nodeList | |
| 196 */ | |
| 197 public void removeNode(Node node) { | |
| 198 try { | |
| 199 Session session = HibernateUtil.getSessionFactory() | |
| 200 .getCurrentSession(); | |
| 201 session.getTransaction().begin(); | |
| 202 | |
| 203 this.removeNodesById(node.getId(), session); | |
| 204 | |
| 205 session.getTransaction().commit(); | |
| 206 } catch (Exception e) { | |
| 207 logger.error(e.getMessage(), e); | |
| 208 // e.printStackTrace(); | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 /** | |
| 213 * This method delete the nodes and its history. | |
| 214 * | |
| 215 * @param nodeList | |
| 216 */ | |
| 217 public void removeNodeList(List<Node> nodeList) { | |
| 218 try { | |
| 219 Session session = HibernateUtil.getSessionFactory() | |
| 220 .getCurrentSession(); | |
| 221 session.getTransaction().begin(); | |
| 222 | |
| 223 logger.debug("##### Deleting Node List: " + nodeList.size() | |
| 224 + " #####"); | |
| 225 | |
| 226 for (Node node : nodeList) { | |
| 227 this.removeNodesById(node.getId(), session); | |
| 228 } | |
| 229 | |
| 230 session.getTransaction().commit(); | |
| 231 } catch (Exception e) { | |
| 232 logger.error(e.getMessage(), e); | |
| 233 // e.printStackTrace(); | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 /** | |
| 238 * Save every part of the entity but not generate new versions nor ownValue | |
| 239 * | |
| 240 * @param nodeList | |
| 241 */ | |
| 242 public void saveEntityListAsNode(List<Entity> entList) { | |
| 243 int entitiesSize = 0; | |
| 244 int nodeSize = 0; | |
| 245 long start = System.currentTimeMillis(); | |
| 246 try { | |
| 247 Session session = HibernateUtil.getSessionFactory() | |
| 248 .getCurrentSession(); | |
| 249 session.getTransaction().begin(); | |
| 250 | |
| 251 Sequence idSequence = this.getIdSequence(session); | |
| 252 | |
| 253 DecimalFormat df = new DecimalFormat("#.##"); | |
| 254 int counter = 0; | |
| 255 | |
| 256 List<Node> nodeList = generateNodeList(entList); | |
| 257 logger.debug("##### Saving Node List #####"); | |
| 258 logger.debug("Entities: " + entList.size() + " #####"); | |
| 259 logger.debug("Nodes: " + nodeList.size() + " #####"); | |
| 260 | |
| 261 entitiesSize = entList.size(); | |
| 262 nodeSize = nodeList.size(); | |
| 263 | |
| 264 for (Node node : nodeList) { | |
| 265 | |
| 266 node.autoNormalize(); | |
| 267 | |
| 268 if (node.getId() == null) | |
| 269 node.setId(idSequence.generateId()); | |
|
34
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
270 if (node.getRowId() == null) { |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
271 logger.debug("Save node from EntityListAsNode: "+node); |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
272 txLog.info("save node: "+node.toEncString()); |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
273 session.save(node); |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
274 } else { |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
275 txLog.info("merge node: "+node.toEncString()); |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
276 session.merge(node); |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
277 |
|
5737ab564b94
more transaction logging. logs more attributes and logs nodes.
casties
parents:
33
diff
changeset
|
278 } |
| 1 | 279 |
| 280 double percent = ((double) counter / (double) nodeList.size()) * 100.0; | |
| 281 long diff = System.currentTimeMillis() - start; | |
| 282 double min = (double) diff / (double) (60 * 1000); | |
| 283 | |
| 284 if ((percent % 10) < 0.005) { | |
| 285 logger.debug("\n[" + df.format(percent) + " %] counter: " | |
| 286 + counter + " / " + nodeList.size()); | |
| 287 | |
| 288 logger.debug("Tempo: " + (double) counter / min); | |
| 289 } | |
| 290 counter++; | |
| 291 } | |
| 292 | |
| 293 session.save(idSequence); | |
| 294 session.getTransaction().commit(); | |
| 295 | |
| 296 StringBuilder sb = new StringBuilder(); | |
| 297 sb.append("\n\t#### saveEntityListAsNode ####\n"); | |
| 298 sb.append("\tentitiesSize=\t" + entitiesSize + "\n"); | |
| 299 sb.append("\tnodeSize=\t" + nodeSize + "\n"); | |
| 300 sb.append("\ttime[ms]=\t" + (System.currentTimeMillis() - start) | |
| 301 + "\n"); | |
| 302 logger.info(sb.toString()); | |
| 303 | |
| 304 } catch (Exception e) { | |
| 305 logger.error(e.getMessage(), e); | |
| 306 e.printStackTrace(); | |
| 307 } | |
| 308 } | |
| 309 | |
| 310 private List<Node> generateNodeList(List<Entity> list) { | |
| 311 List<Node> list0 = new ArrayList<Node>(); | |
| 312 for (Entity ent : list) { | |
| 313 list0.add(ent); | |
| 314 for (Attribute att : ent.getAttributes()) { | |
| 315 list0.add(att); | |
| 316 } | |
| 317 for (Relation rel : ent.getSourceRelations()) { | |
| 318 list0.add(rel); | |
| 319 } | |
| 320 for (Relation rel : ent.getTargetRelations()) { | |
| 321 list0.add(rel); | |
| 322 } | |
| 323 } | |
| 324 return list0; | |
| 325 } | |
| 326 | |
| 327 /** | |
| 38 | 328 * Saves all given entities without attributes and relations without creating new versions. |
| 329 * | |
| 1 | 330 * @param nodeList |
| 331 */ | |
| 38 | 332 public void saveEntityListAsNodeWithoutContent(List<Entity> nodeList) throws Exception { |
| 333 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 334 try { | |
| 335 session.getTransaction().begin(); | |
| 336 Sequence idSequence = this.getIdSequence(session); | |
| 337 | |
| 338 logger.debug("START saving Node list of size " + nodeList.size()); | |
| 1 | 339 |
| 38 | 340 for (Entity node : nodeList) { |
| 341 node.autoNormalize(); | |
| 342 if (node.getId() == null) { | |
| 343 node.setId(idSequence.generateId()); | |
| 344 } | |
| 345 if (node.getRowId() == null) { | |
| 346 // node is new | |
| 347 logger.debug("Saving node from EntityListAsNodeWithoutContent: " + node); | |
| 348 txLog.info("save node: " + node.toEncString()); | |
| 349 session.save(node); | |
| 350 } else { | |
| 351 txLog.info("merge node: " + node.toEncString()); | |
| 352 session.merge(node); | |
| 353 } | |
| 354 } | |
| 355 // update sequence | |
| 356 session.save(idSequence); | |
| 357 | |
| 358 logger.debug("END saving Node list."); | |
| 359 | |
| 360 } catch (Exception e) { | |
| 361 logger.error(e); | |
| 362 } finally { | |
| 363 session.getTransaction().commit(); | |
| 364 } | |
| 1 | 365 } |
| 366 | |
| 367 public void deleteEntityList(List<Entity> entities) { | |
| 368 try { | |
| 369 Session session = HibernateUtil.getSessionFactory() | |
| 370 .getCurrentSession(); | |
| 371 session.getTransaction().begin(); | |
| 372 | |
| 373 for (Entity ent : entities) { | |
| 374 this.removePersistenceEntity(ent, session); | |
| 375 } | |
| 376 | |
| 377 session.getTransaction().commit(); | |
| 378 } catch (Exception e) { | |
| 379 logger.error(e.getMessage(), e); | |
| 380 // e.printStackTrace(); | |
| 381 } | |
| 382 } | |
| 383 | |
| 384 public void saveEntityList(List<Entity> entities) throws Exception { | |
| 385 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 386 session.getTransaction().begin(); | |
| 387 | |
| 388 for (Entity entity : entities) { | |
| 389 saveEntity0(session, entity); | |
| 390 } | |
| 391 | |
| 392 session.getTransaction().commit(); | |
| 393 } | |
| 394 | |
| 395 public Map<Long, Long> saveEntityListAsNew(List<Entity> entities, | |
| 396 Map<Long, Long> idMap) throws Exception { | |
| 397 | |
| 398 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 399 session.getTransaction().begin(); | |
| 400 Sequence idSequence = this.getIdSequence(session); | |
| 401 | |
| 402 for (Entity entity : entities) { | |
| 403 | |
| 404 Long oldId = entity.getId(); | |
| 405 entity.resetId(); | |
| 406 entity.resetRowId(); | |
| 407 this.saveEntity0(session, entity); | |
| 408 idMap.put(oldId, entity.getId()); | |
| 409 } | |
| 410 | |
| 411 session.save(idSequence); | |
| 412 session.getTransaction().commit(); | |
| 413 return idMap; | |
| 414 } | |
| 415 | |
| 416 /** | |
| 417 * <p> | |
| 418 * This method deletes all entities by type (TBox/ABox), it means the method | |
| 419 * deletes either all concepts or all assertions. | |
| 420 * </p> | |
| 421 * <p> | |
| 422 * The history of every deleted entity will be removed too. | |
| 423 * </p> | |
| 424 * | |
| 425 * @param type | |
| 426 * Node.TYPE_TBOX or Node.TYPE_ABOX | |
| 427 */ | |
| 428 @Deprecated | |
| 429 public void deleteEntities(Long id, String type, Boolean deleteHistory) { | |
| 430 if (!(Node.TYPE_ABOX.equals(type) || Node.TYPE_TBOX.equals(type))) { | |
| 431 throw new IllegalArgumentException( | |
| 432 "The parameter 'type' should be either: " + Node.TYPE_ABOX | |
| 433 + " or " + Node.TYPE_TBOX + ", but was: " + type); | |
| 434 } | |
| 435 | |
| 436 List<Entity> entList = this.getEntities(id, | |
| 437 Node.SYS_STATUS_CURRENT_VERSION, type, null); | |
| 438 | |
| 439 // loading previous versions | |
| 440 List<Entity> historyEntList = new ArrayList<Entity>(); | |
| 441 if (deleteHistory) { | |
| 442 for (Entity ent : entList) { | |
| 443 historyEntList.addAll(this.getEntities(ent.getId(), | |
| 444 Node.SYS_STATUS_PREVIOUS_VERSION, type, null)); | |
| 445 } | |
| 446 } | |
| 447 | |
| 448 try { | |
| 449 Session session = HibernateUtil.getSessionFactory() | |
| 450 .getCurrentSession(); | |
| 451 session.getTransaction().begin(); | |
| 452 | |
| 453 for (Entity ent : entList) { | |
| 454 this.removePersistenceEntity(ent, session); | |
| 455 } | |
| 456 if (deleteHistory) { | |
| 457 for (Entity ent : historyEntList) { | |
| 458 this.removePersistenceEntity(ent, session); | |
| 459 } | |
| 460 } | |
| 461 | |
| 462 session.getTransaction().commit(); | |
| 463 } catch (Exception e) { | |
| 464 logger.error(e.getMessage(), e); | |
| 465 // e.printStackTrace(); | |
| 466 } | |
| 467 } | |
| 468 | |
| 469 /** | |
| 470 * This deleting is made using JDBC and not hibernate. The history is too | |
| 471 * removed by this method. TODO test what could happen with the hibernate | |
| 472 * cache | |
| 473 * | |
| 474 * @param ent | |
| 475 * @param session | |
| 476 */ | |
| 477 private void removePersistenceEntity(Entity ent, Session session) { | |
| 478 for (Attribute att : ent.getAttributes()) { | |
| 479 // session.delete(att); | |
| 480 removeNodesById(att.getId(), session); | |
| 481 for (View view : att.getViews()) { | |
| 482 // session.delete(view); | |
| 483 removeNodesById(view.getId(), session); | |
| 484 } | |
| 485 } | |
| 486 for (Relation rel : ent.getSourceRelations()) { | |
| 487 // session.delete(rel); | |
| 488 removeNodesById(rel.getId(), session); | |
| 489 for (View view : rel.getViews()) { | |
| 490 // session.delete(view); | |
| 491 removeNodesById(view.getId(), session); | |
| 492 } | |
| 493 } | |
| 494 for (Relation rel : ent.getTargetRelations()) { | |
| 495 // session.delete(rel); | |
| 496 removeNodesById(rel.getId(), session); | |
| 497 for (View view : rel.getViews()) { | |
| 498 // session.delete(view); | |
| 499 removeNodesById(view.getId(), session); | |
| 500 } | |
| 501 } | |
| 502 for (View view : ent.getViews()) { | |
| 503 // session.delete(view); | |
| 504 removeNodesById(view.getId(), session); | |
| 505 } | |
| 506 // session.delete(ent); | |
| 507 removeNodesById(ent.getId(), session); | |
| 508 } | |
| 509 | |
| 510 private int removeNodesById(Long id, Session session) { | |
| 511 String sql = "DELETE FROM Node WHERE id = :id"; | |
| 512 Query query = session.createQuery(sql); | |
| 513 query.setLong("id", id); | |
| 514 return query.executeUpdate(); | |
| 515 } | |
| 516 | |
| 517 /** | |
| 518 * <p> | |
| 519 * Changes the system status of the entity from CURRENT_VERSION to | |
| 520 * PREVIOUS_VERSION. | |
| 521 * </p> | |
| 522 * | |
| 523 * <p> | |
| 524 * It means, the entity will stay in the DB, but the it will not be visible | |
| 525 * by the ordinary methods. | |
| 526 * </p> | |
| 527 * | |
| 528 * @param entity | |
| 529 */ | |
| 51 | 530 public void removeEntCurrentVersion(Long entId, String type) throws Exception { |
| 531 logger.info("Deleting entity [ID=" + entId + ", type=" + type + "]. But keeping history in DB."); | |
| 532 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 533 try { | |
| 534 session.getTransaction().begin(); | |
| 1 | 535 |
| 51 | 536 if (entId != null) { |
| 537 /* | |
| 538 * this method get the current version. Related to the relation, | |
| 539 * the time_modification is not consider, because it it | |
| 540 * producing problems. | |
| 541 */ | |
| 542 List<Entity> previousEntityList = this.getEntities(session, entId, Node.SYS_STATUS_CURRENT_VERSION, | |
| 543 type, null, false); | |
| 544 if (previousEntityList.size() > 0) { | |
| 545 if (previousEntityList.size() > 1) { | |
| 546 logger.error("Found more than one current entities!"); | |
| 547 } | |
| 548 Entity previousEntity = previousEntityList.get(0); | |
| 549 logger.info("Saving previous entity: " + previousEntity); | |
| 550 this.persistEntityAsPrevious(session, previousEntity); | |
| 551 } | |
| 552 } | |
| 553 } catch (Exception e) { | |
| 554 logger.error(e); | |
| 555 } finally { | |
| 556 session.getTransaction().commit(); | |
| 557 } | |
| 558 } | |
| 1 | 559 |
| 560 /** | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
561 * Save the entity to the database. |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
562 * |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
563 * Creates a new version of the entity. Runs in a transaction. |
| 1 | 564 * |
| 565 * @param entity | |
| 566 * @return | |
| 567 */ | |
| 568 public void saveEntity(Entity entity) throws Exception { | |
| 36 | 569 // start transaction |
| 1 | 570 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
| 571 session.getTransaction().begin(); | |
| 36 | 572 // save entity |
| 1 | 573 saveEntity0(session, entity); |
| 36 | 574 // end transaction |
| 1 | 575 session.getTransaction().commit(); |
| 576 } | |
| 577 | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
578 /** |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
579 * Save the entity to the database using the given session. |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
580 * |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
581 * Creates a new version and sets the existing entity to PREVIOUS_VERSION. |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
582 * |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
583 * @param session |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
584 * @param entity |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
585 * @throws Exception |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
586 */ |
| 1 | 587 private void saveEntity0(Session session, Entity entity) throws Exception { |
| 588 if (entity.getId() != null) { | |
| 36 | 589 // get the still current entity(s) |
| 1 | 590 List<Entity> previousEntityList = this.getEntities(session, entity.getId(), Node.SYS_STATUS_CURRENT_VERSION, entity.getType(), null, false); |
| 591 if (previousEntityList.size() > 0) { | |
| 592 if (previousEntityList.size() > 1) { | |
| 51 | 593 logger.error("Found more than one current entities!"); |
| 1 | 594 } |
| 36 | 595 // set the first current entity as previous |
| 1 | 596 Entity previousEntity = previousEntityList.get(0); |
|
40
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
39
diff
changeset
|
597 // compare old and new entity |
|
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
39
diff
changeset
|
598 if (! previousEntity.equalsStructure(entity)) { |
|
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
39
diff
changeset
|
599 logger.warn("Entity to save has different structure! old="+previousEntity+" new="+entity); |
|
c181cb6f1761
first version of equalsStructure to test when saving entities.
casties
parents:
39
diff
changeset
|
600 } |
| 1 | 601 logger.info("Saving previous entity: " + previousEntity); |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
602 this.persistEntityAsPrevious(session, previousEntity); |
| 1 | 603 } |
| 604 } | |
| 605 this.saveCurrentEntity(session, entity, null); | |
| 606 } | |
| 607 | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
608 /** |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
609 * Save the entity as (new) current version using the given session. |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
610 * |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
611 * Sets system_status to CURRENT_VERSION and updates row_id, version, modification_time, |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
612 * etc. |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
613 * |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
614 * @param session |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
615 * @param entity |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
616 * @param idSequence |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
617 * @throws Exception |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
618 */ |
| 36 | 619 private void saveCurrentEntity(Session session, Entity entity, Sequence idSequence) throws Exception { |
| 1 | 620 Long time = System.currentTimeMillis(); |
| 57 | 621 refreshEntityRelations(session, entity); |
| 1 | 622 entity.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); |
| 623 entity.resetRowId(); | |
| 624 entity.increaseVersion(); | |
| 625 entity.setObjectClass(entity.getObjectClass()); | |
| 626 entity.setModificationTime(time); | |
| 627 entity.setType(entity.getType()); | |
| 628 entity.setUser(entity.getUser()); | |
| 629 entity.autoNormalize(); | |
| 630 // generating of id, connecting rels, atts and views to the entity | |
| 631 this.prepareEntity(session, entity, idSequence); | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
632 this.persistEntity(session, entity); |
| 1 | 633 } |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
634 |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
635 /** |
| 57 | 636 * Updates the endpoints of the relations of this entity. |
| 637 * | |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
638 * This class must be called, |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
639 * because sometimes the current entity is related to other entities that |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
640 * have been changed in parallel to the edition of this entity. |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
641 * For this reason, some relations of the current entity can have old information of the related entities (like the modification time). |
| 57 | 642 * This method updates the relations of the current entities getting the actual information of the related entities directly from the DB. |
| 643 * | |
| 644 * @author jurzua | |
| 645 * | |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
646 * @param session |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
647 * @param entity |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
648 * @throws Exception |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
649 */ |
| 57 | 650 private void refreshEntityRelations(Session session, Entity entity) throws Exception { |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
651 for(Relation rel : entity.getSourceRelations()){ |
| 57 | 652 refreshRelationTarget(session, rel); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
653 } |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
654 |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
655 for(Relation rel : entity.getTargetRelations()){ |
| 57 | 656 refreshRelationSource(session, rel); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
657 } |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
658 } |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
659 |
| 57 | 660 /** |
| 661 * Update the target of the relation. | |
| 662 * | |
| 663 * @author jurzua | |
| 664 * | |
| 665 * @param session | |
| 666 * @param rel | |
| 667 * @throws Exception | |
| 668 */ | |
| 669 private void refreshRelationTarget(Session session, Relation rel) throws Exception { | |
|
80
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
670 if(rel.getTargetId() == null){ |
|
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
671 throw new Exception("Refreshing entity " + rel.getSourceId() + ", the system found a relation with empty target."); |
|
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
672 } |
| 74 | 673 StringBuilder sb = new StringBuilder("refreshTarget of " + rel.toString() + " to "+rel.printTarget()); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
674 List<Entity> entityList = this.getEntities(session, rel.getTargetId(), Node.SYS_STATUS_CURRENT_VERSION, rel.getType(), null, false); |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
675 if(entityList.isEmpty()){ |
|
80
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
676 throw new Exception("Refreshing entity " + rel.getSourceId() + ", the system found a relation without target. " + rel.toString()); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
677 } |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
678 Entity target = entityList.get(0); |
|
80
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
679 long targetModifOld = rel.getTargetModif(); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
680 rel.setTarget(target); |
| 74 | 681 sb.append(" new "+rel.printTarget()); |
| 57 | 682 // the refresh is logged only if we detect a difference between the relation and the target entity. |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
683 if(targetModifOld != rel.getTargetModif()){ |
| 57 | 684 logger.warn(sb.toString()); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
685 } |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
686 } |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
687 |
| 57 | 688 /** |
| 689 * Update the source of the relation. | |
| 690 * | |
| 691 * @author jurzua | |
| 692 * | |
| 693 * @param session | |
| 694 * @param rel | |
| 695 * @throws Exception | |
| 696 */ | |
| 697 private void refreshRelationSource(Session session, Relation rel) throws Exception { | |
|
80
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
698 if(rel.getSourceId() == null){ |
|
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
699 throw new Exception("Refreshing entity " + rel.getTargetId() + ", the system found a relation with emtpy source."); |
|
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
700 } |
| 74 | 701 StringBuilder sb = new StringBuilder("refreshSource of " + rel.toString() + " to "+rel.printSource()); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
702 List<Entity> entityList = this.getEntities(session, rel.getSourceId(), Node.SYS_STATUS_CURRENT_VERSION, rel.getType(), null, false); |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
703 if(entityList.isEmpty()){ |
|
80
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
704 throw new Exception("Refreshing entity " + rel.getTargetId() + ", the system found a relation without source. " + rel.toString()); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
705 } |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
706 Entity source = entityList.get(0); |
|
80
4c9ceb28cfd0
fix some NPEs with refreshRelations
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
75
diff
changeset
|
707 long sourceModifOld = rel.getSourceModif(); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
708 rel.setSource(source); |
| 74 | 709 sb.append(" new "+rel.printSource()); |
| 57 | 710 // the refresh is logged only if we detect a difference between the relation and the source entity. |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
711 if(sourceModifOld != rel.getSourceModif()){ |
| 57 | 712 logger.warn(sb.toString()); |
|
56
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
713 } |
|
467843399e70
Fixs for lost relation, when editing clone entities in parallel
jurzua <jjjurzua@hotmail.com>
parents:
51
diff
changeset
|
714 } |
| 1 | 715 |
| 716 /** | |
| 39 | 717 * Update the entity to prepare it for persistence using the session. |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
718 * |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
719 * If the ID is null new IDs will be assigned for the entity and its |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
720 * attributes and relations. |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
721 * The modification time is propagated from the entity to its |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
722 * attributes and relations. |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
723 * Own-values are normalized. |
| 1 | 724 * |
| 725 * @param session | |
| 726 * @param entity | |
| 727 * @return | |
| 728 */ | |
| 36 | 729 private Entity prepareEntity(Session session, Entity entity, Sequence idSequence) { |
| 1 | 730 |
| 36 | 731 /* |
| 732 * update id | |
| 733 */ | |
| 1 | 734 if (entity.getId() == null) { |
| 735 if (idSequence == null) | |
| 736 entity.setId(this.generateId(session)); | |
| 737 else | |
| 738 entity.setId(idSequence.generateId()); | |
| 739 } | |
| 740 | |
| 36 | 741 /* |
| 742 * update attributes | |
| 743 */ | |
| 1 | 744 for (Attribute att : entity.getAttributes()) { |
| 745 if (att.getId() == null) { | |
| 746 if (idSequence == null) | |
| 747 att.setId(this.generateId(session)); | |
| 748 else | |
| 749 att.setId(idSequence.generateId()); | |
| 750 } | |
| 751 att.setSourceId(entity.getId()); | |
| 752 att.setSourceModif(entity.getModificationTime()); | |
| 753 att.setSourceObjectClass(entity.getObjectClass()); | |
| 754 att.autoNormalize(); | |
| 755 } | |
| 756 | |
| 36 | 757 /* |
| 758 * update source relations | |
| 759 */ | |
| 1 | 760 for (Relation rel : entity.getSourceRelations()) { |
| 761 if (rel.getId() == null) { | |
| 762 if (idSequence == null) | |
| 763 rel.setId(this.generateId(session)); | |
| 764 else | |
| 765 rel.setId(idSequence.generateId()); | |
| 766 } | |
| 767 rel.setSourceId(entity.getId()); | |
| 768 rel.setSourceModif(entity.getModificationTime()); | |
| 769 rel.setSourceObjectClass(entity.getObjectClass()); | |
| 36 | 770 // TODO: update relation target too? |
| 771 rel.autoNormalize(); // TODO: normalize relation? | |
| 772 if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)) { | |
| 1 | 773 rel.setObjectClass(rel.getOwnValue()); |
| 36 | 774 } else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)) { |
| 1 | 775 rel.setObjectClass(Node.TYPE_TBOX); |
| 776 } | |
| 777 | |
| 36 | 778 /* |
| 779 * update attributes for relations | |
| 780 */ | |
| 1 | 781 for (Attribute att : rel.getAttributes()) { |
| 782 if (att.getId() == null) { | |
| 783 if (idSequence == null) | |
| 784 att.setId(this.generateId(session)); | |
| 785 else | |
| 786 att.setId(idSequence.generateId()); | |
| 787 } | |
| 788 att.setSourceId(rel.getId()); | |
| 789 att.setSourceModif(rel.getModificationTime()); | |
| 790 att.setSourceObjectClass(rel.getOwnValue()); | |
| 791 att.autoNormalize(); | |
| 792 } | |
| 793 } | |
| 794 | |
| 36 | 795 /* |
| 796 * update target relations | |
| 797 */ | |
| 1 | 798 for (Relation rel : entity.getTargetRelations()) { |
| 799 if (rel.getId() == null) { | |
| 800 if (idSequence == null) | |
| 801 rel.setId(this.generateId(session)); | |
| 802 else | |
| 803 rel.setId(idSequence.generateId()); | |
| 804 } | |
| 805 rel.setTargetId(entity.getId()); | |
| 806 rel.setTargetModif(entity.getModificationTime()); | |
| 807 rel.setTargetObjectClass(entity.getObjectClass()); | |
| 36 | 808 // TODO: update relation source too? |
| 809 rel.autoNormalize(); // TODO: normalize? | |
| 1 | 810 if(StringUtils.equals(entity.getType(), Node.TYPE_ABOX)){ |
| 811 rel.setObjectClass(rel.getOwnValue()); | |
| 812 }else if(StringUtils.equals(entity.getType(), Node.TYPE_TBOX)){ | |
| 813 rel.setObjectClass(Node.TYPE_TBOX); | |
| 814 } | |
| 36 | 815 /* |
| 816 * update relation attributes | |
| 817 */ | |
| 1 | 818 for (Attribute att : rel.getAttributes()) { |
| 819 if (att.getId() == null) { | |
| 820 if (idSequence == null) | |
| 821 att.setId(this.generateId(session)); | |
| 822 else | |
| 823 att.setId(idSequence.generateId()); | |
| 824 } | |
| 825 att.setSourceId(rel.getId()); | |
| 826 att.setSourceModif(rel.getModificationTime()); | |
| 827 att.setSourceObjectClass(rel.getOwnValue()); | |
| 828 att.autoNormalize(); | |
| 829 } | |
| 830 } | |
| 36 | 831 |
| 832 /* | |
| 833 * update views(?) | |
| 834 */ | |
| 1 | 835 for (View view : entity.getViews()) { |
| 836 if (view.getId() == null) { | |
| 837 if (idSequence == null) | |
| 838 view.setId(this.generateId(session)); | |
| 839 else | |
| 840 view.setId(idSequence.generateId()); | |
| 841 } | |
| 842 view.setSourceId(entity.getId()); | |
| 843 view.setSourceModif(entity.getModificationTime()); | |
| 844 view.setSourceObjectClass(entity.getObjectClass()); | |
| 845 view.autoNormalize(); | |
| 846 } | |
| 36 | 847 |
| 1 | 848 return entity; |
| 849 } | |
| 850 | |
| 851 /** | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
852 * Set an entities' system state to PREVIOUS_VERSION and persist it. |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
853 * |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
854 * Also persists the entities' attributes and relations. |
| 1 | 855 * |
| 856 * @param session | |
| 857 * @param entity | |
| 858 */ | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
859 private void persistEntityAsPrevious(Session session, Entity entity) { |
| 1 | 860 entity.setSystemStatus(Node.SYS_STATUS_PREVIOUS_VERSION); |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
861 txLog.debug("* START save previous entity..."); |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
862 txLog.info("save previous entity: "+entity.toEncString()); |
| 1 | 863 session.save(entity); |
| 864 for (Attribute attribute : entity.getAttributes()) { | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
865 txLog.info("save previous entity attribute: "+attribute.toEncString()); |
| 1 | 866 session.save(attribute); |
| 867 } | |
| 868 for (Relation rel : entity.getSourceRelations()) { | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
869 txLog.info("save previous source relation: "+rel.toEncString()); |
| 1 | 870 session.save(rel); |
| 871 for (Attribute att : rel.getAttributes()) { | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
872 txLog.info("save previous source relation attribute: "+att.toEncString()); |
| 1 | 873 session.save(att); |
| 874 } | |
| 875 } | |
| 876 for (Relation rel : entity.getTargetRelations()) { | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
877 txLog.info("save previous target relation: "+rel.toEncString()); |
| 1 | 878 session.save(rel); |
|
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
879 for (Attribute att : rel.getAttributes()) { |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
880 txLog.info("save previous target relation attribute: "+att.toEncString()); |
|
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
881 session.save(att); |
|
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
882 } |
| 1 | 883 } |
| 884 for (View view : entity.getViews()) { | |
| 885 session.save(view); | |
| 886 } | |
| 36 | 887 txLog.debug("* END ...save previous entity"); |
| 1 | 888 } |
| 889 | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
890 /** |
| 39 | 891 * Persist a (current) entity and its attributes and relations using the session. |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
892 * |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
893 * @param session |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
894 * @param entity |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
895 * @throws Exception |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
896 */ |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
897 private void persistEntity(Session session, Entity entity) throws Exception { |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
898 txLog.info("* START save entity..."); |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
899 txLog.info("save entity: "+entity.toEncString()); |
| 1 | 900 session.save(entity); |
| 901 for (Attribute attribute : entity.getAttributes()) { | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
902 txLog.info("save entity attribute: "+attribute.toEncString()); |
| 1 | 903 session.save(attribute); |
| 904 } | |
| 905 for (Relation rel : entity.getSourceRelations()) { | |
| 906 rel.setSource(entity); | |
| 907 if (rel.getSourceId() == null || rel.getTargetId() == null) { | |
| 908 throw new IllegalStateException( | |
| 909 "Either the sourceId or the targetId was not initialized to the source relation: " | |
| 910 + rel.getOwnValue()); | |
| 911 } | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
912 txLog.info("save source relation: "+rel.toEncString()); |
| 1 | 913 session.save(rel); |
| 914 for (Attribute att : rel.getAttributes()) { | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
915 txLog.info("save source relation attribute: "+att.toEncString()); |
| 1 | 916 session.save(att); |
| 917 } | |
| 918 } | |
| 919 for (Relation rel : entity.getTargetRelations()) { | |
| 920 rel.setTarget(entity); | |
| 921 if (rel.getSourceId() == null || rel.getTargetId() == null) { | |
| 922 throw new IllegalStateException( | |
|
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
923 "Either the sourceId or the targetId was not initialized to the target relation: " |
| 1 | 924 + rel.getOwnValue()); |
| 925 } | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
926 txLog.info("save target relation: "+rel.toEncString()); |
| 1 | 927 session.save(rel); |
|
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
928 for (Attribute att : rel.getAttributes()) { |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
929 txLog.info("save target relation attribute: "+att.toEncString()); |
|
25
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
930 session.save(att); |
|
c23ae718fdd3
clean up and more logging when saving attributes.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
1
diff
changeset
|
931 } |
| 1 | 932 } |
| 933 | |
| 934 // Call of ownValue Generator. | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
935 if (!isImportMode()) { |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
936 String ownValue = this.getOwnValueGenerator().generateOwnValue(entity, session); |
| 1 | 937 if (StringUtils.isNotEmpty(ownValue)) { |
| 938 entity.setOwnValue(ownValue); | |
| 939 entity.autoNormalize(); | |
| 36 | 940 txLog.info("save entity (new ov): "+entity.toEncString()); |
| 1 | 941 session.save(entity); |
| 942 } | |
| 943 } | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
944 txLog.debug("* ...END save entity"); |
| 1 | 945 } |
| 946 | |
| 947 public OwnValueGenerator getOwnValueGenerator() { | |
| 948 return ownValueGenerator; | |
| 949 } | |
| 950 | |
| 951 public void setOwnValueGenerator(OwnValueGenerator ownValueGenerator) { | |
| 952 this.ownValueGenerator = ownValueGenerator; | |
| 953 } | |
| 954 | |
| 955 /** | |
| 39 | 956 * Returns entities with their content from the database using the session. |
| 36 | 957 * |
| 958 * If a parameter is null then the condition is omitted from the query | |
| 959 * returning all entities matching the remaining conditions. | |
| 1 | 960 * |
| 961 * @param session | |
| 962 * @param id | |
| 963 * @param systemStatus | |
| 964 * @param type | |
| 965 * @param ownValue | |
| 36 | 966 * @param considerTimeModif |
| 1 | 967 * @return |
| 968 */ | |
| 969 private List<Entity> getEntities(Session session, Long id, | |
| 970 String systemStatus, String type, String ownValue, boolean considerTimeModif) { | |
| 971 | |
| 39 | 972 if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) |
| 973 || systemStatus.equals(Node.SYS_STATUS_CURRENT_VERSION))) { | |
| 974 throw new IllegalArgumentException("Invalid input systemStatus: " + systemStatus); | |
| 975 } | |
| 1 | 976 |
| 977 if (StringUtils.isNotEmpty(type) | |
| 978 && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { | |
| 979 throw new IllegalArgumentException("Invalid input type: " + type); | |
| 980 } | |
| 981 | |
| 982 List<Entity> entities; | |
| 983 | |
| 984 String hqlEntities = "from Entity where systemStatus = :systemStatus"; | |
| 985 | |
| 986 if (StringUtils.isNotEmpty(type)) { | |
| 987 hqlEntities += " AND type = :type"; | |
| 988 } | |
| 989 | |
| 990 if (id != null) { | |
| 991 hqlEntities += " AND id = :id"; | |
| 992 } | |
| 993 | |
| 994 if (StringUtils.isNotEmpty(ownValue)) { | |
| 995 hqlEntities += " AND ownValue = :ownValue"; | |
| 996 } | |
| 997 | |
| 998 Query queryEntities = session.createQuery(hqlEntities); | |
| 999 queryEntities.setString("systemStatus", systemStatus); | |
| 1000 | |
| 1001 if (StringUtils.isNotEmpty(type)) { | |
| 1002 queryEntities.setString("type", type); | |
| 1003 } | |
| 1004 | |
| 1005 if (StringUtils.isNotEmpty(ownValue)) { | |
| 1006 queryEntities.setString("ownValue", ownValue); | |
| 1007 } | |
| 1008 | |
| 1009 if (id != null) { | |
| 1010 queryEntities.setLong("id", id); | |
| 1011 } | |
| 1012 entities = queryEntities.list(); | |
| 1013 | |
| 1014 for (Entity entity : entities) { | |
| 1015 entity.setLightweight(true); | |
| 1016 entity = this.getEntityContent(session, entity, considerTimeModif); | |
| 1017 } | |
| 1018 | |
| 1019 return entities; | |
| 1020 } | |
| 1021 | |
| 1022 /** | |
| 39 | 1023 * Returns entities with their content from the database. |
| 1 | 1024 * |
| 36 | 1025 * If a parameter is null then the condition is omitted from the query |
| 1026 * returning all entities matching the remaining conditions. | |
| 1027 * | |
| 1 | 1028 * @param id |
| 1029 * @param systemStatus | |
| 1030 * @param type | |
| 1031 * @param ownValue | |
| 1032 * @return | |
| 1033 */ | |
| 36 | 1034 public List<Entity> getEntities(Long id, String systemStatus, String type, String ownValue) { |
| 1035 // logger.debug("GET ENTITIES Entities [id=" + id + ", type=" + type + ", ownValue=" + ownValue + "]"); | |
| 1 | 1036 |
| 1037 if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) || systemStatus | |
| 1038 .equals(Node.SYS_STATUS_CURRENT_VERSION))) { | |
| 1039 throw new IllegalArgumentException("Invalid input systemStatus: " | |
| 1040 + systemStatus); | |
| 1041 } | |
| 1042 | |
| 1043 if (StringUtils.isNotEmpty(type) | |
| 1044 && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { | |
| 1045 throw new IllegalArgumentException("Invalid input type: " + type); | |
| 1046 } | |
| 1047 | |
| 1048 List<Entity> entities = null; | |
| 1049 Session session = null; | |
| 1050 try { | |
| 36 | 1051 //long start = System.currentTimeMillis(); |
| 1 | 1052 session = HibernateUtil.getSessionFactory().getCurrentSession(); |
| 1053 session.getTransaction().begin(); | |
| 1054 | |
| 36 | 1055 entities = this.getEntities(session, id, systemStatus, type, ownValue, true); |
| 1 | 1056 |
| 36 | 1057 /* long dif = System.currentTimeMillis() - start; |
| 1 | 1058 String s = "Found=\n"; |
| 1059 for (Entity e : entities) { | |
| 1060 s += e.toString() + "\n"; | |
| 1061 } | |
| 1062 s += "time used= " + dif + "[ms]\n\n"; | |
| 36 | 1063 logger.debug(s); */ |
| 1 | 1064 |
| 1065 } catch (Exception e) { | |
| 1066 logger.error(e.getMessage(), e); | |
| 1067 // e.printStackTrace(); | |
| 1068 } finally { | |
| 1069 session.getTransaction().commit(); | |
| 1070 } | |
| 1071 return entities; | |
| 1072 } | |
| 1073 | |
| 1074 private String whereUpdate(String where) { | |
| 1075 if (StringUtils.isEmpty(where)) | |
| 1076 where = " where "; | |
| 1077 else | |
| 1078 where += " AND "; | |
| 1079 return where; | |
| 1080 } | |
| 1081 | |
| 36 | 1082 /** |
| 1083 * Returns a list of Nodes. | |
| 1084 * | |
| 1085 * If a parameter is null then the condition is omitted from the query | |
| 1086 * returning all entities matching the remaining conditions. | |
| 1087 * | |
| 1088 * @param session | |
| 1089 * @param id | |
| 1090 * @param systemStatus | |
| 1091 * @param sourceId | |
| 1092 * @param srcModif | |
| 1093 * @param targetId | |
| 1094 * @param tarModif | |
| 1095 * @return | |
| 1096 */ | |
| 1 | 1097 public List<Node> getNodes(Session session, Long id, String systemStatus, |
| 1098 Long sourceId, Long srcModif, Long targetId, Long tarModif) { | |
| 1099 List<Node> nodes = null; | |
| 1100 | |
| 36 | 1101 /* |
| 1102 * create HQL query string | |
| 1103 */ | |
| 1 | 1104 String from = "from Node "; |
| 1105 String where = ""; | |
| 1106 | |
| 1107 if (id != null) { | |
| 1108 where = this.whereUpdate(where); | |
| 1109 where += " id = :id "; | |
| 36 | 1110 } |
| 1 | 1111 |
| 1112 if (StringUtils.isNotEmpty(systemStatus)) { | |
| 1113 where = this.whereUpdate(where); | |
| 1114 where += " systemStatus = :systemStatus "; | |
| 1115 } | |
| 36 | 1116 |
| 1 | 1117 if (sourceId != null) { |
| 1118 where = this.whereUpdate(where); | |
| 1119 where += " sourceId = :sourceId "; | |
| 1120 } | |
| 36 | 1121 |
| 1 | 1122 if (srcModif != null) { |
| 1123 where = this.whereUpdate(where); | |
| 1124 where += " sourceModif = :sourceModif "; | |
| 1125 } | |
| 1126 | |
| 1127 if (targetId != null) { | |
| 1128 where = this.whereUpdate(where); | |
| 1129 where += " targetId = :targetId "; | |
| 1130 } | |
| 36 | 1131 |
| 1 | 1132 if (tarModif != null) { |
| 1133 where = this.whereUpdate(where); | |
| 1134 where += " targetModif = :targetModif "; | |
| 1135 } | |
| 1136 | |
| 1137 String hql = from + where; | |
| 36 | 1138 // create query object |
| 1 | 1139 Query query = session.createQuery(hql); |
| 1140 | |
| 36 | 1141 /* |
| 1142 * add query parameters | |
| 1143 */ | |
| 1 | 1144 if (id != null) |
| 1145 query.setLong("id", id); | |
| 36 | 1146 |
| 1 | 1147 if (StringUtils.isNotEmpty(systemStatus)) |
| 1148 query.setString("systemStatus", systemStatus); | |
| 1149 | |
| 1150 if (sourceId != null) | |
| 1151 query.setLong("sourceId", sourceId); | |
| 36 | 1152 |
| 1 | 1153 if (srcModif != null) |
| 1154 query.setLong("sourceModif", srcModif); | |
| 1155 | |
| 1156 if (targetId != null) | |
| 1157 query.setLong("targetId", targetId); | |
| 36 | 1158 |
| 1 | 1159 if (tarModif != null) |
| 1160 query.setLong("targetModif", tarModif); | |
| 1161 | |
| 36 | 1162 /* |
| 1163 * run the query | |
| 1164 */ | |
| 1 | 1165 nodes = query.list(); |
| 1166 | |
| 1167 return nodes; | |
| 1168 } | |
| 1169 | |
| 36 | 1170 /** |
| 1171 * Populate a lightweight entity with attributes and relations. | |
| 1172 * | |
| 1173 * @param session | |
| 1174 * @param entity | |
| 1175 * @param considerTimeModif | |
| 1176 * @return | |
| 1177 */ | |
| 1 | 1178 private Entity getEntityContent(Session session, Entity entity, boolean considerTimeModif) { |
| 1179 if (entity != null && entity.isLightweight()) { | |
| 1180 entity.setLightweight(false); | |
| 1181 entity.setAttributes(new ArrayList<Attribute>()); | |
| 1182 entity.setSourceRelations(new ArrayList<Relation>()); | |
| 1183 entity.setTargetRelations(new ArrayList<Relation>()); | |
| 1184 | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1185 /* |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1186 * Danger: getNodes finds all attributes in the db and tries to attach them to |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1187 * this entity if id=null! |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1188 */ |
|
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1189 if (entity.getId() == null) { |
| 27 | 1190 logger.error("Entity with id=null! Abort loading attributes."); |
|
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1191 return entity; |
|
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1192 } |
|
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1193 |
| 36 | 1194 /* |
| 1195 * get all nodes with source_id = entity.id | |
| 1196 * i.e. Attributes and SourceRelations. | |
| 1197 */ | |
|
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1198 List<Node> nodes = null; |
| 36 | 1199 if (considerTimeModif) { |
| 1 | 1200 nodes = this.getNodes(session, null, |
| 1201 entity.getSystemStatus(), entity.getId(), | |
| 1202 entity.getModificationTime(), null, null); | |
| 36 | 1203 } else { |
| 1 | 1204 nodes = this.getNodes(session, null, |
| 1205 entity.getSystemStatus(), entity.getId(), | |
| 1206 null, null, null); | |
| 1207 } | |
| 1208 | |
| 36 | 1209 /* |
| 1210 * add Attributes and SourceRelations | |
| 1211 */ | |
| 1 | 1212 for (Node node : nodes) { |
| 1213 if (node instanceof Attribute) { | |
| 1214 entity.addAttribute((Attribute) node); | |
| 36 | 1215 |
| 1 | 1216 } else if (node instanceof Relation) { |
| 1217 Relation rel = (Relation) node; | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1218 /* |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1219 * Danger: getNodes finds all attributes in the db and tries to attach them to |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1220 * this relation if id=null! |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1221 */ |
|
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1222 if (rel.getId() == null) { |
|
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1223 logger.error("Relation with id=null! Abort loading attributes."); |
|
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1224 continue; |
|
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1225 } |
| 36 | 1226 /* |
| 1227 * get all nodes with source_id = rel.id | |
| 1228 * i.e. relation Attributes | |
| 1229 */ | |
| 1 | 1230 List<Node> attrs = this.getNodes(session, null, |
| 1231 rel.getSystemStatus(), rel.getId(), | |
| 1232 rel.getModificationTime(), null, null); | |
| 1233 for (Node attNode : attrs) { | |
| 1234 if (attNode instanceof Attribute) { | |
| 1235 rel.addAttribute((Attribute) attNode); | |
| 1236 } | |
| 1237 } | |
| 36 | 1238 // TODO: ??? |
| 1239 if (considerTimeModif) { | |
| 1 | 1240 entity.addSourceRelation(rel); |
| 36 | 1241 } else { |
| 1 | 1242 entity.getSourceRelations().add(rel); |
| 1243 } | |
| 36 | 1244 |
| 1 | 1245 } else if (node instanceof View) { |
| 1246 entity.getViews().add((View) node); | |
| 36 | 1247 |
| 1 | 1248 } else { |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1249 throw new IllegalArgumentException("Invalid node found: " + node); |
| 1 | 1250 } |
| 1251 } | |
| 1252 | |
| 36 | 1253 /* |
| 1254 * get all nodes with target_id = entity.id | |
| 1255 * i.e. TargetRelations | |
| 1256 */ | |
| 1 | 1257 List<Node> tarRels = null; |
| 1258 | |
| 36 | 1259 if (considerTimeModif) { |
| 1 | 1260 tarRels = this.getNodes(session, null, |
| 1261 entity.getSystemStatus(), null, null, entity.getId(), | |
| 1262 entity.getModificationTime()); | |
| 36 | 1263 } else { |
| 1 | 1264 tarRels = this.getNodes(session, null, |
| 1265 entity.getSystemStatus(), null, null, entity.getId(), | |
| 1266 null); | |
| 1267 } | |
| 1268 | |
| 36 | 1269 /* |
| 1270 * add TargetRelations | |
| 1271 */ | |
| 1 | 1272 for (Node node : tarRels) { |
| 1273 if (node instanceof Relation) { | |
| 1274 Relation rel = (Relation) node; | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1275 /* |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1276 * Danger: getNodes finds all attributes in the db and tries to attach them to |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1277 * this relation if id=null! |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1278 */ |
|
26
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1279 if (rel.getId() == null) { |
|
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1280 logger.error("Relation with id=null! Abort loading attributes."); |
|
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1281 continue; |
|
5e24413d355b
Fixed bug that deleted all attributes in the system if a relation had id=null :-(
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
25
diff
changeset
|
1282 } |
| 36 | 1283 /* |
| 1284 * get all nodes with source_id = rel.id | |
| 1285 * i.e. relation Attributes | |
| 1286 */ | |
| 1 | 1287 List<Node> attrs = this.getNodes(session, null, |
| 1288 rel.getSystemStatus(), rel.getId(), | |
| 1289 rel.getModificationTime(), null, null); | |
| 1290 for (Node attNode : attrs) { | |
| 1291 if (attNode instanceof Attribute) { | |
| 1292 rel.addAttribute((Attribute) attNode); | |
| 1293 } | |
| 1294 } | |
| 36 | 1295 // TODO: ??? |
| 1296 if (considerTimeModif) { | |
| 1 | 1297 entity.addTargetRelation(rel); |
| 36 | 1298 } else { |
| 1 | 1299 entity.getTargetRelations().add(rel); |
| 1300 } | |
| 36 | 1301 |
| 1 | 1302 } else { |
| 36 | 1303 throw new IllegalArgumentException("Invalid node found: " + node); |
| 1 | 1304 } |
| 1305 } | |
| 1306 } | |
| 1307 return entity; | |
| 1308 } | |
| 1309 | |
| 36 | 1310 /** |
| 1311 * Populate a lightweight entity with attributes and relations. | |
| 1312 * | |
| 1313 * @param entity | |
| 1314 * @return | |
| 1315 */ | |
| 1316 public Entity getEntityContent(Entity entity) { | |
| 1317 if (entity != null && entity.isLightweight()) { | |
| 1318 Session session = null; | |
| 1319 try { | |
| 1320 /* logger.debug("GET ENTITY CONTENT [objClass=" + entity.getObjectClass() + ", id=" + entity.getId() | |
| 1321 + ", ownValue=" + entity.getOwnValue() + "]"); */ | |
| 1322 //long start = System.currentTimeMillis(); | |
| 1323 | |
| 1324 // start transaction | |
| 1325 session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 1326 session.getTransaction().begin(); | |
| 1327 // get entity content | |
| 1328 entity = this.getEntityContent(session, entity, true); | |
| 1329 | |
| 1330 /* long diff = System.currentTimeMillis() - start; | |
| 1331 logger.debug("Time to get content=" + diff + "(s)"); */ | |
| 1332 } catch (Exception e) { | |
| 1333 logger.error(e.getMessage(), e); | |
| 1334 } finally { | |
| 1335 session.getTransaction().commit(); | |
| 1336 } | |
| 1337 } | |
| 1338 return entity; | |
| 1339 } | |
| 1 | 1340 |
| 36 | 1341 public List<String> getObjecClassSuggestion(String objectClass, Class nodeClass, int maxResults) { |
| 1 | 1342 objectClass += "%"; |
| 1343 try { | |
| 1344 | |
| 1345 List<String> suggestions = new ArrayList<String>(); | |
| 1346 Session session = HibernateUtil.getSessionFactory() | |
| 1347 .getCurrentSession(); | |
| 1348 session.getTransaction().begin(); | |
| 1349 String hql = "from "; | |
| 1350 if (nodeClass.equals(Entity.class)) { | |
| 1351 hql += "Entity "; | |
| 1352 } | |
| 1353 if (nodeClass.equals(Relation.class)) { | |
| 1354 hql += "Relation "; | |
| 1355 } | |
| 1356 if (nodeClass.equals(Attribute.class)) { | |
| 1357 hql += "Attribute "; | |
| 1358 } | |
| 1359 if (nodeClass.equals(Node.class)) { | |
| 1360 hql += "Node "; | |
| 1361 } | |
| 1362 if (nodeClass.equals(View.class)) { | |
| 1363 hql += "View "; | |
| 1364 } | |
| 1365 hql += " where objectClass like :objectClass " | |
| 1366 + "group by objectClass"; | |
| 1367 | |
| 1368 Query query = session.createQuery(hql); | |
| 1369 query.setString("objectClass", objectClass); | |
| 1370 query.setMaxResults(maxResults); | |
| 1371 List<Node> list = query.list(); | |
| 1372 for (Node node : list) { | |
| 1373 suggestions.add(node.getObjectClass()); | |
| 1374 } | |
| 1375 return suggestions; | |
| 1376 | |
| 1377 } catch (Exception e) { | |
| 1378 logger.error(e.getMessage(), e); | |
| 1379 // e.printStackTrace(); | |
| 1380 } | |
| 1381 return null; | |
| 1382 } | |
| 1383 | |
| 36 | 1384 public List<String> getOwnValueSuggestion(String ownValue, Class nodeClass, int maxResults) { |
| 1 | 1385 ownValue += "%"; |
| 1386 try { | |
| 1387 | |
| 1388 List<String> suggestions = new ArrayList<String>(); | |
| 1389 Session session = HibernateUtil.getSessionFactory() | |
| 1390 .getCurrentSession(); | |
| 1391 session.getTransaction().begin(); | |
| 1392 String hql = "from "; | |
| 1393 if (nodeClass.equals(Entity.class)) { | |
| 1394 hql += "Entity "; | |
| 1395 } | |
| 1396 if (nodeClass.equals(Relation.class)) { | |
| 1397 hql += "Relation "; | |
| 1398 } | |
| 1399 if (nodeClass.equals(Attribute.class)) { | |
| 1400 hql += "Attribute "; | |
| 1401 } | |
| 1402 if (nodeClass.equals(Node.class)) { | |
| 1403 hql += "Node "; | |
| 1404 } | |
| 1405 if (nodeClass.equals(View.class)) { | |
| 1406 hql += "View "; | |
| 1407 } | |
| 1408 hql += " where ownValue like :ownValue " + "group by ownValue"; | |
| 1409 | |
| 1410 Query query = session.createQuery(hql); | |
| 1411 query.setString("ownValue", ownValue); | |
| 1412 query.setMaxResults(maxResults); | |
| 1413 List<Node> list = query.list(); | |
| 1414 for (Node node : list) { | |
| 1415 suggestions.add(node.getOwnValue()); | |
| 1416 } | |
| 1417 return suggestions; | |
| 1418 | |
| 1419 } catch (Exception e) { | |
| 1420 logger.error(e.getMessage(), e); | |
| 1421 // e.printStackTrace(); | |
| 1422 } | |
| 1423 return null; | |
| 1424 } | |
| 1425 | |
| 1426 public Entity loadEntitiesForTargetRelation(Entity entity) { | |
| 1427 try { | |
| 1428 // where parent_id = :parent_id AND type = :type | |
| 1429 Session session = HibernateUtil.getSessionFactory() | |
| 1430 .getCurrentSession(); | |
| 1431 session.getTransaction().begin(); | |
| 1432 | |
| 1433 for (Relation rel : entity.getTargetRelations()) { | |
| 1434 List<Entity> entities = this.getEntities(session, | |
| 1435 rel.getSourceId(), Node.SYS_STATUS_CURRENT_VERSION, | |
| 1436 null, null, true); | |
| 1437 if (entities.size() > 0) { | |
| 1438 Entity source = entities.get(0); | |
| 1439 rel.setSource(source); | |
| 1440 } | |
| 1441 } | |
| 1442 | |
| 1443 session.getTransaction().commit(); | |
| 1444 return entity; | |
| 1445 } catch (Exception e) { | |
| 1446 logger.error(e.getMessage(), e); | |
| 1447 // e.printStackTrace(); | |
| 1448 } | |
| 1449 return entity; | |
| 1450 } | |
| 1451 | |
| 1452 /** | |
| 39 | 1453 * Return all (lightweight) entities matching the query. |
| 1454 * | |
| 1455 * Any parameter that is null is omitted as a query condition matching | |
| 1456 * all values in this parameter. | |
| 1457 * | |
| 1458 * Own value is searched as substring if ownValueSubString=true. | |
| 1459 * | |
| 1460 * @param session | |
| 1 | 1461 * @param systemStatus |
| 1462 * @param id | |
| 1463 * @param type | |
| 1464 * @param objectClass | |
| 1465 * @param ownValue | |
| 39 | 1466 * @param ownValueSubString |
| 1 | 1467 * @param maxResult |
| 1468 * @return | |
| 1469 */ | |
| 39 | 1470 protected List<Entity> getLightweightEntities(Session session, String systemStatus, Long id, String type, |
| 1471 String objectClass, String ownValue, boolean ownValueSubString, int maxResult) { | |
| 1472 | |
| 1473 /* | |
| 1474 * build query string | |
| 1475 */ | |
| 1476 String hqlEntities = "from Entity where "; | |
| 1477 if (StringUtils.isNotEmpty(ownValue)) { | |
| 1478 if (ownValueSubString) | |
| 1479 hqlEntities += "ownValue like :ownValue AND "; | |
| 1480 else | |
| 1481 hqlEntities += "ownValue = :ownValue AND "; | |
| 1482 } | |
| 1483 if (id != null) { | |
| 1484 hqlEntities += "id = :id AND "; | |
| 1485 } | |
| 1486 if (StringUtils.isNotEmpty(objectClass)) { | |
| 1487 hqlEntities += "objectClass = :objectClass AND "; | |
| 1488 } | |
| 1489 if (StringUtils.isNotEmpty(type)) { | |
| 1490 hqlEntities += "type = :type AND "; | |
| 1491 } | |
| 1 | 1492 |
| 39 | 1493 hqlEntities += "systemStatus = :systemStatus "; |
| 1494 | |
| 1495 /* | |
| 1496 * create query object | |
| 1497 */ | |
| 1498 Query queryEntities = session.createQuery(hqlEntities); | |
| 1499 | |
| 1500 /* | |
| 1501 * add query parameters | |
| 1502 */ | |
| 1503 queryEntities.setString("systemStatus", systemStatus); | |
| 1504 if (StringUtils.isNotEmpty(ownValue)) { | |
| 1505 if (ownValueSubString) { | |
| 1506 // substring search | |
| 1507 queryEntities.setString("ownValue", "%" + ownValue + "%"); | |
| 1508 //logger.debug("ownValue=%" + ownValue + "%"); | |
| 1509 } else { | |
| 1510 queryEntities.setString("ownValue", ownValue); | |
| 1511 //logger.debug("ownValue=" + ownValue); | |
| 1512 } | |
| 1513 } | |
| 1514 if (maxResult > 0) { | |
| 1515 queryEntities.setMaxResults(maxResult); | |
| 1516 } | |
| 1 | 1517 |
| 39 | 1518 if (StringUtils.isNotEmpty(type)) { |
| 1519 queryEntities.setString("type", type); | |
| 1520 } | |
| 1521 if (id != null) { | |
| 1522 queryEntities.setLong("id", id); | |
| 1523 } | |
| 1524 if (StringUtils.isNotEmpty(objectClass)) { | |
| 1525 queryEntities.setString("objectClass", objectClass); | |
| 1526 } | |
| 1527 | |
| 1528 /* | |
| 1529 * run query and return results | |
| 1530 */ | |
| 1531 List<Entity> entities = queryEntities.list(); | |
| 1 | 1532 |
| 39 | 1533 for (Entity ent : entities) { |
| 1534 ent.setLightweight(true); | |
| 1535 } | |
| 1536 | |
| 1537 return entities; | |
| 1538 } | |
| 1 | 1539 |
| 39 | 1540 /** |
| 1541 * Return all (lightweight) entities matching the query. | |
| 1542 * | |
| 1543 * Any parameter that is null is omitted as a query condition matching | |
| 1544 * all values in this parameter. | |
| 1545 * | |
| 1546 * Own value is searched as substring if ownValueSubString=true. | |
| 1547 * | |
| 1548 * @param systemStatus | |
| 1549 * @param id | |
| 1550 * @param type | |
| 1551 * @param objectClass | |
| 1552 * @param ownValue | |
| 1553 * @param ownValueSubString | |
| 1554 * @param maxResult | |
| 1555 * @return | |
| 1556 */ | |
| 1557 public List<Entity> getLightweightEntities(String systemStatus, Long id, String type, String objectClass, | |
| 1558 String ownValue, boolean ownValueSubString, int maxResult) { | |
| 62 | 1559 //logger.debug("GET LW ENTITIES [type=" + type + " id=" + id + ", objectClass=" + objectClass + ", ownValue=" |
| 1560 // + ownValue + "]"); | |
| 1 | 1561 |
| 39 | 1562 List<Entity> entities = new ArrayList<Entity>(); |
| 1563 /* | |
| 1564 * check inputs | |
| 1565 */ | |
| 1566 if (!(systemStatus.equals(Node.SYS_STATUS_PREVIOUS_VERSION) | |
| 1567 || systemStatus.equals(Node.SYS_STATUS_CURRENT_VERSION))) { | |
| 1568 throw new IllegalArgumentException("Invalid input systemStatus: " + systemStatus); | |
| 1569 } | |
| 1570 if (StringUtils.isNotEmpty(type) && !(type.equals(Node.TYPE_ABOX) || type.equals(Node.TYPE_TBOX))) { | |
| 1571 throw new IllegalArgumentException("Invalid input type: " + type); | |
| 1572 } | |
| 1573 | |
| 1574 /* | |
| 1575 * call getLightweightEntities with transaction | |
| 1576 */ | |
| 1577 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 1578 try { | |
| 1579 session.getTransaction().begin(); | |
| 1580 | |
| 1581 entities = this.getLightweightEntities(session, systemStatus, id, type, objectClass, ownValue, | |
| 1582 ownValueSubString, maxResult); | |
| 1583 | |
| 1584 } catch (Exception e) { | |
| 1585 logger.error(e); | |
| 1586 } finally { | |
| 1587 session.getTransaction().commit(); | |
| 1588 } | |
| 1589 return entities; | |
| 1590 } | |
| 1 | 1591 |
| 1592 private Sequence getIdSequence(Session session) { | |
| 1593 Sequence sequence = null; | |
| 1594 String hqlJoin = "from Sequence where name = :name"; | |
| 1595 Query query = session.createQuery(hqlJoin); | |
| 1596 query.setString("name", NODE_SEQUENCE); | |
| 1597 List<Sequence> sequences = query.list(); | |
| 1598 if (sequences.size() > 0) | |
| 1599 sequence = sequences.get(0); | |
| 1600 if (sequence == null) { | |
| 1601 sequence = new Sequence(NODE_SEQUENCE, new Long(0)); | |
| 1602 } | |
| 1603 return sequence; | |
| 1604 } | |
| 1605 | |
| 1606 protected Long generateId(Session session) { | |
| 1607 Long id = null; | |
| 1608 Sequence sequence = null; | |
| 1609 String hqlJoin = "from Sequence where name = :name"; | |
| 1610 Query query = session.createQuery(hqlJoin); | |
| 1611 query.setString("name", NODE_SEQUENCE); | |
| 1612 List<Sequence> sequences = query.list(); | |
| 1613 if (sequences.size() > 0) { | |
| 1614 sequence = sequences.get(0); | |
| 1615 } else { | |
| 1616 sequence = new Sequence(NODE_SEQUENCE, new Long(0)); | |
| 1617 } | |
| 1618 id = sequence.generateId(); | |
| 1619 session.save(sequence); | |
| 1620 return id; | |
| 1621 } | |
| 1622 | |
| 1623 public ConfigurationService getConfigurationService() { | |
| 1624 return configurationService; | |
| 1625 } | |
| 1626 | |
| 1627 public void setConfigurationService( | |
| 1628 ConfigurationService configurationService) { | |
| 1629 this.configurationService = configurationService; | |
| 1630 } | |
| 1631 | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1632 public boolean isImportMode() { |
|
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1633 return importMode; |
| 1 | 1634 } |
| 1635 | |
| 1636 public void setImportModus(boolean importModus) { | |
|
33
e52f593f9e0d
new transaction logger "openmind.transactionlog" logging entity save actions and their data.
casties
parents:
27
diff
changeset
|
1637 this.importMode = importModus; |
| 1 | 1638 } |
| 1639 | |
| 38 | 1640 /** |
| 1641 * Returns all definition relations. | |
| 1642 * | |
| 1643 * @return | |
| 1644 * @throws Exception | |
| 1645 */ | |
| 1 | 1646 public List<Relation> getDefRelations() { |
| 1647 List<Relation> list = new ArrayList<Relation>(); | |
| 1648 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 1649 try { | |
| 1650 session.getTransaction().begin(); | |
| 1651 | |
| 1652 String hql = "from Relation where systemStatus = :systemStatus and type = :type"; | |
| 1653 Query query = session.createQuery(hql); | |
| 1654 | |
| 1655 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
| 1656 query.setString("type", Node.TYPE_TBOX); | |
| 1657 | |
| 1658 list = query.list(); | |
| 1659 } catch (Exception e) { | |
| 38 | 1660 logger.error(e); |
| 1 | 1661 } finally { |
| 1662 session.getTransaction().commit(); | |
| 1663 } | |
| 1664 return list; | |
| 1665 } | |
| 1666 | |
| 38 | 1667 /** |
| 1668 * Returns all definition attributes. | |
| 1669 * | |
| 1670 * @return | |
| 1671 * @throws Exception | |
| 1672 */ | |
| 1 | 1673 public List<Attribute> getDefAttributes() { |
| 1674 List<Attribute> list = new ArrayList<Attribute>(); | |
| 1675 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 1676 try { | |
| 1677 session.getTransaction().begin(); | |
| 1678 | |
| 1679 String hql = "from Attribute where systemStatus = :systemStatus and type = :type"; | |
| 1680 Query query = session.createQuery(hql); | |
| 1681 | |
| 1682 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
| 1683 query.setString("type", Node.TYPE_TBOX); | |
| 1684 | |
| 1685 list = query.list(); | |
| 1686 | |
| 1687 } catch (Exception e) { | |
| 38 | 1688 logger.error(e); |
| 1 | 1689 } finally { |
| 1690 session.getTransaction().commit(); | |
| 1691 } | |
| 1692 return list; | |
| 1693 } | |
| 1694 | |
| 38 | 1695 /** |
| 1696 * Returns all (lightweight) definition entities. | |
| 1697 * | |
| 1698 * @return | |
| 1699 * @throws Exception | |
| 1700 */ | |
| 1701 public List<Entity> getLWDefinitions() throws Exception { | |
| 1702 List<Entity> list = new ArrayList<Entity>(); | |
| 1703 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); | |
| 1704 try { | |
| 1705 session.getTransaction().begin(); | |
| 1 | 1706 |
| 38 | 1707 String hql = "from Entity where systemStatus = :systemStatus and type = :type"; |
| 1708 Query query = session.createQuery(hql); | |
| 1 | 1709 |
| 38 | 1710 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); |
| 1711 query.setString("type", Node.TYPE_TBOX); | |
| 1 | 1712 |
| 38 | 1713 list = query.list(); |
| 1714 for (Entity def : list) { | |
| 1715 def.setLightweight(true); | |
| 1716 } | |
| 1717 } catch (Exception e) { | |
| 1718 logger.error(e); | |
| 1719 } finally { | |
| 1720 session.getTransaction().commit(); | |
| 1721 } | |
| 1 | 1722 |
| 38 | 1723 return list; |
| 1724 } | |
| 1 | 1725 |
| 1726 // ################################################################# | |
| 1727 // ################################################################# | |
| 1728 // ################################################################# | |
| 1729 | |
| 75 | 1730 /** |
| 1731 * Returns the number of Entities of the given objectClass in the database. | |
| 1732 * | |
| 1733 * Returns the number of all Entities if objectClass==null. | |
| 1734 * | |
| 1735 * @param objectClass | |
| 1736 * @return | |
| 1737 */ | |
| 1 | 1738 public Long getEntityCount(String objectClass) { |
| 1739 Long count = null; | |
| 1740 try { | |
| 75 | 1741 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
| 1 | 1742 session.getTransaction().begin(); |
| 1743 | |
| 1744 String hql = "select count(*) from Entity where "; | |
| 1745 | |
| 1746 if (StringUtils.isNotEmpty(objectClass)) { | |
| 1747 hql += "objectClass = :objectClass AND "; | |
| 1748 } else { | |
| 1749 hql += "objectClass != :objectClass AND "; | |
| 1750 } | |
| 1751 hql += "systemStatus = :systemStatus "; | |
| 1752 | |
| 1753 Query query = session.createQuery(hql); | |
| 1754 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); | |
| 1755 if (StringUtils.isNotEmpty(objectClass)) { | |
| 1756 query.setString("objectClass", objectClass); | |
| 1757 } else { | |
| 1758 query.setString("objectClass", Node.TYPE_TBOX); | |
| 1759 } | |
| 1760 count = (Long) query.uniqueResult(); | |
| 1761 | |
| 1762 session.getTransaction().commit(); | |
| 1763 } catch (Exception e) { | |
| 75 | 1764 logger.error(e); |
| 1 | 1765 } |
| 1766 return count; | |
| 1767 } | |
| 1768 | |
| 1769 /** | |
| 75 | 1770 * Returns all Entities of the given objectClass in the database |
| 1771 * between startRecord and endRecord. | |
| 1 | 1772 * |
| 1773 * @param objectClass | |
| 75 | 1774 * if it is null, all entities are returned (except |
| 1775 * definitions). To get the definitions objectClass should be: | |
| 1 | 1776 * Node.TYPE_TBOX |
| 1777 * @param startRecord | |
| 1778 * @param endRecord | |
| 1779 * @return | |
| 1780 */ | |
| 75 | 1781 public List<Entity> getEntityPage(String objectClass, final int startRecord, final int endRecord) { |
| 1 | 1782 |
| 1783 List<Entity> entities = new ArrayList<Entity>(); | |
| 1784 try { | |
| 75 | 1785 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
| 1 | 1786 session.getTransaction().begin(); |
| 1787 | |
| 1788 String hql = "from Entity where "; | |
| 1789 | |
| 1790 if (StringUtils.isNotEmpty(objectClass)) { | |
| 75 | 1791 // real objectClass |
| 1 | 1792 hql += "objectClass = :objectClass AND "; |
| 1793 } else { | |
| 75 | 1794 // objectClass == null -- match object_class != 'TBox' |
| 1 | 1795 hql += "objectClass != :objectClass AND "; |
| 1796 } | |
| 1797 | |
| 1798 hql += "systemStatus = :systemStatus order by ownValue"; | |
| 1799 | |
| 1800 Query query = session.createQuery(hql); | |
| 75 | 1801 // use Hibernate's result paging |
| 1 | 1802 query.setFirstResult(startRecord); |
| 1803 query.setMaxResults(endRecord); | |
| 75 | 1804 // add query params |
| 1 | 1805 query.setString("systemStatus", Node.SYS_STATUS_CURRENT_VERSION); |
| 1806 if (StringUtils.isNotEmpty(objectClass)) { | |
| 75 | 1807 // real objectClass |
| 1 | 1808 query.setString("objectClass", objectClass); |
| 1809 } else { | |
| 75 | 1810 // objectClass == null -- match object_class != 'TBox' |
| 1 | 1811 query.setString("objectClass", Node.TYPE_TBOX); |
| 1812 } | |
| 75 | 1813 // fetch Entities |
| 1 | 1814 entities = query.list(); |
| 1815 | |
| 1816 session.getTransaction().commit(); | |
| 1817 } catch (Exception e) { | |
| 75 | 1818 logger.error(e); |
| 1 | 1819 } |
| 1820 return entities; | |
| 1821 } | |
| 1822 | |
| 1823 public static void main(String[] args) { | |
| 1824 /* | |
| 1825 * ServiceRegistry sr = new ServiceRegistry(); Long count = | |
| 1826 * sr.getPersistenceService().getEntityCount("CODEX"); | |
| 1827 * logger.info("count codex " + count); | |
| 1828 * | |
| 1829 * List<Entity> list = sr.getPersistenceService().getEntityPage("CODEX", | |
| 1830 * count.intValue() - 100, count.intValue() - 50); for(Entity e : list){ | |
| 1831 * logger.info(e.toString()); } | |
| 1832 */ | |
| 1833 } | |
| 1834 | |
| 1835 } |
