• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List

E:/E/GEAMP/www/openbiz/openbiz/bin/data/private/BizRecord.php

00001 <?php
00024 class BizRecord extends MetaIterator
00025 {
00030     protected $m_KeyFldColMap = array();
00035     protected $m_ColFldMap = array();
00040     public $m_InputFields;
00041     
00042     protected $m_IgnoreInQuery = false;
00043 
00052     function __construct(&$xmlArr, $className, $parentObj=null)
00053     {
00054         parent::__construct($xmlArr, $className, $parentObj);
00055         $this->_initSetup();
00056     }
00057 
00064     public function merge(&$anotherMIObj)
00065     {
00066         parent::merge($anotherMIObj);
00067         $this->_initSetup();
00068     }
00069 
00075     private function _initSetup()
00076     {
00077         unset($this->m_ColFldMap); $this->m_ColFldMap = array();
00078         unset($this->m_KeyFldColMap); $this->m_KeyFldColMap = array();
00079         $i = 0;
00080         // generate column index if the column is one of the basetable (m_Column!="")
00081         foreach($this->m_var as $key=>$field)
00082         {  // $key is fieldname, $field is fieldobj
00084         // TODO: join fields and nonjoin fields may have same column name
00086             if ($field->m_Column && !$field->m_Join)  // ignore the joined field column
00087                 $this->m_ColFldMap[$field->m_Column] = $key;
00088             if ($field->m_Column || $field->m_SqlExpression)
00089             {
00090                 $field->m_Index = $i++;
00091             }
00092         }
00093         // create key field column map to support composite key
00094         if (isset($this->m_var["Id"]) && $this->m_var["Id"]->m_Column)
00095         {
00096             $keycols = explode(",", $this->m_var["Id"]->m_Column);
00097             foreach ($keycols as $col)
00098             {
00099                 $field = $this->getFieldByColumn($col);  // main table
00100                 $this->m_KeyFldColMap[$field] = $col;
00101             }
00102         }
00103     }
00104 
00112     public function getFieldByColumn($column, $table=null)
00113     {
00114         // TODO: 2 columns can have the same name in case of joined fields
00115         if(array_key_exists($column, $this->m_ColFldMap))
00116             return $this->m_ColFldMap[$column];
00117         return null;
00118     }
00119 
00125     final public function getEmptyRecordArr()
00126     {
00127         $recArr = array();
00128         foreach ($this->m_var as $key=>$field)
00129         {
00130             $recArr[$key] = $field->getDefaultValue();
00131         }
00132         return $recArr;
00133     }
00134 
00143     final public function getKeyValue($isUseOldValue=false)
00144     {
00145         $keyValue = "";
00146         foreach($this->m_KeyFldColMap as $fieldName=>$colName)
00147         {
00148             $val = $isUseOldValue ? $this->m_var[$fieldName]->m_OldValue : $this->m_var[$fieldName]->m_Value;
00149             if ($keyValue == "")
00150                 $keyValue .= $val;
00151             else
00152             // use base64 (a-zA-Z1-9+-) to encode the key and connect them with "#"
00153                 $keyValue .= CK_CONNECTOR . $val;
00154         }
00155         return $keyValue;
00156     }
00157 
00163     final public function getKeyFields()
00164     {
00165         $keyFields = array();
00166         foreach($this->m_KeyFldColMap as $fieldName=>$colName)
00167         {
00168             $keyFields[$fieldName] = $this->m_var[$fieldName];
00169         }
00170         return $keyFields;
00171     }
00172 
00181     public function getKeySearchRule($isUseOldValue=false, $isUseColumnName=false)
00182     {
00183         $keyFields = $this->getKeyFields();
00184         $retStr = "";
00185         foreach ($keyFields as $fieldName=>$fieldObj)
00186         {
00187             if ($retStr != "") $retStr .= " AND ";
00188             $lhs = $isUseColumnName ? $fieldObj->m_Column : "[$fieldName]";
00189             $rhs = $isUseOldValue ? $fieldObj->m_OldValue : $fieldObj->m_Value;
00190             if ($rhs == "")
00191                 $retStr .= "(".$lhs."='".$rhs."' or ".$lhs." is null)";
00192             else
00193                 $retStr .= $lhs."='".$rhs."'";
00194         }
00195         return $retStr;
00196     }
00197 
00204     public function setRecordArr($recArr)
00205     {
00206         if (!$recArr) return;
00207         foreach ($this->m_var as $key=>$field)
00208         {
00209             if (key_exists($key, $recArr)){
00210                                    $recArr[$key] = $field->setValue($recArr[$key]);
00211             }
00212         }
00213     }
00214 
00221     final public function setInputRecord(&$inputArr)
00222     {
00223         // unformat the inputs
00224         unset($this->m_InputFields);
00225         foreach($inputArr as $key=>$value)
00226         {   
00227             // if allow changing key field, need to keep the old value which is also useful for audit trail
00228             // if (!$value)
00229             //    continue;
00230             $bizField = $this->m_var[$key];
00231             if (!$bizField) continue;
00232 
00233             $realVal = BizSystem::typeManager()->formattedStringToValue($bizField->m_Type, $bizField->m_Format, $value);
00234             if(strtoupper($bizField->m_Encrypted)=='Y'){
00235                                    $svcobj = BizSystem::getService(CRYPT_SERVICE);                              
00236                                    $realVal = $svcobj->encrypt($realVal);
00237                      $bizField->setValue($realVal);
00238             }
00239             // todo: need to optimize on lob column            
00240             $bizField->setValue($realVal);
00241             
00242             $this->m_InputFields[] = $key;
00243         }
00244         //$this->m_var["Id"]->setValue($this->getKeyValue());
00245     }
00246 
00254     final public function saveOldRecord(&$inputArr)
00255     {
00256         if (!$inputArr)
00257             return;
00258         foreach($inputArr as $key=>$value)
00259         {
00260             $bizField = $this->m_var[$key];
00261             if (!$bizField) continue;
00262             $bizField->saveOldValue($value);
00263         }
00264     }
00265 
00272     final public function getRecordArr($sqlArr=null)
00273     {
00274         if ($sqlArr)
00275             $this->_setSqlRecord($sqlArr);
00276         $recArr = array();
00277         foreach ($this->m_var as $key=>$field){
00278               if(strtoupper($field->m_Encrypted)=='Y'){
00279               $svcobj = BizSystem::getService(CRYPT_SERVICE);         
00280                      $value = $svcobj->decrypt($field->getValue()); 
00281               $recArr[$key] = $value;
00282               }else{
00283                      $recArr[$key] = $field->getValue();       
00284               }
00285         }
00286         return $recArr;
00287     }
00288 
00295     public function convertSqlArrToRecArr($sqlArr)
00296     {
00297         $recArr = array();
00298         foreach ($this->m_var as $key=>$field)
00299         {
00300             if ($field->m_Column || $field->m_SqlExpression)
00301             {
00302                 $recArr[$key] = $sqlArr[$field->m_Index];
00303             }
00304             else
00305                 $recArr[$key] = "";
00306         }
00307         return $recArr;
00308     }
00309 
00316     private function _setSqlRecord($sqlArr)
00317     {
00318         foreach ($this->m_var as $key=>$field)
00319         {
00320             if ($field->m_Column || $field->m_SqlExpression)
00321             {
00322                 $field->setValue($sqlArr[$field->m_Index]);
00323             }
00324         }
00325         if (isset($this->m_var["Id"]))
00326             $this->m_var["Id"]->setValue($this->getKeyValue());
00327     }
00328 
00335     public function getJoinInputRecord($join)
00336     {
00337         $inputFields = $this->m_InputFields;  // Added by Jixian on 2009-02-15 for implement onSaveDataObj
00338         $recArr = array();
00339         foreach ($this->m_var as $key=>$value)
00340         {
00341             // do not consider joined columns
00342             // Added by Jixian on 2009-02-15 for implement onSaveDataObj
00343             /*
00344              * It's the time to consider about joined columns
00345              */
00346 
00347             $field = $value;
00348 
00349             if ($field->m_Join == $join)
00350             {
00351                 $recArr[$key] = $value;
00352             }
00353         }
00354         return $recArr;
00355     }
00356 
00357     
00367     public function getJoinSearchRule($tableJoin, $isUseOldValue=false)
00368     {
00369         $joinFieldName = $this->getFieldByColumn($tableJoin->m_ColumnRef, $tableJoin->m_Table);
00370         $joinField=$this->m_var[$joinFieldName];
00371         $rhs = $isUseOldValue ? $joinField->m_OldValue : $joinField->m_Value;
00372         $retStr = $tableJoin->m_Column . "='" . $rhs . "'";
00373         return $retStr;
00374     }
00375 
00382     final public function getToSaveFields($type)
00383     {
00384         // TODO: if join != null, get columns only for the join
00385         $sqlFields = array();
00386 
00387         // expand input fields with oncreate or onupdate fields
00388         $inputFields = $this->m_InputFields;
00389         foreach ($this->m_var as $key=>$field)
00390         {
00391             if (($type=='UPDATE' && $field->m_ValueOnUpdate != null)
00392                 || ($type=='CREATE' && $field->m_ValueOnCreate != null))
00393             {
00394                 if (!in_array($key, $this->m_InputFields))
00395                     $inputFields[] = $key;
00396             }
00397         }
00398 
00399         foreach ($inputFields as $key)
00400         {
00401             // ignore the composite key Id field
00402             if ($key == "Id" && count($this->m_KeyFldColMap) > 1)
00403                 continue;
00404             $field = $this->m_var[$key];
00405             // do not consider joined columns
00406             if ($field->m_Column && !$field->m_Join)
00407             {
00408                 $sqlFields[] = $field;
00409             }
00410         }
00411         return $sqlFields;
00412     }
00413 }
00414 ?>

Generated on Thu Apr 19 2012 17:09:13 for openbiz by  doxygen 1.7.2