Blame view

php/classes/AmdaObjectMgr.php 10.6 KB
16035364   Benjamin Renard   First commit
1
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
 * @class AmdaObjectMgr 
 * @version $Id: AmdaObjectMgr.php 2891 2015-04-30 11:31:51Z elena $
 * 
 */

 class AmdaObjectMgr {

      protected $xmlName, $xp;      
      protected $attributes, $optionalAttributes;       
      protected $contentRootId, $objTagName;
      protected $descFileName, $resFileName;
      protected $id_prefix;
      protected $contentRootTag;
      protected $obj, $id;
      protected $types;

      public $contentDom; 
      public $objectDom;

      protected function __construct($xmlFile) {
// content XML
		$this->xmlName = USERWSDIR."/".$xmlFile;
		$this->contentDom = new DomDocument("1.0");
		$this->contentDom->preserveWhiteSpace = false;
		$this->contentDom->formatOutput = true;

		if (file_exists($this->xmlName)) {
		    $this->contentDom -> load($this->xmlName);	 
		    $this->xp = new domxpath($this->contentDom); 
		}
// object desc XML
//TODO not used in RequestMgr
		$this->objectDom = new DomDocument("1.0"); 
		$this->objectDom->preserveWhiteSpace = false;
		$this->objectDom->formatOutput = true;
      }
   
     private function getNewId() {                   
      //  Get all ID attributes
	   $elements = $this->xp->query("//".$this->objTagName."/@xml:id");

      // Now find New Valid ID  
		if ($elements->length  > 0) {
		      if($elements->length > 0) for ($i = 0; $i < $elements->length; $i++) {
							     $id =  explode('_',$elements->item($i)->nodeValue);
						             $idList[] = $id[1]; 
			}

			sort($idList);

			for ($i = 0; $i < count($idList); $i++) {
			      if ($idList[$i] > $i) { 
				      $newID = $i;
				      break;
				}  
			      $newID = $i+1;  
			    }                 
		  } else
			  $newID = 0;
	    return $newID;
      }

	protected function setId() { 
                $id_ = $this->getNewId();
		if ($id_ === false) return false;
		$this->id = $this->id_prefix.$id_;
		return $this->id;         
	} 
   
	protected function objectExistsByName($name){
                $this->obj = $this->xp->query("//".$this->objTagName."[@name='".$name."']");		 
		if ($this->obj->length != 0) return true;
		return false;
	} 

	protected function getObjectIdByName($name) {
		$this->obj = $this->xp->query("//".$this->objTagName."[@name='".$name."']");
                if ($this->obj->length == 0) return false; 
		    $id = $this->obj->item(0)->getAttribute('xml:id');
                if ($id) return $id;
		return false;           
	}
	
	protected function folderExistsByName($name){
                $this->obj = $this->xp->query("//".$this->contentRootTag."/folder[@name='".$name."']");		 
		if ($this->obj->length != 0) return true;
		return false;
	}  

	protected function objectExistsById($id){      
		$this->obj = $this->contentDom->getElementById($id); 
		if ($this->obj != null) return $this->obj;
		return false;
        } 
      
         protected function createObjectResource(){}
         protected function renameInResource(){}

/*
*  Write Object into desc file
*/
	protected function createObjectDescription($obj){
	      $root = $this->objectDom->createElement($this->objTagName); 
	      $root->setAttribute('xml:id',$this->id);
          
	      foreach($obj as $key => $value) {
		if ($key != 'id' && $key != 'leaf' && $key != 'nodeType') {	
		    $node =  $this->objectDom->createElement($key,htmlspecialchars($value)); 
		    $root -> appendChild($node);
		}
	      }      
// add Optional Attributes if they are undefined	      
              foreach ($this->optionalAttributes as $key => $value)   
		 if ($root->getElementsByTagName($key)->length == 0) {
                        $node =  $this->objectDom->createElement($key,htmlspecialchars($value));  
			$root -> appendChild($node);
                  }
 
	      $this->objectDom->appendChild($root);
	      $this->objectDom->save($this->descFileName);  
      }
/*
*    Just Save  Content XML
*/
     protected function saveContent() {
	      $this->contentDom->save($this->xmlName);
      }
    
/*
* Add Object to Content XML
*/
      protected function addToContent($obj, $folder) {
              
                $folderToAdd = null;

		$objList = $this->contentDom->getElementById($this->contentRootId); 		
                $newObj = $this->contentDom->createElement($this->objTagName);
                $newObj->setAttribute('xml:id',$this->id);
// object to mapped array
                $obj_arr = (array)$obj;
                foreach ($this->attributes as $key => $value) {
                          $newObj->setAttribute($key, $obj_arr[$key]);	 
                }              
	        if ($folder != null)  
                          $folderToAdd = $this->contentDom->getElementById($folder);

		if ($folderToAdd) $folderToAdd -> appendChild($newObj);
                else $objList -> appendChild($newObj);
		  

		$this->saveContent();
      }
     
/*
* Delete Object From Content XML
*/
      protected function deleteFromContent($obj) {
		$objList = $obj -> parentNode;// $this->contentDom->getElementById($this->contentRootId);
                $objList -> removeChild($obj);
		$this->saveContent();
      }

/*
*         Create Folder
*/
      protected function createFolder($obj){
                 if ($this -> folderExistsByName($obj->name)) return array('error' => NAME_EXISTS);
                 $newFolder = $this->contentDom->createElement('folder');
		 $id = $obj->name."_".$this->objTagName;
		 $newFolder -> setAttribute('xml:id',$id); 
                 $newFolder -> setAttribute('name',$obj->name);
		 $objList = $this->contentDom->getElementById($obj->parent);
                 $objList -> appendChild($newFolder);
		 $this -> saveContent();
		return array('id' => $id);          
      }
/*
*         Get Folder of the object 
*/
      protected function getObjectFolder($id) {

                if (!($obj = $this->objectExistsById($id))) return "NO_SUCH_ID";
		if ($obj->parentNode->tagName == 'folder') return $obj->parentNode->getAttribute('xml:id');
		return null;

      }
         
	protected function setAlias($chain) {

		$aliasMgr = new AliasMgr();		 
		$listeAlias = $aliasMgr->getList();

		foreach($listeAlias as $alias) {
			$chain = $aliasMgr->substrParamAlias($chain, $alias->getAttribute("xml:id"),$alias->getAttribute("name"));
		}
        	
		return $chain;
	}	

	protected function resetAlias($chain) {

		$aliasMgr = new AliasMgr();		 
		$listeAlias = $aliasMgr->getList();

		foreach($listeAlias as $alias) {
			$chain = $aliasMgr->substrAliasParam($chain, $alias->getAttribute("xml:id"),$alias->getAttribute("name"));
		}
        	
		return $chain;
	}	
	
	protected function createDom() {
		 
		$rootElement = $this->contentDom->createElement('ws');
                foreach ($this->types as $type) {
		    $contentId = $type.'-treeRootNode';
		    $contentTag = $type.'List';
		    $typeElement = $this->contentDom->createElement($contentTag);
		    $typeElement->setAttribute('xml:id', $contentId);
		    $rootElement->appendChild($typeElement);
		}
                $this->contentDom->appendChild($rootElement);
		$this->contentDom->save($this->xmlName);
        }

  
/*****************************************************************
*                           PUBLIC FUNCTIONS
*****************************************************************/  
	
/*
*        Create Parameter[TT...]/Folder
*        create object itself, add it to content DOM
*/   
      function createObject($p, $folder){

	      if ($p -> leaf) return $this->createParameter($p, $folder);
	//      else return $this->createFolder($p);
//TODO check if this is possible?
	      else return array('error' => 'createFolder should be called from RENAME');

      }

/*
*          Rename Parameter[TT...]/Folder
*/
      function renameObject($p){

		if (!($objToRename = $this -> objectExistsById($p->id))) {
// NO SUCH ID:  leaf -> error;
			   if ($p->leaf) return array('error' => NO_SUCH_ID);
// NO SUCH ID:  folder  -> create  
			    return  $this -> createFolder($p); 			    
                 }
// object was just DD in the tree : move tag in xml
		if ($p -> name == $p -> old_name) {
		    if (!($parentNode = $this->contentDom->getElementById($p -> parent))) return array('error' => NO_SUCH_PARENT_ID);   
		    $parentNode -> appendChild($objToRename);  
		    $this->saveContent();
		    return array('id' => $p->id);
		 }

//		if ($this -> objectExistsByName($p->name, $this->objTagName)) return array('error' => NAME_EXISTS);
//TODO  CHECK: With some PHP versions setAttribute ADD attribute
		if ($objToRename->hasAttribute('name')) $objToRename->removeAttribute('name');	
                $objToRename->setAttribute('name',$p->name);
		if (!$p->leaf) {		 
		    $objToRename->removeAttribute('xml:id');
		    $objToRename->setAttribute('xml:id', $p->name.'_'.$this->objTagName);
		  }
		else  
                    $this -> renameInResource($p->name, $p->id); 

		 $this->saveContent();
		 return array('id' => $p->id);		
	}
   
/*
*          Delete Parameter[TT...]/Folder
*    	   Delete object itself, delete from contentDOM, mark as undefined in depending objects		
*/
       function deleteObject($p){

		  if ($p->leaf) {       
//   if Parameter[TT...] - delete resources first
                         $isDeleted = $this->deleteParameter($p->id);
	 		 if (!($objToDelete = $this -> objectExistsById($p->id))) return array('error' => NO_SUCH_ID);		                         
                  }
		  else {
			if (!($objToDelete = $this -> objectExistsById($p->id))) return array('error' => NO_SUCH_ID); 
			$folderToDelete = $objToDelete->getElementsByTagName($this->objTagName);
// delete all parameters[TT..] in folder(s)
			foreach ($folderToDelete as $obj) {
			    $id = $obj->getAttribute('xml:id');
			    $this->deleteParameter($id);
                        }
		  }
		  $this -> deleteFromContent($objToDelete);

		if ($isDeleted) return array('id' => $p->id, 'maskDeleted' => true); 
		else return array('id' => $p->id);
      }

/*
*         Modify Parameter[TT...]/Folder
*/ 
     
      function modifyObject($p){ 
            if ($this->renameOnly($p)) { 
			$p->leaf = 1;
                        return $this->renameObject($p);
	      } 
	    $folder = $this->getObjectFolder($p->id);
            $this->deleteObject($p);	    	     
	    return 
		    $this->createObject($p, $folder);
         } 

      
      function validNameObject($p){
      	if (!isset($p->name) || ($p->name == ""))
      		return array('valid' => false, 'error' => 'Name is required');
      	if ($p->leaf)
      		$alreadyExist = $this -> objectExistsByName($p->name, $this->objTagName);
      	else
      		$alreadyExist = $this -> folderExistsByName($p->name);
      	if ($alreadyExist)
      		return array('valid' => false, 'error' => 'This name already exists in this subtree!');
      	return array('valid' => true);
      }
         
      function getObject($id){}
16035364   Benjamin Renard   First commit
335
336
337
 
}
?>