00001 <?php
00002
00003 class XMLConfigLoader
00004 {
00005 private $broker;
00006 private $xmlMaps = array();
00007 private $classMaps = array();
00008
00009 public function __construct(PersistentManagerFactory $broker)
00010 {
00011 $this->broker = $broker;
00012 }
00013
00014 private function getAsArray($object)
00015 {
00016 return (is_array($object)) ? $object : array($object);
00017 }
00018
00019 public function getClassMap($module, $name, $associative=FALSE)
00020 {
00021 global $MIOLO;
00022
00023 if (isset($this->classMaps[$module][$name]))
00024 {
00025 return $this->classMaps[$module][$name];
00026 }
00027 if (isset($this->xmlMaps[$module][$name]))
00028 {
00029 $xml = $this->xmlMaps[$module][$name];
00030 }
00031 else
00032 {
00033 $file = $MIOLO->GetModulePath($module,'db/map/'.$name.'.xml');
00034 if (! file_exists( $file ))
00035 {
00036 $file = $MIOLO->GetModulePath($module,'classes/map/'.$name.'.xml');
00037 }
00038 $xmlTree = new MXMLTree($file);
00039 $tree = $xmlTree->getTree();
00040 $xml = $xmlTree->getXMLTreeElement($tree);
00041 $this->xmlMaps[$module][$name] = $xml;
00042 if (!$associative)
00043 {
00044 $MIOLO->UsesBusiness($module,$name);
00045 }
00046 }
00047
00048 $database = (string)$xml->databaseName;
00049 $className = (string)$xml->moduleName . (string)$xml->className;
00050 $cm = new ClassMap($className, $database, $this->broker);
00051 $dm = new DatabaseMap((string)$xml->databaseName);
00052 $tableMap = new TableMap();
00053 $tableMap->setName((string)$xml->tableName);
00054 $tableMap->setDatabaseMap($dm);
00055 if (isset($xml->extends))
00056 {
00057 $superClassName = (string)$xml->extends->moduleName . (string)$xml->extends->className;
00058 $cm->setSuperClass($superClassName, $this->broker);
00059 }
00060 $attributes = $this->getAsArray($xml->attribute);
00061 foreach($attributes as $attr)
00062 {
00063 $am = new AttributeMap((string)$attr->attributeName);
00064 $converter = $this->getConverter($attr->converter);
00065 if (isset($attr->attributeIndex))
00066 {
00067 $am->setIndex($attr->attributeIndex);
00068 }
00069 if (isset($attr->columnName))
00070 {
00071 $colm = new ColumnMap($attr->columnName, $tableMap, $converter);
00072
00073 if (isset($attr->key))
00074 {
00075 if ($attr->key == 'primary')
00076 {
00077 $colm->setKeyType('primary');
00078 if (isset($attr->idgenerator))
00079 {
00080 $idGenerator = $attr->idgenerator;
00081 $colm->setIdGenerator($idGenerator);
00082 }
00083 }
00084 elseif ($attr->key == 'foreign')
00085 {
00086 $colm->setKeyType('foreign');
00087 }
00088 }
00089 else
00090 {
00091 $colm->setKeyType('none');
00092 }
00093 $am->setColumnMap($colm);
00094 }
00095 $am->setProxy($attr->proxy);
00096 if ((isset($attr->reference)) && ($cm->getSuperClass() != NULL) )
00097 {
00098 $referenceAttribute = $cm->getSuperClass()->getAttributeMap($attr->reference);
00099 if ($referenceAttribute)
00100 $am->setReference($referenceAttribute);
00101 }
00102 if ($attr->attributeName == 'timestamp')
00103 {
00104 $cm->setTimestampAttributeMap($am);
00105 }
00106 else
00107 {
00108 $cm->addAttributeMap($am);
00109 }
00110 }
00111 $this->classMaps[$module][$name] = $cm;
00112
00113
00114 if (isset($xml->association))
00115 {
00116 $fromClassMap = $cm;
00117 $associations = $this->getAsArray($xml->association);
00118 foreach($associations as $assoc)
00119 {
00120 $toModule = $assoc->toClassModule;
00121 $toName = $assoc->toClassName;
00122 $toClassMap = $this->getClassMap($toModule, $toName);
00123 $am = new UniDirectionalAssociationMap();
00124 $am->setForClass($toClassMap);
00125 $am->setTargetName($assoc->target);
00126 $am->setTarget($fromClassMap->getAttributeMap($assoc->target));
00127 $am->setDeleteAutomatic($assoc->deleteAutomatic);
00128 $am->setSaveAutomatic($assoc->saveAutomatic);
00129 $am->setRetrieveAutomatic($assoc->retrieveAutomatic);
00130 $am->setJoinAutomatic($assoc->joinAutomatic);
00131 if (isset($assoc->indexAttribute))
00132 {
00133 $am->setIndexAttribute($assoc->indexAttribute->indexAttributeName);
00134 }
00135 $am->setInverse($assoc->inverse);
00136 $am->setCardinality($assoc->cardinality);
00137 if ($assoc->cardinality == 'manyToMany')
00138 {
00139 $associativeModule = $assoc->associativeClassModule;
00140 $associativeName = $assoc->associativeClassName;
00141 $associativeClassMap = $this->getClassMap($associativeModule, $associativeName, true);
00142 $am->setAssociativeClass($associativeClassMap);
00143 foreach($assoc->direction as $direction)
00144 {
00145 $am->addDirection($direction);
00146 }
00147 }
00148 else
00149 {
00150 $entries = $this->getAsArray($assoc->entry);
00151 foreach($entries as $entry)
00152 {
00153 $fromAttribute = $entry->fromAttribute;
00154 $toAttribute = $entry->toAttribute;
00155 if ($am->isInverse())
00156 {
00157 $e = new UDAMapEntry($toClassMap->getAttributeMap($fromAttribute), $fromClassMap->getAttributeMap($toAttribute));
00158 }
00159 else
00160 {
00161 $e = new UDAMapEntry($fromClassMap->getAttributeMap($fromAttribute), $toClassMap->getAttributeMap($toAttribute));
00162 }
00163 $am->addEntry($e);
00164 }
00165 }
00166 if (isset($assoc->orderAttribute))
00167 {
00168 $orderEntry = array();
00169 $orderAttributes = $this->getAsArray($assoc->orderAttribute);
00170 foreach($orderAttributes as $order)
00171 {
00172 $ascend = ($order->orderAttributeDirection == 'ascend');
00173
00174 $attributeMap = $am->getForClass()->getAttributeMap($order->orderAttributeName);
00175 $orderEntry[] = new OrderEntry($attributeMap, $ascend);
00176 }
00177 if (count($orderEntry))
00178 {
00179 $am->setOrderAttributes($orderEntry);
00180 }
00181 }
00182 $fromClassMap->putAssociationMap($am);
00183 }
00184 }
00185
00186 return $cm;
00187 }
00188
00189 public function getConverter($converterNode)
00190 {
00191 if (!$converterNode)
00192 {
00193 $converter = ConverterFactory::getTrivialConverter();
00194 }
00195 else
00196 {
00197 $name = $converterNode->converterName;
00198 $converter = $this->broker->getConverter($name);
00199 if (!$converter)
00200 {
00201 $parameters = $this->getParameters($converterNode);
00202 $factory = new ConverterFactory();
00203 $converter = $factory->getConverter($name, $parameters);
00204 $this->broker->putConverter($name, $converter);
00205 }
00206 }
00207 return $converter;
00208 }
00209
00210 public function getParameters($node = NULL)
00211 {
00212 $param = NULL;
00213 if ($node)
00214 {
00215 $parameters = $this->getAsArray($node->parameter);
00216 foreach($parameters as $parameter)
00217 {
00218 $param[$parameter->parameterName] = $parameter->parameterValue;
00219 }
00220 }
00221 return $param;
00222 }
00223 }
00224 ?>