1 23 24 package org.infoglue.cms.controllers.kernel.impl.simple; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import org.apache.log4j.Logger; 34 import org.exolab.castor.jdo.Database; 35 import org.exolab.castor.jdo.OQLQuery; 36 import org.exolab.castor.jdo.PersistenceException; 37 import org.exolab.castor.jdo.QueryResults; 38 import org.infoglue.cms.entities.kernel.BaseEntityVO; 39 import org.infoglue.cms.entities.kernel.IBaseEntity; 40 import org.infoglue.cms.entities.kernel.ValidatableEntityVO; 41 import org.infoglue.cms.entities.management.InterceptionPointVO; 42 import org.infoglue.cms.entities.management.InterceptorVO; 43 import org.infoglue.cms.exception.Bug; 44 import org.infoglue.cms.exception.ConstraintException; 45 import org.infoglue.cms.exception.SystemException; 46 import org.infoglue.cms.security.InfoGluePrincipal; 47 import org.infoglue.cms.security.interceptors.InfoGlueInterceptor; 48 import org.infoglue.cms.util.ConstraintExceptionBuffer; 49 import org.infoglue.cms.util.validators.Constants; 50 import org.infoglue.cms.util.validators.ConstraintRule; 51 import org.infoglue.cms.util.validators.EmailValidator; 52 import org.infoglue.cms.util.validators.StringValidator; 53 54 71 72 public abstract class BaseController 73 { 74 private final static Logger logger = Logger.getLogger(BaseController.class.getName()); 75 76 79 85 96 97 protected void intercept(Map hashMap, String InterceptionPointName, InfoGluePrincipal infogluePrincipal) throws ConstraintException, SystemException, Bug, Exception 98 { 99 InterceptionPointVO interceptionPointVO = InterceptionPointController.getController().getInterceptionPointVOWithName(InterceptionPointName); 100 101 if(interceptionPointVO == null) 102 throw new SystemException("The InterceptionPoint " + InterceptionPointName + " was not found. The system will not work unless you restore it."); 103 104 List interceptors = InterceptionPointController.getController().getInterceptorsVOList(interceptionPointVO.getInterceptionPointId()); 105 Iterator interceptorsIterator = interceptors.iterator(); 106 while(interceptorsIterator.hasNext()) 107 { 108 InterceptorVO interceptorVO = (InterceptorVO)interceptorsIterator.next(); 109 logger.info("Adding interceptorVO:" + interceptorVO.getName()); 110 try 111 { 112 InfoGlueInterceptor infoGlueInterceptor = (InfoGlueInterceptor)Class.forName(interceptorVO.getClassName()).newInstance(); 113 infoGlueInterceptor.intercept(infogluePrincipal, interceptionPointVO, hashMap); 114 } 115 catch(ClassNotFoundException e) 116 { 117 logger.warn("The interceptor " + interceptorVO.getClassName() + "was not found: " + e.getMessage(), e); 118 } 119 } 120 121 } 122 123 124 135 136 protected void intercept(Map hashMap, String InterceptionPointName, InfoGluePrincipal infogluePrincipal, Database db) throws ConstraintException, SystemException, Bug, Exception 137 { 138 InterceptionPointVO interceptionPointVO = InterceptionPointController.getController().getInterceptionPointVOWithName(InterceptionPointName, db); 139 140 if(interceptionPointVO == null) 141 throw new SystemException("The InterceptionPoint " + InterceptionPointName + " was not found. The system will not work unless you restore it."); 142 143 List interceptors = InterceptionPointController.getController().getInterceptorsVOList(interceptionPointVO.getInterceptionPointId(), db); 144 Iterator interceptorsIterator = interceptors.iterator(); 145 while(interceptorsIterator.hasNext()) 146 { 147 InterceptorVO interceptorVO = (InterceptorVO)interceptorsIterator.next(); 148 logger.info("Adding interceptorVO:" + interceptorVO.getName()); 149 try 150 { 151 InfoGlueInterceptor infoGlueInterceptor = (InfoGlueInterceptor)Class.forName(interceptorVO.getClassName()).newInstance(); 152 infoGlueInterceptor.intercept(infogluePrincipal, interceptionPointVO, hashMap, db); 153 } 154 catch(ClassNotFoundException e) 155 { 156 logger.warn("The interceptor " + interceptorVO.getClassName() + "was not found: " + e.getMessage(), e); 157 } 158 } 159 160 } 161 162 163 private static Integer getEntityId(Object entity) throws Bug 164 { 165 Integer entityId = new Integer (-1); 166 167 try 168 { 169 entityId = ((IBaseEntity) entity).getId(); 170 } 171 catch (Exception e) 172 { 173 e.printStackTrace(); 174 throw new Bug("Unable to retrieve object id"); 175 } 176 177 186 return entityId; 187 } 188 189 192 193 protected static Object createEntity(Object entity) throws SystemException, Bug 196 { 197 Database db = CastorDatabaseService.getDatabase(); 198 beginTransaction(db); 199 200 try 201 { 202 db.create(entity); 203 commitTransaction(db); 204 207 } 208 catch(Exception e) 209 { 210 logger.error("An error occurred so we should not complete the transaction:" + e, e); 211 rollbackTransaction(db); 213 throw new SystemException(e.getMessage()); 214 } 215 return entity; 216 } 217 218 219 protected static Object createEntity(Object entity, Database db) throws SystemException, Bug, Exception 221 { 222 db.create(entity); 223 return entity; 224 } 225 245 246 public static void deleteEntity(Class entClass, Integer id) throws Bug, SystemException 248 { 249 Database db = CastorDatabaseService.getDatabase(); 250 Object entity = null; 251 252 beginTransaction(db); 253 254 try 255 { 256 entity = getObjectWithId(entClass, id, db); 257 258 db.remove(entity); 260 commitTransaction(db); 261 } 264 catch(Exception e) 265 { 266 logger.error("An error occurred so we should not complete the transaction:" + e, e); 267 rollbackTransaction(db); 268 throw new SystemException(e.getMessage()); 269 } 270 } 271 272 273 public static void deleteEntity(Class entClass, String id) throws Bug, SystemException 275 { 276 Database db = CastorDatabaseService.getDatabase(); 277 Object entity = null; 278 279 beginTransaction(db); 280 281 try 282 { 283 entity = getObjectWithId(entClass, id, db); 284 285 db.remove(entity); 287 commitTransaction(db); 288 } 291 catch(Exception e) 292 { 293 logger.error("An error occurred so we should not complete the transaction:" + e, e); 294 rollbackTransaction(db); 295 throw new SystemException(e.getMessage()); 296 } 297 } 298 299 300 public static void deleteEntity(Class entClass, String id, Database db) throws Bug, SystemException, Exception 302 { 303 Object entity = getObjectWithId(entClass, id, db); 304 db.remove(entity); 306 } 307 308 309 public static void deleteEntity(Class entClass, Integer id, Database db) throws Bug, SystemException 311 { 312 Object entity = null; 313 314 try 315 { 316 entity = getObjectWithId(entClass, id, db); 317 db.remove(entity); 319 } 320 catch(Exception e) 321 { 322 logger.error("An error occurred so we should not complete the transaction:" + e, e); 323 throw new SystemException(e.getMessage()); 324 } 325 } 326 327 328 public static BaseEntityVO updateEntity(Class arg, BaseEntityVO vo) throws Bug, SystemException 329 { 330 Database db = CastorDatabaseService.getDatabase(); 331 332 IBaseEntity entity = null; 333 334 beginTransaction(db); 335 336 try 337 { 338 entity = (IBaseEntity) getObjectWithId(arg, vo.getId(), db); 339 entity.setVO(vo); 340 341 commitTransaction(db); 342 } 343 catch(Exception e) 344 { 345 logger.error("An error occurred so we should not complete the transaction:" + e, e); 346 rollbackTransaction(db); 347 throw new SystemException(e.getMessage()); 348 } 349 350 return entity.getVO(); 351 } 352 353 354 public static BaseEntityVO updateEntity(Class arg, BaseEntityVO vo, Database db) throws Bug, SystemException 355 { 356 IBaseEntity entity = null; 357 358 entity = (IBaseEntity) getObjectWithId(arg, vo.getId(), db); 359 entity.setVO(vo); 360 361 return entity.getVO(); 362 } 363 364 365 369 public static BaseEntityVO updateEntity(Class entClass, BaseEntityVO vo, String collectionMethod, Class manyClass, String [] manyIds) throws ConstraintException, SystemException 370 { 371 Database db = CastorDatabaseService.getDatabase(); 372 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 373 374 IBaseEntity entity = null; 375 376 beginTransaction(db); 377 378 try 379 { 380 List manyList = new ArrayList (); 382 if(manyIds != null) 383 { 384 for (int i=0; i < manyIds.length; i++) 385 { 386 IBaseEntity manyEntity = (IBaseEntity) getObjectWithId(manyClass, new Integer (manyIds[i]), db); 387 logger.info("!!Using experimental code: BaseController::update. getting " + manyEntity.toString()); 388 manyList.add(manyEntity); 389 } 390 } 391 392 393 entity = (IBaseEntity) getObjectWithId(entClass, vo.getId(), db); 394 entity.setVO(vo); 395 396 Object [] arg = {manyList}; 398 Class [] parm = {Collection .class}; 399 entity.getClass().getDeclaredMethod(collectionMethod, parm).invoke(entity, arg); 400 401 403 ceb.throwIfNotEmpty(); 405 406 commitTransaction(db); 407 409 } 410 catch(ConstraintException ce) 411 { 412 logger.warn("An error occurred so we should not complete the transaction:" + ce, ce); 413 rollbackTransaction(db); 414 throw ce; 415 } 416 catch(Exception e) 417 { 418 logger.error("An error occurred so we should not complete the transaction:" + e, e); 419 rollbackTransaction(db); 420 throw new SystemException(e.getMessage()); 421 } 422 423 return entity.getVO(); 424 } 425 426 430 public static IBaseEntity updateEntity(Class entClass, BaseEntityVO vo, String collectionMethod, Class manyClass, String [] manyIds, Database db) throws ConstraintException, SystemException, Exception 431 { 432 IBaseEntity entity = null; 433 434 List manyList = new ArrayList (); 435 if(manyIds != null) 436 { 437 for (int i=0; i < manyIds.length; i++) 438 { 439 IBaseEntity manyEntity = (IBaseEntity) getObjectWithId(manyClass, new Integer (manyIds[i]), db); 440 logger.info("!!Using experimental code: BaseController::update. getting " + manyEntity.toString()); 441 manyList.add(manyEntity); 442 } 443 } 444 445 entity = (IBaseEntity) getObjectWithId(entClass, vo.getId(), db); 446 entity.setVO(vo); 447 448 Object [] arg = {manyList}; 450 Class [] parm = {Collection .class}; 451 entity.getClass().getDeclaredMethod(collectionMethod, parm).invoke(entity, arg); 452 453 return entity; 454 } 455 456 457 476 477 480 481 protected static Object getObjectWithId(Class arg, Integer id, Database db) throws SystemException, Bug 482 { 483 Object object = null; 484 try 485 { 486 logger.info("Loading " + arg + " in read/write mode."); 487 object = db.load(arg, id); 488 } 489 catch(Exception e) 490 { 491 throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e); 492 } 493 494 if(object == null) 495 { 496 throw new Bug("The object with id [" + id + "] was not found. This should never happen."); 497 } 498 return object; 499 } 500 501 502 505 506 protected static Object getObjectWithIdAsReadOnly(Class arg, Integer id, Database db) throws SystemException, Bug 507 { 508 Object object = null; 509 try 510 { 511 object = db.load(arg, id, Database.ReadOnly); 512 } 513 catch(Exception e) 514 { 515 throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e); 516 } 517 518 if(object == null) 519 { 520 throw new Bug("The object with id [" + id + "] was not found. This should never happen."); 521 } 522 return object; 523 } 524 525 528 529 protected static Object getObjectWithId(Class arg, String id, Database db) throws SystemException, Bug 530 { 531 Object object = null; 532 try 533 { 534 logger.info("Loading " + arg + " in read/write mode."); 535 536 object = db.load(arg, id); 537 } 538 catch(Exception e) 539 { 540 throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e); 541 } 542 543 if(object == null) 544 { 545 throw new Bug("The object with id [" + id + "] was not found. This should never happen."); 546 } 547 return object; 548 } 549 550 551 554 555 public static List toVOList(Collection baseEntities) throws SystemException, Bug 556 { 557 List resultVOList = new ArrayList (); 558 559 if(baseEntities != null) 560 { 561 Object o = null; 562 try 563 { 564 Iterator iterator = baseEntities.iterator(); 565 while (iterator.hasNext()) 566 { 567 o = (Object )iterator.next(); 568 resultVOList.add(o.getClass().getDeclaredMethod("getValueObject", new Class [0]).invoke(o, new Object [0])); 570 } 571 } 572 catch(NoSuchMethodException e) 573 { 574 throw new Bug("The object in list was of the wrong type: " + o.getClass().getName() + ". This should never happen.", e); 575 } 576 catch(Exception e) 577 { 578 throw new SystemException("An error occurred when we tried to convert the collection to a valueList. Reason:" + e.getMessage(), e); 579 } 580 } 581 582 return resultVOList; 583 } 584 585 588 589 public static List toModifiableVOList(Collection baseEntities) throws SystemException, Bug 590 { 591 List resultVOList = new ArrayList (); 592 593 if(baseEntities != null) 594 { 595 Object o = null; 596 try 597 { 598 Iterator iterator = baseEntities.iterator(); 599 while (iterator.hasNext()) 600 { 601 o = (Object )iterator.next(); 602 resultVOList.add(o.getClass().getDeclaredMethod("getValueObject", new Class [0]).invoke(o, new Object [0])); 604 } 605 } 606 catch(NoSuchMethodException e) 607 { 608 throw new Bug("The object in list was of the wrong type: " + o.getClass().getName() + ". This should never happen.", e); 609 } 610 catch(Exception e) 611 { 612 throw new SystemException("An error occurred when we tried to convert the collection to a valueList. Reason:" + e.getMessage(), e); 613 } 614 } 615 616 return resultVOList; 617 } 618 619 622 623 626 627 public static Object getVOWithId(Class arg, Integer id) throws SystemException, Bug 628 { 629 Database db = CastorDatabaseService.getDatabase(); 630 Object ret = null; 631 try 632 { 633 beginTransaction(db); 634 ret = getVOWithId(arg, id, db); 635 commitTransaction(db); 636 } 637 catch (Exception e) 638 { 639 rollbackTransaction(db); 640 throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e); 641 } 642 return ret; 643 } 644 645 648 649 public static BaseEntityVO getVOWithId(Class arg, Integer id, Database db) throws SystemException, Bug 650 { 651 IBaseEntity vo = null; 652 try 653 { 654 vo = (IBaseEntity)db.load(arg, id, Database.ReadOnly); 655 } 656 catch(Exception e) 657 { 658 throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e); 659 } 660 661 if(vo == null) 662 { 663 throw new Bug("The object with id [" + id + "] was not found. This should never happen."); 664 } 665 666 return vo.getVO(); 667 } 668 669 670 673 674 public static Object getVOWithId(Class arg, String id) throws SystemException, Bug 675 { 676 Database db = CastorDatabaseService.getDatabase(); 677 Object ret = null; 678 try 679 { 680 beginTransaction(db); 681 ret = getVOWithId(arg, id, db); 682 commitTransaction(db); 683 } 684 catch (Exception e) 685 { 686 rollbackTransaction(db); 687 throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e); 688 } 689 return ret; 690 } 691 692 695 696 public static BaseEntityVO getVOWithId(Class arg, String id, Database db) throws SystemException, Bug 697 { 698 IBaseEntity vo = null; 699 try 700 { 701 vo = (IBaseEntity)db.load(arg, id, Database.ReadOnly); 702 } 703 catch(Exception e) 704 { 705 throw new SystemException("An error occurred when we tried to fetch the object " + arg.getName() + ". Reason:" + e.getMessage(), e); 706 } 707 708 if(vo == null) 709 { 710 throw new Bug("The object with id [" + id + "] was not found. This should never happen."); 711 } 712 713 return vo.getVO(); 714 } 715 716 717 720 739 740 743 744 public static List getAllVOObjects(Class arg, String orderByAttribute, String direction) throws SystemException, Bug 745 { 746 Database db = CastorDatabaseService.getDatabase(); 747 List ret = null; 748 try 749 { 750 beginTransaction(db); 751 ret = getAllVOObjects(arg, orderByAttribute, direction, db); 752 commitTransaction(db); 753 } 754 catch(Exception e) 755 { 756 rollbackTransaction(db); 757 throw new SystemException("An error occurred when we tried to fetch " + arg.getName() + " Reason:" + e.getMessage(), e); 758 } 759 return ret; 760 } 761 762 763 764 767 768 public static List getAllVOObjects(Class arg, String orderByField, String direction, Database db) throws SystemException, Bug 769 { 770 ArrayList resultList = new ArrayList (); 771 OQLQuery oql; 772 try 773 { 774 775 logger.info("BaseHelper::GetAllObjects for " + arg.getName()); 776 oql = db.getOQLQuery( "SELECT u FROM " + arg.getName() + " u ORDER BY u." + orderByField + " " + direction); 777 QueryResults results = oql.execute(Database.ReadOnly); 778 779 while (results.hasMore()) 780 { 781 Object o = results.next(); 782 783 resultList.add(o.getClass().getDeclaredMethod("getValueObject", new Class [0]).invoke(o, new Object [0])); 785 } 786 } 787 catch(NoSuchMethodException e) 788 { 789 throw new Bug("The object [" + arg.getName() + "] is of the wrong type. This should never happen.", e); 790 } 791 catch(Exception e) 792 { 793 throw new SystemException("An error occurred when we tried to fetch " + arg.getName() + " Reason:" + e.getMessage(), e); 794 } 795 796 return resultList; 797 } 798 799 802 803 public List getAllVOObjects(Class arg, String primaryKey) throws SystemException, Bug 804 { 805 Database db = CastorDatabaseService.getDatabase(); 806 List ret = null; 807 try 808 { 809 beginTransaction(db); 810 ret = getAllVOObjects(arg, primaryKey, db); 811 commitTransaction(db); 812 } 813 catch(Exception e) 814 { 815 rollbackTransaction(db); 816 throw new SystemException("An error occurred when we tried to fetch " + arg.getName() + " Reason:" + e.getMessage(), e); 817 } 818 return ret; 819 } 820 821 822 825 826 public List getAllVOObjects(Class arg, String primaryKey, Database db) throws SystemException, Bug 827 { 828 ArrayList resultList = new ArrayList (); 829 OQLQuery oql; 830 try 831 { 832 oql = db.getOQLQuery( "SELECT u FROM " + arg.getName() + " u ORDER BY u." + primaryKey); 833 QueryResults results = oql.execute(Database.ReadOnly); 834 835 while (results.hasMore()) 836 { 837 IBaseEntity baseEntity = (IBaseEntity)results.next(); 838 resultList.add(baseEntity.getVO()); 839 } 840 } 841 catch(ClassCastException e) 842 { 843 throw new Bug("The object [" + arg.getName() + "] is of the wrong type. This should never happen.", e); 844 } 845 catch(Exception e) 846 { 847 throw new SystemException("An error occurred when we tried to fetch " + arg.getName() + " Reason:" + e.getMessage(), e); 848 } 849 850 return resultList; 851 } 852 853 856 857 public List getAllObjects(Class arg, String primaryKey, Database db) throws SystemException, Bug 858 { 859 ArrayList resultList = new ArrayList (); 860 OQLQuery oql; 861 try 862 { 863 oql = db.getOQLQuery( "SELECT u FROM " + arg.getName() + " u ORDER BY u." + primaryKey); 864 QueryResults results = oql.execute(Database.ReadOnly); 865 866 while (results.hasMore()) 867 { 868 IBaseEntity baseEntity = (IBaseEntity)results.next(); 869 resultList.add(baseEntity); 870 } 871 } 872 catch(ClassCastException e) 873 { 874 throw new Bug("The object [" + arg.getName() + "] is of the wrong type. This should never happen.", e); 875 } 876 catch(Exception e) 877 { 878 throw new SystemException("An error occurred when we tried to fetch " + arg.getName() + " Reason:" + e.getMessage(), e); 879 } 880 881 return resultList; 882 } 883 884 894 protected static List executeQuery(String query) throws SystemException 895 { 896 return executeQuery(query, Collections.EMPTY_LIST); 897 } 898 899 906 protected static List executeQuery(String query, Database db) throws SystemException 907 { 908 return executeQuery(query, Collections.EMPTY_LIST, db); 909 } 910 911 919 protected static List executeQuery(String query, List params) throws SystemException 920 { 921 Database db = beginTransaction(); 922 923 try 924 { 925 List results = new ArrayList (); 926 results = Collections.list(createQuery(db, query, params).execute(Database.ReadOnly)); 927 commitTransaction(db); 928 return toVOList(results); 929 } 930 catch (Exception e) 931 { 932 logger.error("Error executing " + query, e); 933 rollbackTransaction(db); 934 throw new SystemException(e.getMessage(), e); 935 } 936 } 937 938 939 948 protected static List executeQuery(String query, List params, Database db) throws SystemException 949 { 950 try 951 { 952 List resultList = new ArrayList (); 953 954 OQLQuery oql = createQuery(db, query, params); 955 QueryResults results = oql.execute(); 956 resultList = Collections.list(results); 957 958 results.close(); 959 oql.close(); 960 961 return resultList; 962 } 963 catch (Exception e) 964 { 965 logger.error("Error executing " + query, e); 966 throw new SystemException(e.getMessage(), e); 967 } 968 } 969 970 971 980 protected static OQLQuery createQuery(Database db, String query, List params) throws PersistenceException 981 { 982 OQLQuery oql = db.getOQLQuery(query); 983 if (params != null) 984 for (Iterator i = params.iterator(); i.hasNext();) 985 oql.bind(i.next()); 986 987 return oql; 988 } 989 990 991 994 995 public static ConstraintExceptionBuffer validateEntity(ValidatableEntityVO vo) 996 { 997 1003 ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer(); 1005 1006 vo.PrepareValidation(); 1008 1009 Iterator iterator = vo.getConstraintRules().iterator(); 1011 while (iterator.hasNext()) 1012 { 1013 ConstraintRule cr = (ConstraintRule) iterator.next(); 1014 Integer intId = vo.getId(); 1015 logger.info("Validating object id: " + intId); 1016 1017 switch (cr.getConstraintType()) 1019 { 1020 case Constants.EMAIL: 1021 { 1022 if (cr.getValue() != null) 1023 { 1024 EmailValidator v = new EmailValidator(cr.getFieldName()); 1026 1027 v.setObjectClass(vo.getConstraintRuleList().getEntityClass()); 1029 v.setRange(cr.getValidRange()); 1030 v.setIsRequired(cr.required); 1031 v.setMustBeUnique(cr.unique); 1032 v.setExcludeId(intId); 1033 1034 v.validate((String ) cr.getValue(), ceb); 1036 1037 1042 } 1043 break; 1044 } 1045 case Constants.STRING: 1046 { 1047 if (cr.getValue() != null) 1048 { 1049 StringValidator v = new StringValidator(cr.getFieldName()); 1050 v.setObjectClass(vo.getConstraintRuleList().getEntityClass()); 1051 v.setRange(cr.getValidRange()); 1052 v.setIsRequired(cr.required); 1053 v.setMustBeUnique(cr.unique); 1054 v.setExcludeId(intId); 1055 1056 v.validate((String ) cr.getValue(), ceb); 1057 } 1058 break; 1059 } 1060 case Constants.FLOAT: 1061 { 1062 break; 1063 } 1064 case Constants.INTEGER: 1065 { 1066 break; 1067 } 1068 case Constants.PROPERNOUN: 1069 { 1070 break; 1071 } 1072 1073 } 1075 } 1077 return ceb; 1078 } 1079 1080 1081 1082 1085 1086 1091 protected static Database beginTransaction() throws SystemException 1092 { 1093 Database db = CastorDatabaseService.getDatabase(); 1094 beginTransaction(db); 1095 return db; 1096 } 1097 1098 1101 1102 protected static void beginTransaction(Database db) throws SystemException 1103 { 1104 try 1105 { 1106 db.begin(); 1108 } 1109 catch(Exception e) 1110 { 1111 throw new SystemException("An error occurred when we tried to begin an transaction. Reason:" + e.getMessage(), e); 1112 } 1113 } 1114 1115 1118 1119 protected static void commitTransaction(Database db) throws SystemException 1120 { 1121 try 1122 { 1123 1125 db.commit(); 1126 db.close(); 1127 } 1128 catch(Exception e) 1129 { 1130 throw new SystemException("An error occurred when we tried to commit an transaction. Reason:" + e.getMessage(), e); 1131 } 1132 } 1133 1134 1135 1138 1139 protected static void rollbackTransaction(Database db) throws SystemException 1140 { 1141 try 1142 { 1143 1145 if (db != null && db.isActive()) 1146 { 1147 db.rollback(); 1148 db.close(); 1149 } 1150 } 1151 catch(Exception e) 1152 { 1153 logger.warn("An error occurred when we tried to rollback an transaction. Reason:" + e.getMessage()); 1154 } 1155 } 1156 1157 1160 1161 protected static void closeDatabase(Database db) throws SystemException 1162 { 1163 try 1164 { 1165 if (db != null) 1166 { 1167 db.close(); 1168 } 1169 } 1170 catch(Exception e) 1171 { 1172 logger.warn("An error occurred when we tried to rollback an transaction. Reason:" + e.getMessage()); 1173 } 1174 } 1175 1176 public abstract BaseEntityVO getNewVO(); 1177} | Popular Tags |