1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: abstract class AlphaDAO {
48: 49: 50: 51: 52: 53:
54: protected $OID;
55:
56: 57: 58: 59: 60: 61:
62: protected $lastQuery;
63:
64: 65: 66: 67: 68: 69:
70: protected $version_num;
71:
72: 73: 74: 75: 76: 77:
78: protected $created_ts;
79:
80: 81: 82: 83: 84: 85:
86: protected $created_by;
87:
88: 89: 90: 91: 92: 93:
94: protected $updated_ts;
95:
96: 97: 98: 99: 100: 101:
102: protected $updated_by;
103:
104: 105: 106: 107: 108: 109:
110: protected $defaultAttributes = array("OID", "lastQuery", "version_num", "dataLabels", "created_ts", "created_by", "updated_ts", "updated_by", "defaultAttributes", "transientAttributes", "uniqueAttributes", "TABLE_NAME", "logger");
111:
112: 113: 114: 115: 116: 117:
118: protected $transientAttributes = array("lastQuery", "dataLabels", "defaultAttributes", "transientAttributes", "uniqueAttributes", "TABLE_NAME", "logger");
119:
120: 121: 122: 123: 124: 125:
126: protected $uniqueAttributes = array();
127:
128: 129: 130: 131: 132: 133:
134: protected $dataLabels = array();
135:
136: 137: 138: 139: 140: 141:
142: private static $logger = null;
143:
144: 145: 146: 147: 148: 149:
150: private $maintainHistory = false;
151:
152: 153: 154: 155: 156:
157: public function __construct() {
158: self::$logger = new Logger('AlphaDAO');
159: self::$logger->debug('>>__construct()');
160:
161: $this->version_num = new Integer(0);
162: $this->created_ts = new Timestamp(date("Y-m-d H:i:s"));
163: $person_ID = (isset($_SESSION['currentUser'])? $_SESSION['currentUser']->getOID(): 0);
164: $this->created_by = new Integer($person_ID);
165: $this->updated_ts = new Timestamp(date("Y-m-d H:i:s"));
166: $this->updated_by = new Integer($person_ID);
167:
168: self::$logger->debug('<<__construct');
169: }
170:
171: 172: 173: 174: 175: 176: 177: 178:
179: public static function getConnection() {
180: throw new AlphaException('The static getConnection() method is not longer supported! Please update your code to instantiate a AlphaDAOProviderInterface '.
181: 'instance, then invoke the query() method on that instance to run a custom query.');
182: }
183:
184: 185: 186: 187: 188:
189: public static function disconnect() {
190: global $config;
191:
192: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), new PersonObject());
193: $provider->disconnect();
194: }
195:
196: 197: 198: 199: 200: 201: 202: 203:
204: public function query($sqlQuery) {
205: self::$logger->debug('>>query(sqlQuery=['.$sqlQuery.'])');
206:
207: global $config;
208:
209: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
210: $result = $provider->query($sqlQuery);
211:
212: self::$logger->debug('<<query ['.print_r($result, true).']');
213: return $result;
214: }
215:
216: 217: 218: 219: 220: 221: 222:
223: public function load($OID) {
224: self::$logger->debug('>>load(OID=['.$OID.'])');
225:
226: if(method_exists($this, 'before_load_callback'))
227: $this->before_load_callback();
228:
229: global $config;
230:
231: $this->OID = $OID;
232:
233: if($config->get('cache.provider.name') != '' && $this->loadFromCache()) {
234:
235: }else{
236: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
237: $provider->load($OID);
238:
239: if($config->get('cache.provider.name') != '')
240: $this->addToCache();
241: }
242:
243: $this->setEnumOptions();
244:
245: if(method_exists($this, 'after_load_callback'))
246: $this->after_load_callback();
247:
248: self::$logger->debug('<<load');
249: }
250:
251: 252: 253: 254: 255: 256: 257: 258: 259: 260:
261: public function loadByAttribute($attribute, $value, $ignoreClassType=false, $loadAttributes=array()) {
262: self::$logger->debug('>>loadByAttribute(attribute=['.$attribute.'], value=['.$value.'], ignoreClassType=['.$ignoreClassType.'],
263: loadAttributes=['.var_export($loadAttributes, true).'])');
264:
265: if(method_exists($this, 'before_loadByAttribute_callback'))
266: $this->before_loadByAttribute_callback();
267:
268: global $config;
269:
270: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
271: $provider->loadByAttribute($attribute, $value, $ignoreClassType, $loadAttributes);
272:
273: $this->setEnumOptions();
274:
275: if($config->get('cache.provider.name') != '' && count($loadAttributes) == 0)
276: $this->addToCache();
277:
278: if(method_exists($this, 'after_loadByAttribute_callback'))
279: $this->after_loadByAttribute_callback();
280:
281: self::$logger->debug('<<loadByAttribute');
282: }
283:
284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295:
296: public function loadAll($start=0, $limit=0, $orderBy='OID', $order='ASC', $ignoreClassType=false) {
297: self::$logger->debug('>>loadAll(start=['.$start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.']');
298:
299: if(method_exists($this, 'before_loadAll_callback'))
300: $this->before_loadAll_callback();
301:
302: global $config;
303:
304: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
305: $objects = $provider->loadAll($start, $limit, $orderBy, $order, $ignoreClassType);
306:
307: if(method_exists($this, 'after_loadAll_callback'))
308: $this->after_loadAll_callback();
309:
310: self::$logger->debug('<<loadAll ['.count($objects).']');
311: return $objects;
312: }
313:
314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329:
330: public function loadAllByAttribute($attribute, $value, $start=0, $limit=0, $orderBy="OID", $order="ASC", $ignoreClassType=false, $constructorArgs=array()) {
331: self::$logger->debug('>>loadAllByAttribute(attribute=['.$attribute.'], value=['.$value.'], start=['.$start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.'], constructorArgs=['.print_r($constructorArgs, true).']');
332:
333: if(method_exists($this, 'before_loadAllByAttribute_callback'))
334: $this->before_loadAllByAttribute_callback();
335:
336: global $config;
337:
338: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
339: $objects = $provider->loadAllByAttribute($attribute, $value, $start, $limit, $orderBy, $order, $ignoreClassType);
340:
341: if(method_exists($this, 'after_loadAllByAttribute_callback'))
342: $this->after_loadAllByAttribute_callback();
343:
344: self::$logger->debug('<<loadAllByAttribute ['.count($objects).']');
345: return $objects;
346: }
347:
348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362:
363: public function loadAllByAttributes($attributes=array(), $values=array(), $start=0, $limit=0, $orderBy='OID', $order='ASC', $ignoreClassType=false) {
364: self::$logger->debug('>>loadAllByAttributes(attributes=['.var_export($attributes, true).'], values=['.var_export($values, true).'], start=['.
365: $start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.']');
366:
367: if(method_exists($this, 'before_loadAllByAttributes_callback'))
368: $this->before_loadAllByAttributes_callback();
369:
370: global $config;
371:
372: if(!is_array($attributes) || !is_array($values)) {
373: throw new IllegalArguementException('Illegal arrays attributes=['.var_export($attributes, true).'] and values=['.var_export($values, true).
374: '] provided to loadAllByAttributes');
375: }
376:
377: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
378: $objects = $provider->loadAllByAttributes($attributes, $values, $start, $limit, $orderBy, $order, $ignoreClassType);
379:
380: if(method_exists($this, 'after_loadAllByAttributes_callback'))
381: $this->after_loadAllByAttributes_callback();
382:
383: self::$logger->debug('<<loadAllByAttributes ['.count($objects).']');
384: return $objects;
385: }
386:
387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399:
400: public function loadAllByDayUpdated($date, $start=0, $limit=0, $orderBy="OID", $order="ASC", $ignoreClassType=false) {
401: self::$logger->debug('>>loadAllByDayUpdated(date=['.$date.'], start=['.$start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.']');
402:
403: if(method_exists($this, 'before_loadAllByDayUpdated_callback'))
404: $this->before_loadAllByDayUpdated_callback();
405:
406: global $config;
407:
408: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
409: $objects = $provider->loadAllByDayUpdated($date, $start, $limit, $orderBy, $order, $ignoreClassType);
410:
411: if(method_exists($this, 'after_loadAllByDayUpdated_callback'))
412: $this->after_loadAllByDayUpdated_callback();
413:
414: self::$logger->debug('<<loadAllByDayUpdated ['.count($objects).']');
415: return $objects;
416: }
417:
418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430:
431: public function loadAllFieldValuesByAttribute($attribute, $value, $returnAttribute, $order='ASC', $ignoreClassType=false) {
432: self::$logger->debug('>>loadAllFieldValuesByAttribute(attribute=['.$attribute.'], value=['.$value.'], returnAttribute=['.$returnAttribute.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.']');
433:
434: global $config;
435:
436: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
437: $values = $provider->loadAllFieldValuesByAttribute($attribute, $value, $returnAttribute, $order, $ignoreClassType);
438:
439: self::$logger->debug('<<loadAllFieldValuesByAttribute ['.count($values).']');
440: return $values;
441: }
442:
443: 444: 445: 446: 447: 448: 449: 450:
451: public function save() {
452: self::$logger->debug('>>save()');
453:
454: if(method_exists($this, 'before_save_callback'))
455: $this->before_save_callback();
456:
457: global $config;
458:
459:
460: if(!$this->validate()) {
461: throw new FailedSaveException('Could not save due to a validation error.');
462: return;
463: }else{
464: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
465: $provider->save();
466:
467: if($config->get('cache.provider.name') != '') {
468: $this->removeFromCache();
469: $this->addToCache();
470: }
471:
472: if(method_exists($this, 'after_save_callback'))
473: $this->after_save_callback();
474: }
475: }
476:
477: 478: 479: 480: 481: 482: 483: 484: 485: 486:
487: public function saveAttribute($attribute, $value) {
488: self::$logger->debug('>>saveAttribute(attribute=['.$attribute.'], value=['.$value.'])');
489:
490: if(method_exists($this, 'before_saveAttribute_callback'))
491: $this->before_saveAttribute_callback();
492:
493: global $config;
494:
495: if(!isset($this->$attribute))
496: throw new IllegalArguementException('Could not perform save, as the attribute ['.$attribute.'] is not present on the class['.get_class($this).']');
497:
498: if($this->isTransient())
499: throw new FailedSaveException('Cannot perform saveAttribute method on transient BO!');
500:
501: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
502: $provider->saveAttribute($attribute, $value);
503:
504: if($config->get('cache.provider.name') != '') {
505: $this->removeFromCache();
506: $this->addToCache();
507: }
508:
509: if(method_exists($this, 'after_saveAttribute_callback'))
510: $this->after_saveAttribute_callback();
511:
512: self::$logger->debug('<<saveAttribute');
513: }
514:
515: 516: 517: 518: 519: 520:
521: public function saveHistory() {
522: self::$logger->debug('>>saveHistory()');
523:
524: if(method_exists($this, 'before_saveHistory_callback'))
525: $this->before_saveHistory_callback();
526:
527: global $config;
528:
529: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
530: $provider->saveHistory();
531:
532: if(method_exists($this, 'after_saveHistory_callback'))
533: $this->after_saveHistory_callback();
534: }
535:
536: 537: 538: 539: 540: 541: 542:
543: protected function validate() {
544: self::$logger->debug('>>validate()');
545:
546: if(method_exists($this, 'before_validate_callback'))
547: $this->before_validate_callback();
548:
549: $valid = true;
550:
551:
552: $reflection = new ReflectionClass(get_class($this));
553: $properties = $reflection->getProperties();
554:
555: foreach($properties as $propObj) {
556: $propName = $propObj->name;
557: if(!in_array($propName, $this->defaultAttributes) && !in_array($propName, $this->transientAttributes)) {
558: $propClass = get_class($this->getPropObject($propName));
559: if (strtoupper($propClass) != "ENUM" &&
560: strtoupper($propClass) != "DENUM" &&
561: strtoupper($propClass) != "DENUMITEM" &&
562: strtoupper($propClass) != "BOOLEAN") {
563: if ($this->getPropObject($propName) != false && !preg_match($this->getPropObject($propName)->getRule(), $this->getPropObject($propName)->getValue())) {
564: throw new ValidationException('Failed to save, validation error is: '.$this->getPropObject($propName)->getHelper());
565: $valid = false;
566: }
567: }
568: }
569: }
570:
571: if(method_exists($this, 'after_validate_callback'))
572: $this->after_validate_callback();
573:
574: self::$logger->debug('<<validate ['.$valid.']');
575: return $valid;
576: }
577:
578: 579: 580: 581: 582: 583:
584: public function delete() {
585: self::$logger->debug('>>delete()');
586:
587: if(method_exists($this, 'before_delete_callback'))
588: $this->before_delete_callback();
589:
590: global $config;
591:
592:
593: $reflection = new ReflectionClass(get_class($this));
594: $properties = $reflection->getProperties();
595:
596:
597: foreach($properties as $propObj) {
598: $propName = $propObj->name;
599:
600: if(!$propObj->isPrivate() && isset($this->$propName) && $this->$propName instanceof Relation) {
601: $prop = $this->getPropObject($propName);
602:
603:
604: if($prop->getRelationType() == 'MANY-TO-MANY') {
605: self::$logger->debug('Deleting MANY-TO-MANY lookup objects...');
606:
607: try{
608:
609: $side = $prop->getSide(get_class($this));
610: }catch (IllegalArguementException $iae) {
611: $side = $prop->getSide(ucfirst($this->getTableName()).'Object');
612: }
613:
614: self::$logger->debug('Side is ['.$side.']'.$this->getOID());
615:
616: $lookUp = $prop->getLookup();
617: self::$logger->debug('Lookup object['.var_export($lookUp, true).']');
618:
619:
620: if($side == 'left')
621: $lookUp->deleteAllByAttribute('leftID', $this->getOID());
622: else
623: $lookUp->deleteAllByAttribute('rightID', $this->getOID());
624: self::$logger->debug('...done deleting!');
625: }
626:
627:
628: if($prop->getRelationType() == 'ONE-TO-MANY' && !$prop->getRelatedClass() == 'TagObject') {
629: $relatedObjects = $prop->getRelatedObjects();
630:
631: foreach($relatedObjects as $object) {
632: $object->set($prop->getRelatedClassField(), null);
633: $object->save();
634: }
635: }
636:
637:
638: if($prop->getRelationType() == 'ONE-TO-MANY' && $prop->getRelatedClass() == 'TagObject') {
639:
640: $prop->setValue($this->getOID());
641: $relatedObjects = $prop->getRelatedObjects();
642:
643: foreach($relatedObjects as $object) {
644: $object->delete();
645: }
646: }
647: }
648: }
649:
650: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
651: $provider->delete();
652:
653: if($config->get('cache.provider.name') != '')
654: $this->removeFromCache();
655:
656: if(method_exists($this, 'after_delete_callback'))
657: $this->after_delete_callback();
658:
659: $this->clear();
660: self::$logger->debug('<<delete');
661: }
662:
663: 664: 665: 666: 667: 668: 669: 670: 671:
672: public function deleteAllByAttribute($attribute, $value) {
673: self::$logger->debug('>>deleteAllByAttribute(attribute=['.$attribute.'], value=['.$value.'])');
674:
675: if(method_exists($this, 'before_deleteAllByAttribute_callback'))
676: $this->before_deleteAllByAttribute_callback();
677:
678: try {
679: $doomedObjects = $this->loadAllByAttribute($attribute, $value);
680: $deletedRowCount = 0;
681:
682: foreach ($doomedObjects as $object) {
683: $object->delete();
684: $deletedRowCount++;
685: }
686: }catch (BONotFoundException $bonf) {
687:
688: self::$logger->warn($bonf->getMessage());
689: return 0;
690: }catch (AlphaException $e) {
691: throw new FailedDeleteException('Failed to delete objects, error is ['.$e->getMessage().']');
692: self::$logger->debug('<<deleteAllByAttribute [0]');
693: return 0;
694: }
695:
696: if(method_exists($this, 'after_deleteAllByAttribute_callback'))
697: $this->after_deleteAllByAttribute_callback();
698:
699: self::$logger->debug('<<deleteAllByAttribute ['.$deletedRowCount.']');
700: return $deletedRowCount;
701: }
702:
703: 704: 705: 706: 707: 708: 709:
710: public function getVersion() {
711: self::$logger->debug('>>getVersion()');
712:
713: if(method_exists($this, 'before_getVersion_callback'))
714: $this->before_getVersion_callback();
715:
716: global $config;
717:
718: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
719: $ver = $provider->getVersion();
720:
721: if(method_exists($this, 'after_getVersion_callback'))
722: $this->after_getVersion_callback();
723:
724: self::$logger->debug('<<getVersion ['.$ver.']');
725: return $ver;
726: }
727:
728: 729: 730: 731: 732: 733:
734: public function makeTable() {
735: self::$logger->debug('>>makeTable()');
736:
737: if(method_exists($this, 'before_makeTable_callback'))
738: $this->before_makeTable_callback();
739:
740: global $config;
741:
742: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
743: $provider->makeTable();
744:
745: if(method_exists($this, 'after_makeTable_callback'))
746: $this->after_makeTable_callback();
747:
748: self::$logger->info('Successfully created the table ['.$this->getTableName().'] for the class ['.get_class($this).']');
749:
750: self::$logger->debug('<<makeTable');
751: }
752:
753: 754: 755: 756: 757: 758:
759: public function makeHistoryTable() {
760: self::$logger->debug('>>makeHistoryTable()');
761:
762: if(method_exists($this, 'before_makeHistoryTable_callback'))
763: $this->before_makeHistoryTable_callback();
764:
765: global $config;
766:
767: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
768: $provider->makeHistoryTable();
769:
770: if(method_exists($this, 'after_makeHistoryTable_callback'))
771: $this->after_makeHistoryTable_callback();
772:
773: self::$logger->info('Successfully created the table ['.$this->getTableName().'_history] for the class ['.get_class($this).']');
774:
775: self::$logger->debug('<<makeHistoryTable');
776: }
777:
778: 779: 780: 781: 782: 783:
784: public function rebuildTable() {
785: self::$logger->debug('>>rebuildTable()');
786:
787: if(method_exists($this, 'before_rebuildTable_callback'))
788: $this->before_rebuildTable_callback();
789:
790: global $config;
791:
792: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
793: $provider->rebuildTable();
794:
795: if(method_exists($this, 'after_rebuildTable_callback'))
796: $this->after_rebuildTable_callback();
797:
798: self::$logger->debug('<<rebuildTable');
799: }
800:
801: 802: 803: 804: 805: 806: 807:
808: public function dropTable($tableName=null) {
809: self::$logger->debug('>>dropTable()');
810:
811: if(method_exists($this, 'before_dropTable_callback'))
812: $this->before_dropTable_callback();
813:
814: global $config;
815:
816: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
817: $provider->dropTable($tableName);
818:
819: if(method_exists($this, 'after_dropTable_callback'))
820: $this->after_dropTable_callback();
821:
822: self::$logger->debug('<<dropTable');
823: }
824:
825: 826: 827: 828: 829: 830: 831: 832:
833: public function addProperty($propName) {
834: self::$logger->debug('>>addProperty(propName=['.$propName.'])');
835:
836: global $config;
837:
838: if(method_exists($this, 'before_addProperty_callback'))
839: $this->before_addProperty_callback();
840:
841: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
842: $provider->addProperty($propName);
843:
844: if(method_exists($this, 'after_addProperty_callback'))
845: $this->after_addProperty_callback();
846:
847: self::$logger->debug('<<addProperty');
848: }
849:
850: 851: 852: 853: 854: 855:
856: public function populateFromPost($filterAll=false) {
857: self::$logger->debug('>>populateFromPost(filterAll=['.$filterAll.'])');
858:
859: if(method_exists($this, 'before_populateFromPost_callback'))
860: $this->before_populateFromPost_callback();
861:
862:
863: $reflection = new ReflectionClass(get_class($this));
864: $properties = $reflection->getProperties();
865:
866: foreach($properties as $propObj) {
867: $propName = $propObj->name;
868:
869: if(!in_array($propName, $this->defaultAttributes) && !in_array($propName, $this->transientAttributes)) {
870: $propClass = get_class($this->getPropObject($propName));
871:
872: if(isset($_POST[$propName])) {
873:
874: if(strtoupper($propClass) == 'BOOLEAN' && $_POST[$propName] == 'on') {
875: $_POST[$propName] = 1;
876: }
877:
878: if (strtoupper($propClass) != 'DATE' && strtoupper($propClass) != 'TIMESTAMP') {
879: if($filterAll) {
880: $this->getPropObject($propName)->setValue(InputFilter::encode($_POST[$propName], false));
881: }else{
882: if(strtoupper($propClass) == 'TEXT' && !$this->getPropObject($propName)->getAllowHTML())
883: $this->getPropObject($propName)->setValue(InputFilter::encode($_POST[$propName], false));
884: else
885: $this->getPropObject($propName)->setValue(InputFilter::encode($_POST[$propName], true));
886: }
887: }else{
888: if($filterAll)
889: $this->getPropObject($propName)->populateFromString(InputFilter::encode($_POST[$propName], false));
890: else
891: $this->getPropObject($propName)->populateFromString(InputFilter::encode($_POST[$propName], true));
892: }
893: }
894: }
895: if ($propName == 'version_num' && isset($_POST['version_num']))
896: $this->version_num->setValue($_POST['version_num']);
897: }
898: if(method_exists($this, 'after_populateFromPost_callback'))
899: $this->after_populateFromPost_callback();
900:
901: self::$logger->debug('<<populateFromPost');
902: }
903:
904: 905: 906: 907: 908: 909: 910:
911: public function getMAX() {
912: self::$logger->debug('>>getMAX()');
913:
914: if(method_exists($this, 'before_getMAX_callback'))
915: $this->before_getMAX_callback();
916:
917: global $config;
918:
919: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
920: $max = $provider->getMAX();
921:
922: if(method_exists($this, 'after_getMAX_callback'))
923: $this->after_getMAX_callback();
924:
925: self::$logger->debug('<<getMAX ['.$max.']');
926: return $max;
927: }
928:
929: 930: 931: 932: 933: 934: 935: 936: 937:
938: public function getCount($attributes=array(), $values=array()) {
939: self::$logger->debug('>>getCount(attributes=['.var_export($attributes, true).'], values=['.var_export($values, true).'])');
940:
941: if(method_exists($this, 'before_getCount_callback'))
942: $this->before_getCount_callback();
943:
944: global $config;
945:
946: if(!is_array($attributes) || !is_array($values)) {
947: throw new IllegalArguementException('Illegal arrays attributes=['.var_export($attributes, true).'] and values=['.var_export($values, true).'] provided to loadAllByAttributes');
948: }
949:
950: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
951: $count = $provider->getCount($attributes, $values);
952:
953: if(method_exists($this, 'after_getCount_callback'))
954: $this->after_getCount_callback();
955:
956: self::$logger->debug('<<getCount ['.$count.']');
957: return $count;
958: }
959:
960: 961: 962: 963: 964: 965: 966: 967:
968: public function getHistoryCount() {
969: self::$logger->debug('>>getHistoryCount()');
970:
971: if(method_exists($this, 'before_getHistoryCount_callback'))
972: $this->before_getHistoryCount_callback();
973:
974: global $config;
975:
976: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
977: $count = $provider->getHistoryCount();
978:
979: if(method_exists($this, 'after_getHistoryCount_callback'))
980: $this->after_getHistoryCount_callback();
981:
982: self::$logger->debug('<<getHistoryCount ['.$count.']');
983: return $count;
984: }
985:
986: 987: 988: 989: 990: 991: 992: 993:
994: public function getID() {
995: self::$logger->debug('>>getID()');
996: $oid = str_pad($this->OID, 11, '0', STR_PAD_LEFT);
997: self::$logger->debug('<<getID ['.$oid.']');
998: return $oid;
999: }
1000:
1001: 1002: 1003: 1004: 1005: 1006: 1007:
1008: public final function getOID() {
1009: if(self::$logger == null)
1010: self::$logger = new Logger('AlphaDAO');
1011: self::$logger->debug('>>getOID()');
1012: $oid = str_pad($this->OID, 11, '0', STR_PAD_LEFT);
1013: self::$logger->debug('<<getOID ['.$oid.']');
1014: return $oid;
1015: }
1016:
1017: 1018: 1019: 1020: 1021: 1022:
1023: public function getVersionNumber() {
1024: self::$logger->debug('>>getVersionNumber()');
1025: self::$logger->debug('<<getVersionNumber ['.$this->version_num.']');
1026: return $this->version_num;
1027: }
1028:
1029: 1030: 1031: 1032: 1033: 1034:
1035: protected function setEnumOptions() {
1036: self::$logger->debug('>>setEnumOptions()');
1037:
1038: if(method_exists($this, 'before_setEnumOptions_callback'))
1039: $this->before_setEnumOptions_callback();
1040:
1041: global $config;
1042:
1043: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1044: try {
1045: $provider->setEnumOptions();
1046: }catch (NotImplementedException $e) {
1047: self::$logger->info($e->getMessage());
1048: }
1049:
1050: self::$logger->debug('<<setEnumOptions');
1051: }
1052:
1053: 1054: 1055: 1056: 1057: 1058: 1059: 1060: 1061: 1062: 1063: 1064:
1065: public function get($prop, $noChildMethods = false) {
1066: if(self::$logger == null)
1067: self::$logger = new Logger('AlphaDAO');
1068:
1069: self::$logger->debug('>>get(prop=['.$prop.'], noChildMethods=['.$noChildMethods.'])');
1070:
1071: if(method_exists($this, 'before_get_callback'))
1072: $this->before_get_callback();
1073:
1074: if(empty($prop))
1075: throw new IllegalArguementException('Cannot call get with empty $prop arguement!');
1076:
1077:
1078: if(!$noChildMethods && method_exists($this, 'get'.ucfirst($prop))) {
1079: if(method_exists($this, 'after_get_callback'))
1080: $this->after_get_callback();
1081:
1082: self::$logger->debug('<<get ['.eval('return print_r($this->get'.ucfirst($prop).'(), true);').'])');
1083: return eval('return $this->get'.ucfirst($prop).'();');
1084: }else{
1085:
1086: if(isset($this->$prop) && is_object($this->$prop) && method_exists($this->$prop, 'getValue')) {
1087: if(method_exists($this, 'after_get_callback'))
1088: $this->after_get_callback();
1089:
1090:
1091: self::$logger->debug('<<get ['.$this->$prop->getValue().'])');
1092: return $this->$prop->getValue();
1093: }elseif(isset($this->$prop)) {
1094: if(method_exists($this, 'after_get_callback'))
1095: $this->after_get_callback();
1096:
1097:
1098: self::$logger->debug('<<get ['.print_r($this->$prop, true).'])');
1099: return $this->$prop;
1100: }else{
1101: throw new AlphaException('Could not access the property ['.$prop.'] on the object of class ['.get_class($this).']');
1102: self::$logger->debug('<<get [false])');
1103: return false;
1104: }
1105: }
1106: }
1107:
1108: 1109: 1110: 1111: 1112: 1113: 1114: 1115: 1116: 1117: 1118: 1119:
1120: public function set($prop, $value, $noChildMethods = false) {
1121: self::$logger->debug('>>set(prop=['.$prop.'], $value=['.print_r($value, true).'], noChildMethods=['.$noChildMethods.'])');
1122:
1123: if(method_exists($this, 'before_set_callback'))
1124: $this->before_set_callback();
1125:
1126:
1127: if(!$noChildMethods && method_exists($this, 'set'.ucfirst($prop))) {
1128: if(method_exists($this, 'after_set_callback'))
1129: $this->after_set_callback();
1130:
1131: eval('$this->set'.ucfirst($prop).'($value);');
1132: }else{
1133:
1134: if(isset($this->$prop)) {
1135: if(method_exists($this, 'after_set_callback'))
1136: $this->after_set_callback();
1137:
1138:
1139: if (is_object($this->$prop) && get_class($this->$prop) != false) {
1140: if (strtoupper(get_class($this->$prop)) != 'DATE' && strtoupper(get_class($this->$prop)) != 'TIMESTAMP') {
1141: $this->$prop->setValue($value);
1142: }else{
1143:
1144: $this->$prop->populateFromString($value);
1145: }
1146: }else{
1147:
1148: $this->$prop = $value;
1149: }
1150: }else{
1151: throw new AlphaException('Could not set the property ['.$prop.'] on the object of the class ['.get_class($this).']. Property may not exist, or else does not have a setValue() method and is private or protected.');
1152: }
1153: }
1154: self::$logger->debug('<<set');
1155: }
1156:
1157: 1158: 1159: 1160: 1161: 1162: 1163: 1164: 1165:
1166: public function getPropObject($prop) {
1167: self::$logger->debug('>>getPropObject(prop=['.$prop.'])');
1168:
1169: if(method_exists($this, 'before_getPropObject_callback'))
1170: $this->before_getPropObject_callback();
1171:
1172:
1173: $reflection = new ReflectionClass(get_class($this));
1174: $properties = $reflection->getProperties();
1175:
1176:
1177: $attribute = new ReflectionProperty(get_class($this), $prop);
1178:
1179: if($attribute->isPrivate()) {
1180: if(method_exists($this, 'after_getPropObject_callback'))
1181: $this->after_getPropObject_callback();
1182:
1183: self::$logger->debug('<<getPropObject [false]');
1184: return false;
1185: }
1186:
1187: foreach($properties as $propObj) {
1188: $propName = $propObj->name;
1189:
1190: if($prop == $propName) {
1191: if(method_exists($this, 'after_getPropObject_callback'))
1192: $this->after_getPropObject_callback();
1193:
1194: self::$logger->debug('<<getPropObject ['.var_export($this->$prop, true).']');
1195: return $this->$prop;
1196: }
1197: }
1198: throw new IllegalArguementException('Could not access the property object ['.$prop.'] on the object of class ['.get_class($this).']');
1199: self::$logger->debug('<<getPropObject [false]');
1200: return false;
1201: }
1202:
1203: 1204: 1205: 1206: 1207: 1208: 1209: 1210:
1211: public function checkTableExists($checkHistoryTable = false) {
1212: self::$logger->debug('>>checkTableExists()');
1213:
1214: if(method_exists($this, 'before_checkTableExists_callback'))
1215: $this->before_checkTableExists_callback();
1216:
1217: global $config;
1218:
1219: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1220: $tableExists = $provider->checkTableExists($checkHistoryTable);
1221:
1222: if(method_exists($this, 'after_checkTableExists_callback'))
1223: $this->after_checkTableExists_callback();
1224:
1225: self::$logger->debug('<<checkTableExists ['.$tableExists.']');
1226: return $tableExists;
1227: }
1228:
1229: 1230: 1231: 1232: 1233: 1234: 1235: 1236: 1237: 1238:
1239: public static function checkBOTableExists($BOClassName, $checkHistoryTable = false) {
1240: if(self::$logger == null)
1241: self::$logger = new Logger('AlphaDAO');
1242: self::$logger->debug('>>checkBOTableExists(BOClassName=['.$BOClassName.'])');
1243:
1244: global $config;
1245:
1246: require_once $config->get('app.root').'alpha/model/'.$config->get('db.provider.name').'.inc';
1247: eval('$tableExists = '.$config->get('db.provider.name').'::checkBOTableExists($BOClassName, $checkHistoryTable);');
1248:
1249: self::$logger->debug('<<checkBOTableExists ['.($tableExists ? 'true' : 'false').']');
1250: return $tableExists;
1251: }
1252:
1253: 1254: 1255: 1256: 1257: 1258: 1259: 1260:
1261: public function checkTableNeedsUpdate() {
1262: self::$logger->debug('>>checkTableNeedsUpdate()');
1263:
1264: global $config;
1265:
1266: if(method_exists($this, 'before_checkTableNeedsUpdate_callback'))
1267: $this->before_checkTableNeedsUpdate_callback();
1268:
1269: $tableExists = $this->checkTableExists();
1270:
1271: if (!$tableExists) {
1272: self::$logger->debug('<<checkTableNeedsUpdate [true]');
1273: return true;
1274: }else{
1275:
1276: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1277: $updateRequired = $provider->checkTableNeedsUpdate();
1278:
1279: if(method_exists($this, 'after_checkTableNeedsUpdate_callback'))
1280: $this->after_checkTableNeedsUpdate_callback();
1281:
1282: self::$logger->debug('<<checkTableNeedsUpdate ['.$updateRequired.']');
1283: return $updateRequired;
1284: }
1285: }
1286:
1287: 1288: 1289: 1290: 1291: 1292: 1293: 1294:
1295: public function findMissingFields() {
1296: self::$logger->debug('>>findMissingFields()');
1297:
1298: global $config;
1299:
1300: if(method_exists($this, 'before_findMissingFields_callback'))
1301: $this->before_findMissingFields_callback();
1302:
1303: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1304: $missingFields = $provider->findMissingFields();
1305:
1306: if(method_exists($this, 'after_findMissingFields_callback'))
1307: $this->after_findMissingFields_callback();
1308:
1309: self::$logger->debug('<<findMissingFields ['.var_export($missingFields, true).']');
1310: return $missingFields;
1311: }
1312:
1313: 1314: 1315: 1316: 1317: 1318: 1319:
1320: public function getTableName() {
1321: self::$logger->debug('>>getTableName()');
1322:
1323: eval('$TABLE_NAME = '.get_class($this).'::TABLE_NAME;');
1324:
1325: if(!empty($TABLE_NAME)) {
1326: self::$logger->debug('<<getTableName ['.$TABLE_NAME.']');
1327: return $TABLE_NAME;
1328: }else{
1329: throw new AlphaException('Error: no TABLE_NAME constant set for the class '.get_class($this));
1330: self::$logger->debug('<<getTableName []');
1331: return '';
1332: }
1333: }
1334:
1335: 1336: 1337: 1338: 1339: 1340:
1341: public function getCreatorId() {
1342: self::$logger->debug('>>getCreatorId()');
1343: self::$logger->debug('<<getCreatorId ['.$this->created_by.']');
1344: return $this->created_by;
1345: }
1346:
1347: 1348: 1349: 1350: 1351: 1352:
1353: public function getUpdatorId() {
1354: self::$logger->debug('>>getUpdatorId()');
1355: self::$logger->debug('<<getUpdatorId ['.$this->updated_by.']');
1356: return $this->updated_by;
1357: }
1358:
1359: 1360: 1361: 1362: 1363: 1364:
1365: public function getCreateTS() {
1366: self::$logger->debug('>>getCreateTS()');
1367: self::$logger->debug('<<getCreateTS ['.$this->created_ts.']');
1368: return $this->created_ts;
1369: }
1370:
1371: 1372: 1373: 1374: 1375: 1376:
1377: public function getUpdateTS() {
1378: self::$logger->debug('>>getUpdateTS()');
1379: self::$logger->debug('<<getUpdateTS ['.$this->updated_ts.']');
1380: return $this->updated_ts;
1381: }
1382:
1383: 1384: 1385: 1386: 1387: 1388:
1389: public function markTransient($attributeName) {
1390: self::$logger->debug('>>markTransient(attributeName=['.$attributeName.'])');
1391: self::$logger->debug('<<markTransient');
1392: array_push($this->transientAttributes, $attributeName);
1393: }
1394:
1395: 1396: 1397: 1398: 1399: 1400: 1401:
1402: public function markPersistent($attributeName) {
1403: self::$logger->debug('>>markPersistent(attributeName=['.$attributeName.'])');
1404: self::$logger->debug('<<markPersistent');
1405: $this->transientAttributes = array_diff($this->transientAttributes, array($attributeName));
1406: }
1407:
1408: 1409: 1410: 1411: 1412: 1413: 1414: 1415:
1416: protected function markUnique($attribute1Name, $attribute2Name='', $attribute3Name='') {
1417: self::$logger->debug('>>markUnique(attribute1Name=['.$attribute1Name.'], attribute2Name=['.$attribute2Name.'], attribute3Name=['.$attribute3Name.'])');
1418:
1419: if(empty($attribute2Name)) {
1420: array_push($this->uniqueAttributes, $attribute1Name);
1421: }else{
1422:
1423: if($attribute3Name == '')
1424: $attributes = $attribute1Name.'+'.$attribute2Name;
1425: else
1426: $attributes = $attribute1Name.'+'.$attribute2Name.'+'.$attribute3Name;
1427:
1428: array_push($this->uniqueAttributes, $attributes);
1429: }
1430:
1431: self::$logger->debug('<<markUnique');
1432: }
1433:
1434: 1435: 1436: 1437: 1438: 1439:
1440: public function getUniqueAttributes() {
1441: self::$logger->debug('>>getUniqueAttributes()');
1442: self::$logger->debug('<<getUniqueAttributes: ['.print_r($this->uniqueAttributes, true).']');
1443: return $this->uniqueAttributes;
1444: }
1445:
1446: 1447: 1448: 1449: 1450: 1451: 1452:
1453: public function getIndexes() {
1454: self::$logger->debug('>>getIndexes()');
1455:
1456: global $config;
1457:
1458: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1459: $indexNames = $provider->getIndexes();
1460:
1461: self::$logger->debug('<<getIndexes ['.print_r($indexNames, true).']');
1462: return $indexNames;
1463: }
1464:
1465: 1466: 1467: 1468: 1469: 1470: 1471: 1472: 1473: 1474:
1475: public function createForeignIndex($attributeName, $relatedClass, $relatedClassAttribute) {
1476: self::$logger->debug('>>createForeignIndex(attributeName=['.$attributeName.'], relatedClass=['.$relatedClass.'], relatedClassAttribute=['.$relatedClassAttribute.']');
1477:
1478: global $config;
1479:
1480: if(method_exists($this, 'before_createForeignIndex_callback'))
1481: $this->before_createForeignIndex_callback();
1482:
1483: AlphaDAO::loadClassDef($relatedClass);
1484: $relatedBO = new $relatedClass;
1485: $tableName = $relatedBO->getTableName();
1486:
1487:
1488: if($this->getTableName() == $tableName) {
1489: self::$logger->debug('<<createForeignIndex');
1490: return;
1491: }
1492:
1493: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1494: $provider->createForeignIndex($attributeName, $relatedClass, $relatedClassAttribute);
1495:
1496: if(method_exists($this, 'after_createForeignIndex_callback'))
1497: $this->after_createForeignIndex_callback();
1498:
1499: self::$logger->debug('<<createForeignIndex');
1500: }
1501:
1502: 1503: 1504: 1505: 1506: 1507: 1508: 1509: 1510:
1511: public function createUniqueIndex($attribute1Name, $attribute2Name = '', $attribute3Name = '') {
1512: self::$logger->debug('>>createUniqueIndex(attribute1Name=['.$attribute1Name.'], attribute2Name=['.$attribute2Name.'], attribute3Name=['.$attribute3Name.'])');
1513:
1514: if(method_exists($this, 'before_createUniqueIndex_callback'))
1515: $this->before_createUniqueIndex_callback();
1516:
1517: global $config;
1518:
1519: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1520: $provider->createUniqueIndex($attribute1Name, $attribute2Name, $attribute3Name);
1521:
1522: if(method_exists($this, 'after_createUniqueIndex_callback'))
1523: $this->before_createUniqueIndex_callback();
1524:
1525: self::$logger->debug('<<createUniqueIndex');
1526: }
1527:
1528: 1529: 1530: 1531: 1532: 1533:
1534: public function getDataLabels() {
1535: self::$logger->debug('>>getDataLabels()');
1536: self::$logger->debug('<<getDataLabels() ['.var_export($this->dataLabels, true).'])');
1537: return $this->dataLabels;
1538: }
1539:
1540: 1541: 1542: 1543: 1544: 1545: 1546:
1547: public function setDataLabels($labels) {
1548: self::$logger->debug('>>setDataLabels(labels=['.print_r($labels, true).'])');
1549:
1550: if(is_array($labels))
1551: $this->dataLabels = $labels;
1552: else
1553: throw new IllegalArguementException('The value ['.print_r($labels, true).'] provided to setDataLabels() is not a valid array!');
1554:
1555: self::$logger->debug('<<setDataLabels()');
1556: }
1557:
1558: 1559: 1560: 1561: 1562: 1563: 1564: 1565:
1566: public function getDataLabel($att) {
1567: self::$logger->debug('>>getDataLabel(att=['.$att.'])');
1568:
1569: if(in_array($att, array_keys($this->dataLabels))) {
1570: self::$logger->debug('<<getDataLabel ['.$this->dataLabels[$att].'])');
1571: return $this->dataLabels[$att];
1572: }else{
1573: throw new IllegalArguementException('No data label found on the class ['.get_class($this).'] for the attribute ['.$att.']');
1574: self::$logger->debug('<<getDataLabel [])');
1575: return '';
1576: }
1577: }
1578:
1579: 1580: 1581: 1582: 1583: 1584:
1585: public static function getBOClassNames() {
1586: if(self::$logger == null)
1587: self::$logger = new Logger('AlphaDAO');
1588: self::$logger->debug('>>getBOClassNames()');
1589:
1590: global $config;
1591:
1592: $classNameArray = array();
1593:
1594:
1595: if(file_exists($config->get('app.root').'model')) {
1596:
1597: $handle = opendir($config->get('app.root').'model');
1598:
1599:
1600: while (false !== ($file = readdir($handle))) {
1601: if (preg_match("/Object.inc/", $file)) {
1602: $classname = substr($file, 0, -4);
1603:
1604: array_push($classNameArray, $classname);
1605: }
1606: }
1607: }
1608:
1609:
1610:
1611: $handle = opendir($config->get('app.root').'alpha/model');
1612:
1613:
1614: while (false !== ($file = readdir($handle))) {
1615: if (preg_match("/Object.inc/", $file)) {
1616: $classname = substr($file, 0, -4);
1617:
1618: array_push($classNameArray, $classname);
1619: }
1620: }
1621:
1622: asort($classNameArray);
1623: self::$logger->debug('<<getBOClassNames ['.var_export($classNameArray, true).']');
1624: return $classNameArray;
1625: }
1626:
1627: 1628: 1629: 1630: 1631: 1632:
1633: public function getDefaultAttributes() {
1634: self::$logger->debug('>>getDefaultAttributes()');
1635: self::$logger->debug('<<getDefaultAttributes ['.var_export($this->defaultAttributes, true).']');
1636: return $this->defaultAttributes;
1637: }
1638:
1639: 1640: 1641: 1642: 1643: 1644:
1645: public function getTransientAttributes() {
1646: self::$logger->debug('>>getTransientAttributes()');
1647: self::$logger->debug('<<getTransientAttributes ['.var_export($this->transientAttributes, true).']');
1648: return $this->transientAttributes;
1649: }
1650:
1651: 1652: 1653: 1654: 1655: 1656:
1657: public function getPersistentAttributes() {
1658: self::$logger->debug('>>getPersistentAttributes()');
1659:
1660: $attributes = array();
1661:
1662:
1663: $reflection = new ReflectionClass(get_class($this));
1664: $properties = $reflection->getProperties();
1665:
1666: foreach($properties as $propObj) {
1667: $propName = $propObj->name;
1668:
1669:
1670: if(!in_array($propName, $this->transientAttributes)) {
1671: array_push($attributes, $propName);
1672: }
1673: }
1674:
1675: self::$logger->debug('<<getPersistentAttributes ['.var_export($attributes, true).']');
1676: return $attributes;
1677: }
1678:
1679: 1680: 1681: 1682: 1683: 1684:
1685: public function setOID($OID) {
1686: self::$logger->debug('>>setOID(OID=['.$OID.'])');
1687: self::$logger->debug('<<setOID');
1688: $this->OID = $OID;
1689: }
1690:
1691: 1692: 1693: 1694: 1695: 1696:
1697: public function isTransient() {
1698: self::$logger->debug('>>isTransient()');
1699:
1700: if(empty($this->OID) || !isset($this->OID) || $this->OID == '00000000000') {
1701: self::$logger->debug('<<isTransient [true]');
1702: return true;
1703: }else{
1704: self::$logger->debug('<<isTransient [false]');
1705: return false;
1706: }
1707: }
1708:
1709: 1710: 1711: 1712: 1713: 1714:
1715: public function getLastQuery() {
1716: self::$logger->debug('>>getLastQuery()');
1717: self::$logger->debug('<<getLastQuery ['.$this->lastQuery.']');
1718: return $this->lastQuery;
1719: }
1720:
1721: 1722: 1723: 1724: 1725:
1726: private function clear() {
1727: self::$logger->debug('>>clear()');
1728:
1729:
1730: $reflection = new ReflectionClass(get_class($this));
1731: $properties = $reflection->getProperties();
1732:
1733: foreach($properties as $propObj) {
1734: $propName = $propObj->name;
1735: if(!$propObj->isPrivate())
1736: unset($this->$propName);
1737: }
1738:
1739: self::$logger->debug('<<clear');
1740: }
1741:
1742: 1743: 1744: 1745: 1746: 1747:
1748: public function reload() {
1749: self::$logger->debug('>>reload()');
1750:
1751: if(!$this->isTransient()) {
1752: $this->load($this->getOID());
1753: }else{
1754: throw new AlphaException('Cannot reload transient object from database!');
1755: }
1756: self::$logger->debug('<<reload');
1757: }
1758:
1759: 1760: 1761: 1762: 1763: 1764: 1765:
1766: public static function loadClassDef($classname) {
1767: if(self::$logger == null)
1768: self::$logger = new Logger('AlphaDAO');
1769: self::$logger->debug('>>loadClassDef(classname=['.$classname.'])');
1770:
1771: global $config;
1772:
1773: if(file_exists($config->get('app.root').'model/'.$classname.'.inc'))
1774: require_once $config->get('app.root').'model/'.$classname.'.inc';
1775: elseif(file_exists($config->get('app.root').'alpha/model/'.$classname.'.inc'))
1776: require_once $config->get('app.root').'alpha/model/'.$classname.'.inc';
1777: elseif(file_exists($config->get('app.root').'alpha/model/types/'.$classname.'.inc'))
1778: require_once $config->get('app.root').'alpha/model/types/'.$classname.'.inc';
1779: else
1780: throw new IllegalArguementException('The class ['.$classname.'] is not defined anywhere!');
1781:
1782: self::$logger->debug('<<loadClassDef');
1783: }
1784:
1785: 1786: 1787: 1788: 1789: 1790: 1791:
1792: public static function checkClassDefExists($classname) {
1793: if(self::$logger == null)
1794: self::$logger = new Logger('AlphaDAO');
1795: self::$logger->debug('>>checkClassDefExists(classname=['.$classname.'])');
1796:
1797: global $config;
1798:
1799: $exists = false;
1800:
1801: if(file_exists($config->get('app.root').'model/'.$classname.'.inc'))
1802: $exists = true;
1803: if(file_exists($config->get('app.root').'alpha/model/'.$classname.'.inc'))
1804: $exists = true;
1805: if(file_exists($config->get('app.root').'alpha/model/types/'.$classname.'.inc'))
1806: $exists = true;
1807:
1808: self::$logger->debug('<<checkClassDefExists ['.$exists.']');
1809: return $exists;
1810: }
1811:
1812: 1813: 1814: 1815: 1816: 1817: 1818: 1819:
1820: public function checkRecordExists($OID) {
1821: self::$logger->debug('>>checkRecordExists(OID=['.$OID.'])');
1822:
1823: if(method_exists($this, 'before_checkRecordExists_callback'))
1824: $this->before_checkRecordExists_callback();
1825:
1826: global $config;
1827:
1828: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1829: $recordExists = $provider->checkRecordExists($OID);
1830:
1831: if(method_exists($this, 'after_checkRecordExists_callback'))
1832: $this->after_checkRecordExists_callback();
1833:
1834: self::$logger->debug('<<checkRecordExists ['.$recordExists.']');
1835: return $recordExists;
1836: }
1837:
1838: 1839: 1840: 1841: 1842: 1843: 1844: 1845: 1846:
1847: public function isTableOverloaded() {
1848: self::$logger->debug('>>isTableOverloaded()');
1849:
1850: global $config;
1851:
1852: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $this);
1853: $isOverloaded = $provider->isTableOverloaded();
1854:
1855: self::$logger->debug('<<isTableOverloaded ['.$isOverloaded.']');
1856: return $isOverloaded;
1857: }
1858:
1859: 1860: 1861: 1862: 1863: 1864: 1865:
1866: public static function begin($BO = null) {
1867: if(self::$logger == null)
1868: self::$logger = new Logger('AlphaDAO');
1869: self::$logger->debug('>>begin()');
1870:
1871: global $config;
1872:
1873: if(isset($BO))
1874: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $BO);
1875: else
1876: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), new PersonObject());
1877:
1878: try{
1879: $provider->begin();
1880: }catch (Exception $e) {
1881: throw new AlphaException('Error beginning a new transaction, error is ['.$e->getMessage().']');
1882: }
1883:
1884: self::$logger->debug('<<begin');
1885: }
1886:
1887: 1888: 1889: 1890: 1891: 1892: 1893:
1894: public static function commit($BO = null) {
1895: if(self::$logger == null)
1896: self::$logger = new Logger('AlphaDAO');
1897: self::$logger->debug('>>commit()');
1898:
1899: global $config;
1900:
1901: if(isset($BO))
1902: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $BO);
1903: else
1904: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), new PersonObject());
1905:
1906: try{
1907: $provider->commit();
1908: }catch (Exception $e) {
1909: throw new FailedSaveException('Error commiting a transaction, error is ['.$e->getMessage().']');
1910: }
1911:
1912: self::$logger->debug('<<commit');
1913: }
1914:
1915: 1916: 1917: 1918: 1919: 1920: 1921:
1922: public static function rollback($BO = null) {
1923: if(self::$logger == null)
1924: self::$logger = new Logger('AlphaDAO');
1925: self::$logger->debug('>>rollback()');
1926:
1927: global $config;
1928:
1929: if(isset($BO))
1930: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), $BO);
1931: else
1932: $provider = AlphaDAOProviderFactory::getInstance($config->get('db.provider.name'), new PersonObject());
1933:
1934: try{
1935: $provider->rollback();
1936: }catch (Exception $e) {
1937: throw new FailedSaveException('Error aborting a transaction, error is ['.$e->getMessage().']');
1938: }
1939:
1940: self::$logger->debug('<<rollback');
1941: }
1942:
1943: 1944: 1945: 1946: 1947: 1948:
1949: public static function isInstalled() {
1950: if(self::$logger == null)
1951: self::$logger = new Logger('AlphaDAO');
1952: self::$logger->debug('>>isInstalled()');
1953:
1954: global $config;
1955:
1956: 1957: 1958: 1959: 1960: 1961:
1962: if(AlphaDAO::checkBOTableExists('PersonObject') && AlphaDAO::checkBOTableExists('RightsObject')) {
1963: self::$logger->debug('<<isInstalled [true]');
1964: return true;
1965: }else{
1966: self::$logger->debug('<<isInstalled [false]');
1967: return false;
1968: }
1969: }
1970:
1971: 1972: 1973: 1974: 1975: 1976:
1977: public function isTagged() {
1978: if(isset($this->taggedAttributes) && isset($this->tags) && $this->tags instanceof Relation)
1979: return true;
1980: else
1981: return false;
1982: }
1983:
1984: 1985: 1986: 1987: 1988: 1989:
1990: private function setVersion($versionNumber) {
1991: $this->version_num->setValue($versionNumber);
1992: }
1993:
1994: 1995: 1996: 1997: 1998: 1999: 2000: 2001: 2002: 2003:
2004: public function cast($targetClassName, $originalBO) {
2005: AlphaDAO::loadClassDef($targetClassName);
2006:
2007: $BO = new $targetClassName;
2008: $BO->setOID($originalBO->getOID());
2009: $BO->setVersion($originalBO->getVersion());
2010:
2011:
2012: $originalBOreflection = new ReflectionClass(get_class($originalBO));
2013: $originalBOproperties = $originalBOreflection->getProperties();
2014: $newBOreflection = new ReflectionClass($targetClassName);
2015: $newBOproperties = $newBOreflection->getProperties();
2016:
2017:
2018:
2019: if(count($originalBOproperties) < count($newBOproperties)) {
2020:
2021: foreach($originalBOproperties as $propObj) {
2022: $propName = $propObj->name;
2023: if(!in_array($propName, $this->transientAttributes))
2024: $BO->set($propName, $originalBO->get($propName));
2025: }
2026: }else{
2027:
2028: foreach($newBOproperties as $propObj) {
2029: $propName = $propObj->name;
2030: if(!in_array($propName, $this->transientAttributes))
2031: $BO->set($propName, $originalBO->get($propName));
2032: }
2033: }
2034:
2035: return $BO;
2036: }
2037:
2038: 2039: 2040: 2041: 2042: 2043:
2044: public function getFriendlyClassName() {
2045: $name = substr(get_class($this), 0, -6);
2046:
2047: return $name;
2048: }
2049:
2050: 2051: 2052: 2053: 2054: 2055: 2056:
2057: public function hasAttribute($attribute) {
2058: try{
2059: $exists = $this->$attribute;
2060: return true;
2061: }catch(Exception $e) {
2062: return false;
2063: }
2064: }
2065:
2066: 2067: 2068: 2069: 2070:
2071: public function addToCache() {
2072: self::$logger->debug('>>addToCache()');
2073: global $config;
2074:
2075: try {
2076: $cache = AlphaCacheProviderFactory::getInstance($config->get('cache.provider.name'));
2077: $cache->set(get_class($this).'-'.$this->getOID(), $this, 3600);
2078:
2079: }catch(Exception $e) {
2080: self::$logger->error('Error while attempting to store a business object to the ['.$config->get('cache.provider.name').']
2081: instance: ['.$e->getMessage().']');
2082: }
2083:
2084: self::$logger->debug('<<addToCache');
2085: }
2086:
2087: 2088: 2089: 2090: 2091:
2092: public function removeFromCache() {
2093: self::$logger->debug('>>removeFromCache()');
2094: global $config;
2095:
2096: try {
2097: $cache = AlphaCacheProviderFactory::getInstance($config->get('cache.provider.name'));
2098: $cache->delete(get_class($this).'-'.$this->getOID());
2099: }catch(Exception $e) {
2100: self::$logger->error('Error while attempting to remove a business object from ['.$config->get('cache.provider.name').']
2101: instance: ['.$e->getMessage().']');
2102: }
2103:
2104: self::$logger->debug('<<removeFromCache');
2105: }
2106:
2107: 2108: 2109: 2110: 2111: 2112:
2113: public function loadFromCache() {
2114: self::$logger->debug('>>loadFromCache()');
2115: global $config;
2116:
2117: try {
2118: $cache = AlphaCacheProviderFactory::getInstance($config->get('cache.provider.name'));
2119: $BO = $cache->get(get_class($this).'-'.$this->getOID());
2120:
2121: if(!$BO) {
2122: self::$logger->debug('Cache miss on key ['.get_class($this).'-'.$this->getOID().']');
2123: self::$logger->debug('<<loadFromCache: [false]');
2124: return false;
2125: }else{
2126:
2127: $reflection = new ReflectionClass(get_class($this));
2128: $properties = $reflection->getProperties();
2129:
2130: foreach($properties as $propObj) {
2131: $propName = $propObj->name;
2132:
2133:
2134: if(!in_array($propName, $this->transientAttributes)) {
2135: $this->set($propName, $BO->get($propName, true));
2136: }elseif(!$propObj->isPrivate() && isset($this->$propName) && $this->$propName instanceof Relation) {
2137: $prop = $this->getPropObject($propName);
2138:
2139:
2140: if($prop->getRelationType() == 'ONE-TO-MANY') {
2141: $this->set($propObj->name, $this->getOID());
2142: }
2143: }
2144: }
2145:
2146: self::$logger->debug('<<loadFromCache: [true]');
2147: return true;
2148: }
2149: }catch(Exception $e) {
2150: self::$logger->error('Error while attempting to load a business object from ['.$config->get('cache.provider.name').']
2151: instance: ['.$e->getMessage().']');
2152:
2153: self::$logger->debug('<<loadFromCache: [false]');
2154: return false;
2155: }
2156: }
2157:
2158: 2159: 2160: 2161: 2162: 2163:
2164: public function setLastQuery($query) {
2165: self::$logger->sql($query);
2166: $this->lastQuery = $query;
2167: }
2168:
2169: 2170: 2171: 2172: 2173: 2174:
2175: public function __wakeup() {
2176: if(self::$logger == null)
2177: self::$logger = new Logger(get_class($this));
2178: }
2179:
2180: 2181: 2182: 2183: 2184: 2185: 2186:
2187: public function setMaintainHistory($maintainHistory) {
2188: if(!is_bool($maintainHistory))
2189: throw new IllegalArguementException('Non-boolean value ['.$maintainHistory.'] passed to setMaintainHistory method!');
2190:
2191: $this->maintainHistory = $maintainHistory;
2192: }
2193:
2194: 2195: 2196: 2197: 2198: 2199:
2200: public function getMaintainHistory() {
2201: return $this->maintainHistory;
2202: }
2203: }
2204:
2205: ?>