Commit ba9b1b7d6bad5ad4dbf8d5b8c38a8f04d166dc26

Authored by Benjamin Renard
1 parent 342b20ca

Rework of TT and Catalog cache

js/app/views/CatalogUI.js
... ... @@ -333,12 +333,12 @@ Ext.define('amdaUI.CatalogUI', {
333 333  
334 334 Ext.Array.each(result.parameters, function(obj, index) {
335 335 var field = {
336   - name: 'param'+(index+2)
  336 + name: obj.id
337 337 };
338 338 var column = {
339 339 text: obj.name,
340 340 sortable : true,
341   - dataIndex: 'param'+(index+2),
  341 + dataIndex: obj.id,
342 342 menuDisabled: false,
343 343 listeners: {
344 344 'sortchange' : function(ct, column, direction, eOpts) {
... ...
php/classes/CatalogCacheIntervalObject.php
... ... @@ -7,30 +7,19 @@ class CatalogCacheIntervalObject extends TimeTableCacheIntervalObject
7 7  
8 8 public function toArray()
9 9 {
10   - $result = array(
11   - "cacheId" => $this->id,
12   - "start" => $this->getStartToISO(),
13   - "stop" => $this->getStopToISO()
14   - );
15   - if ($this->isNew)
16   - $result["isNew"] = true;
17   -
18   - if ($this->isModified)
19   - $result["isModified"] = true;
20   -
21   - for ($i = 0; $i < count($this->params); $i++)
  10 + $result = parent::toArray();
  11 +
  12 + foreach ($this->cacheObject->getParametersInfo() as $parameter)
22 13 {
23   - $paramObject = array();
24   - $index = 'param'.sprintf("%d",$i+2);
25   - $result[$index] = $this->params[$i];
  14 + $result[$parameter['id']] = $this->params[$parameter['id']];
26 15 }
27 16 return $result;
28 17 }
29 18  
30 19 // for catalog
31   - public function setParams($params)
  20 + public function setParamValue($param_id, $param_value)
32 21 {
33   - $this->params = $params;
  22 + $this->params[$param_id] = $param_value;
34 23 }
35 24  
36 25 public function getParams()
... ... @@ -38,62 +27,113 @@ class CatalogCacheIntervalObject extends TimeTableCacheIntervalObject
38 27 return $this->params;
39 28 }
40 29  
41   - public function writeBin($handle, $paramsNumber, $paramsSizes, $paramsTypes)
  30 + public function writeBin($handle)
42 31 {
43   - fwrite($handle,pack('L6',$this->id,$this->index,$this->start,$this->stop,$this->isNew,$this->isModified));
44   - for ($i = 0; $i < $paramsNumber; $i++)
  32 + parent::writeBin($handle);
  33 +
  34 + foreach ($this->cacheObject->getParametersInfo() as $parameter)
45 35 {
46   - if ($paramsTypes[$i] == '2') // string
47   - {
48   - fwrite($handle,pack('d', strlen($this->params[$i])));
49   - fwrite($handle, $this->params[$i],strlen($this->params[$i]));
  36 + $size = $parameter['size'];
  37 + $values = $this->params[$parameter['id']];
  38 + if ($size > 1) {
  39 + $values_array = explode(',',$values);
  40 + }
  41 + else {
  42 + $values_array = array($values);
50 43 }
51   - else
52   - {
53   - if ($paramsTypes[$i] == '1')
54   - $paramString = TimeUtils::iso2stamp($this->params[$i]);
55   - else
56   - $paramString = $this->params[$i];
57   -
58   - $paramArray = explode(',',$paramString);
59   - for ($j = 0; $j < $paramsSizes[$i]; $j++) fwrite($handle,pack('d', $paramArray[$j]));
  44 + for ($i = 0; $i < $size; ++$i) {
  45 + $value = ($i >= count($values_array)) ? "" : $values_array[$i];
  46 + switch ($parameter['type']) {
  47 + case 0: //double
  48 + fwrite($handle,pack('d', floatval($value)));
  49 + break;
  50 + case 1: //date (timestamp)
  51 + fwrite($handle,pack('L', TimeUtils::iso2stamp($value)));
  52 + break;
  53 + case 2: //string
  54 + fwrite($handle,pack('L', strlen($value)));
  55 + for ($j = 0; $j < strlen($value); ++$j) {
  56 + fwrite($handle,pack('C', ord($value[$j])));
  57 + }
  58 + break;
  59 + case 3: //int
  60 + fwrite($handle,pack('L', intval($value)));
  61 + break;
  62 + default: // not defined => string
  63 + fwrite($handle,pack('L', strlen($value)));
  64 + for ($j = 0; $j < strlen($value); ++$j) {
  65 + fwrite($handle,pack('C', ord($value[$j])));
  66 + }
  67 + }
60 68 }
61 69 }
62 70 }
63 71  
64   - public function loadBin($handle, $paramsNumber, $paramsSizes, $paramsTypes)
  72 + public function loadBin($handle)
65 73 {
66   - $array = unpack('L6int',fread($handle,6*4));
67   - $this->id = $array['int1'];
68   - $this->index = $array['int2'];
69   - $this->start = $array['int3'];
70   - $this->stop = $array['int4'];
71   - $this->isNew = $array['int5'];
72   - $this->isModified = $array['int6'];
73   -
74   - for ($i = 0; $i < $paramsNumber; $i++) {
75   - $this->params[$i] = null;
76   -
77   - for ($j = 0; $j < $paramsSizes[$i]; $j++)
78   - {
79   - $val = unpack('dval',fread($handle,8));
  74 + parent::loadBin($handle);
80 75  
81   - if ($paramsTypes[$i] == '2') // string
82   - {
83   - $this->params[$i] = fread($handle,$val['val']);
84   - }
85   - else
86   - {
87   - if ($paramsTypes[$i] == '1')
88   - $this->params[$i] .= TimeUtils::stamp2iso($val['val']);
89   - else
90   - $this->params[$i] .= $val['val'];
91   -
92   - if ($j != $paramsSizes[$i] - 1) $this->params[$i] .= ',';
  76 + foreach ($this->cacheObject->getParametersInfo() as $parameter) {
  77 + $size = $parameter['size'];
  78 + $id = $parameter['id'];
  79 +
  80 +
  81 + $values = array();
  82 + for ($i = 0; $i < $size; $i++)
  83 + {
  84 + $val = "";
  85 + switch ($parameter['type']) {
  86 + case 0: //double
  87 + if (!$res = unpack('dval',fread($handle,8))) {
  88 + break;
  89 + }
  90 + $val = $res['val'];
  91 + break;
  92 + case 1: //date (timestamp)
  93 + if (!$res = unpack('Lval',fread($handle,4))) {
  94 + break;
  95 + }
  96 + $val = $res['val'];
  97 + break;
  98 + case 2: //string
  99 + if (!$res = unpack('Llength',fread($handle,4))) {
  100 + break;
  101 + }
  102 + $length = $res['length'];
  103 + $val = "";
  104 + for ($j = 0; $j < $length; ++$j) {
  105 + if (!$res = unpack('Cval',fread($handle,1))) {
  106 + break;
  107 + }
  108 + $val .= chr($res['val']);
  109 + }
  110 +
  111 + break;
  112 + case 3: //int
  113 + if (!$res = unpack('Lval',fread($handle,4))) {
  114 + break;
  115 + }
  116 + $val = $res['val'];
  117 + break;
  118 + default: // not defined => string
  119 + if (!$res = unpack('Llength',fread($handle,4))) {
  120 + break;
  121 + }
  122 + $length = $res['length'];
  123 + $val = "";
  124 + for ($j = 0; $j < $length; ++$j) {
  125 + if (!$res = unpack('Cval',fread($handle,1))) {
  126 + break;
  127 + }
  128 + $val .= chr($res['val']);
  129 + }
93 130 }
  131 +
  132 + $values[] = $val;
94 133 }
95   - }
96 134  
  135 + $this->params[$id] = implode(',',$values);
  136 + }
97 137 }
98 138  
99 139 public function dump()
... ... @@ -101,7 +141,7 @@ class CatalogCacheIntervalObject extends TimeTableCacheIntervalObject
101 141 parent::dump();
102 142 echo " parameters = ";
103 143 foreach ($this->params as $param) {
104   - echo $param.", ";
  144 + echo $param." - ";
105 145 }
106 146 echo PHP_EOL;
107 147 }
... ...
php/classes/CatalogCacheMgr.php
... ... @@ -6,6 +6,7 @@
6 6  
7 7 class CatalogCacheMgr extends TimeTableCacheMgr
8 8 {
  9 + const DEFAULT_PARAM_ID_PREFIX = 'cat_param_id_';
9 10  
10 11 function __construct() {
11 12 $this->objectMgr = new CatalogMgr();
... ... @@ -15,55 +16,38 @@ class CatalogCacheMgr extends TimeTableCacheMgr
15 16 return "cacheCat";
16 17 }
17 18  
18   - protected function resetCache() {
  19 + protected function resetCache($options = array()) {
19 20 $this->cache = new CatalogCacheObject();
20   - }
21   -
22   - public function initObjectCache($options = array()) {
23   - $result = parent::initObjectCache($options);
24   - if (!$result['success']) {
25   - return $result;
  21 + if (!empty($options['nparams'])) {
  22 + for ($i = 0; $i < (int)$options['nparams']; $i++) {
  23 + $this->cache->addParameter(CatalogCacheMgr::DEFAULT_PARAM_ID_PREFIX.(string)($i+1), 'column_'.(string)($i+1), 1, 0);
  24 + }
26 25 }
27   -
28   - //init parameters
29   - $nparams = isset($options['nparams']) ? $options['nparams'] : 1;
30   - $this->cache->setParamsNumber((int)$nparams);
31   -
32   - $paramHeaders = array();
33   -
34   - for ($i = 0; $i < (int)$nparams; $i++) {
35   - $paramHeaders[$i]['id'] = 'id_'.(string)($i+1);
36   - $paramHeaders[$i]['name'] = 'column_'.(string)($i+1);
37   - $paramHeaders[$i]['size'] = 1;
38   - $paramHeaders[$i]['type'] = 'Float';
39   -
40   - }
41   - $this->cache->setParamsSizes($paramHeaders);
42   - $this->cache->setParamsTypes($paramHeaders);
43   - //Save cache file
44   - return $result + array('parameters' => $paramHeaders);
45   - }
46   -
47   - public function initFromObject($id, $type)
48   - {
49   - $result = parent::initFromObject($id, $type);
50   - if (!$result['success']) {
51   - return $result;
  26 + else if (!empty($options['parameters'])) {
  27 + $index = 0;
  28 + foreach ($options['parameters'] as $parameter) {
  29 + if (array_key_exists('id',$parameter) && !empty($parameter['id'])) {
  30 + $id = $parameter['id'];
  31 + }
  32 + else if (array_key_exists('ID',$parameter) && !empty($parameter['ID'])) {
  33 + $id = $parameter['ID'];
  34 + }
  35 + else {
  36 + $id = 'cat_param_id_'.$index;
  37 + }
  38 + $this->cache->addParameter($id, $parameter['name'], intval($parameter['size']), intval($parameter['type']));
  39 + ++$index;
  40 + }
52 41 }
  42 + return array('parameters' => $this->cache->getParametersInfo());
  43 + }
53 44  
54   - $params_desc = $this->objectMgr->getCatalogParamDescription($id, $name, FALSE, $type);
  45 + protected function loadAdditionalDescription($id, $type) {
  46 + $params_desc = $this->objectMgr->getCatalogParamDescription($id, "", FALSE, $type);
55 47 if (!$params_desc['success']) {
56   - return $params_desc;
  48 + return array();
57 49 }
58   -
59   - $paramHeaders = $params_desc['parameters'];
60   -
61   - $this->cache->setParamsNumber(count($paramHeaders));
62   - $this->cache->setParamsSizes($paramHeaders);
63   - $this->cache->setParamsTypes($paramHeaders);
64   -
65   - //Save cache file
66   - return $result+ array('parameters' => $paramHeaders);
  50 + return array('parameters' => $params_desc['parameters']);
67 51 }
68 52  
69 53 public function initFromTmpObject($folderId, $name) {
... ... @@ -77,34 +61,45 @@ class CatalogCacheMgr extends TimeTableCacheMgr
77 61 return $params_desc;
78 62 }
79 63  
80   - $paramHeaders = $params_desc['parameters'];
81   -
82   - $this->cache->setParamsNumber(count($paramHeaders));
83   - $this->cache->setParamsSizes($paramHeaders);
84   - $this->cache->setParamsTypes($paramHeaders);
  64 + foreach ($params_desc['parameters'] as $parameter) {
  65 + $this->cache->addParameter($parameter['id'], $parameter['name'], intval($parameter['size']), intval($parameter['type']));
  66 + }
85 67  
86   - return $result+ array('parameters' => $paramHeaders);
  68 + return $result+ array('parameters' => $this->cache->getParametersInfo());
87 69 }
88 70  
89 71 public function initFromUploadedFile($name, $format)
90 72 {
  73 +
  74 +
  75 +
  76 +
91 77 $result = parent::initFromUploadedFile($name, $format);
92 78 if (!$result['success']) {
93 79 return $result;
94 80 }
95 81  
96   - $params_desc = $this->objectMgr->getCatalogParamDescription($id, $name, FALSE, $type);
  82 + $info = $this->objectMgr->getUploadedObject($name, $format,TRUE);
  83 + foreach ($info['parameters'] as $parameter) {
  84 + $this->cache->addParameter($parameter['id'], $parameter['name'], intval($parameter['size']), intval($parameter['type']));
  85 + }
  86 +
  87 +
  88 +
  89 + /*$params_desc = $this->objectMgr->getCatalogParamDescription($id, $name, FALSE, $type);
97 90 if (!$params_desc['success']) {
98 91 return $params_desc;
99 92 }
100 93  
101   - $paramHeaders = $params_desc['parameters'];
  94 + foreach ($params_desc['parameters'] as $parameter) {
  95 + $this->cache->addParameter($parameter['id'], $parameter['name'], intval($parameter['size']), intval($parameter['type']));
  96 + }
  97 +
  98 + $paramHeaders = $params_desc['parameters'];*/
102 99  
103   - $this->cache->setParamsNumber(count($paramHeaders));
104   - $this->cache->setParamsSizes($paramHeaders);
105   - $this->cache->setParamsTypes($paramHeaders);
  100 + //$this->cache->setParameters($paramHeaders);
106 101  
107   - return $result+ array('parameters' => $paramHeaders);
  102 + return $result+ array('parameters' => $info['parameters']);
108 103 }
109 104  
110 105 }
... ...
php/classes/CatalogCacheObject.php
... ... @@ -2,169 +2,115 @@
2 2  
3 3 class CatalogCacheObject extends TimeTableCacheObject
4 4 {
5   - private $paramsNumber;
6   - private $paramsSizes = array();
7   - private $paramsTypes = array();
8   -
9   - public function addInterval($interval, $isNew = false, $index = -1)
10   - {
11   - $intervalObj = new CatalogCacheIntervalObject($this->lastId, count($this->intervals));
12   - ++$this->lastId;
13   - $intervalObj->setStartFromISO($interval['start']);
14   - $intervalObj->setStopFromISO($interval['stop']);
15   - // for catalog
16   - $intervalObj->setParams($interval['paramTable']);
17   -
18   - $intervalObj->setIsNew($isNew);
19   - array_push($this->intervals, $intervalObj);
20   -
21   - if ($index < 0)
22   - array_push($this->indexes, count($this->intervals) - 1);
23   - else
24   - array_splice($this->indexes, $index, 0, array(count($this->intervals) - 1));
25   -
26   - if ($isNew)
27   - $this->isModified = true;
  5 + private $parameters = array();
  6 +
  7 + public function reset() {
  8 + parent::reset();
  9 + $this->parameters = array();
  10 + //ToDo - Init sort and filters for Catalog
  11 + /*unset($this->filter);
  12 + $this->filter = new TimeTableCacheFilterObject();;
  13 + unset($this->sort);
  14 + $this->sort = new TimeTableCacheSortObject();*/
  15 + }
28 16  
29   - return $intervalObj;
  17 + protected function createNewIntervalObject($id, $index = -1) {
  18 + return new CatalogCacheIntervalObject($this, $id, $index);
30 19 }
31 20  
32   - public function setParamsNumber($number)
  21 + public function addInterval($interval, $isNew = false, $index = -1)
33 22 {
34   - $this->paramsNumber = $number;
  23 + $intervalObj = parent::addInterval($interval, $isNew, $index);
  24 + for ($i = 0; $i < count($interval['paramTable']); ++$i) {
  25 + $intervalObj->setParamValue($this->parameters[$i]['id'], $interval['paramTable'][$i]);
  26 + }
  27 + return $intervalObj;
35 28 }
36 29  
37   - public function setParamsSizes($params)
  30 + public function addParameter($id, $name, $size, $type)
38 31 {
39   - for ($i = 0; $i < $this->paramsNumber; $i++)
40   - $this->paramsSizes[$i] = $params[$i]['size'];
  32 + $this->parameters[] = array(
  33 + 'id' => $id,
  34 + 'name' => $name,
  35 + 'size' => $size,
  36 + 'type' => $type,
  37 + );
41 38 }
42 39  
43   - public function setParamsTypes($params)
44   - {
45   - for ($i = 0; $i < $this->paramsNumber; $i++)
46   - $this->paramsTypes[$i] = $params[$i]['type'];
  40 + public function getParametersInfo() {
  41 + return $this->parameters;
47 42 }
48 43  
49   - public function writeBin($handle)
  44 + public function writeAdditionalHeaderBin($handle)
50 45 {
51   - //Magic key ("TTC")
52   - fwrite($handle,pack('C3',ord('T'),ord('T'),ord('C')));
53   -
54   - //Version
55   - fwrite($handle,pack('L',TimeTableCacheObject::$format_version));
56   -
57   - //Token
58   - for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i)
59   - fwrite($handle,pack('C',ord($this->token[$i])));
60   -
61   - //Modified
62   - fwrite($handle,pack('L',$this->isModified));
63   -
64   - //Filter
65   - $this->filter->writeBin($handle);
66   -
67   - //Sort
68   - $this->sort->writeBin($handle);
69   -
70   - //Params Number
71   - fwrite($handle,pack('L',$this->paramsNumber));
72   -
73   - //Params Sizes
74   - for ($i = 0; $i < $this->paramsNumber; $i++)
75   - fwrite($handle,pack('L',$this->paramsSizes[$i]));
76   -
77   - //Params Types
78   - for ($i = 0; $i < $this->paramsNumber; $i++)
79   - fwrite($handle,pack('L',$this->paramsTypes[$i]));
80   -
81   - //Intervals
82   - fwrite($handle,pack('L2', count($this->intervals), $this->lastId));
83   -
84   -
85   - foreach($this->intervals as $interval)
86   - $interval->writeBin($handle,$this->paramsNumber,$this->paramsSizes, $this->paramsTypes);
87   -
88   - //Indexes
89   - fwrite($handle,pack('L',count($this->indexes)));
90   - foreach($this->indexes as $index)
91   - fwrite($handle,pack('L',$index));
  46 + //Params Number
  47 + fwrite($handle,pack('L',count($this->parameters)));
  48 + foreach ($this->parameters as $parameter) {
  49 + //Param Id length
  50 + fwrite($handle,pack('L',strlen($parameter['id'])));
  51 + //Param name length
  52 + fwrite($handle,pack('L',strlen($parameter['name'])));
  53 + //Param Size
  54 + fwrite($handle,pack('L',$parameter['size']));
  55 + //Param Type
  56 + fwrite($handle,pack('L',$parameter['type']));
  57 + //Param Id
  58 + for ($i = 0; $i < strlen($parameter['id']); ++$i)
  59 + fwrite($handle,pack('C',ord($parameter['id'][$i])));
  60 + //Param name:
  61 + for ($i = 0; $i < strlen($parameter['name']); ++$i)
  62 + fwrite($handle,pack('C',ord($parameter['name'][$i])));
  63 + }
92 64 }
93 65  
94   - public function loadBin($handle) {
95   - //Magic key ("TTC")
96   - if (!$res = unpack('C3key',fread($handle,3)))
97   - return false;
98   -
99   - if (($res['key1'] != ord('T')) || ($res['key2'] != ord('T')) || ($res['key3'] != ord('C')))
100   - return false;
101   -
102   - //Version
103   - if (!$res = unpack('Lversion',fread($handle,4)))
104   - return false;
105   - if (($res['version'] != TimeTableCacheObject::$format_version))
106   - return false;
107   -
108   - //Token
109   - $token = "";
110   - for ($i = 0; $i < TimeTableCacheObject::$token_len; ++$i)
111   - {
112   - if (!$res = unpack('Ctoken',fread($handle,1)))
  66 + protected function loadAdditionalHeaderBin($handle) {
  67 + //Params Number
  68 + if (!$res = unpack('Lnumber',fread($handle,4)))
113 69 return false;
114   - $token .= chr($res['token']);
115   - }
116   - $this->token = $token;
117   -
118   - //Modified
119   - if (!$res = unpack('Lmodified',fread($handle,4)))
120   - return false;
121   - $this->isModified = $res['modified'];
  70 + $nbParams = $res['number'];
122 71  
123   - //Filter
124   - $this->filter->loadBin($handle);
  72 + for ($i = 0; $i < $nbParams; ++$i) {
  73 + //Param Id length
  74 + if (!$res = unpack('Lidlength',fread($handle,4)))
  75 + return false;
  76 + $idlength = $res['idlength'];
125 77  
126   - //Sort
127   - $this->sort->loadBin($handle);
128   -
129   - //ParamsNumber
130   - if (!$res = unpack('Lnumber',fread($handle,4)))
131   - return false;
132   - $this->paramsNumber = $res['number'];
133   -
134   - //ParamsSizes
135   - for ($i = 0; $i < $this->paramsNumber; $i++) {
136   - if (!$res = unpack('Lsize',fread($handle,4)))
137   - return false;
138   - $this->paramsSizes[$i] = $res['size'];
139   - }
140   - //ParamsTypes
141   - for ($i = 0; $i < $this->paramsNumber; $i++) {
142   - if (!$res = unpack('Ltype',fread($handle,4)))
143   - return false;
144   - $this->paramsTypes[$i] = $res['type'];
145   - }
146   - //Intervals
147   - $res = unpack('L2data',fread($handle,2*4));
148   - $nbIntervals = $res['data1'];
149   - $this->lastId = $res['data2'];
  78 + //Param Name length
  79 + if (!$res = unpack('Lnamelength',fread($handle,4)))
  80 + return false;
  81 + $namelength = $res['namelength'];
150 82  
151   - for ($i = 0; $i < $nbIntervals; ++$i)
152   - {
153   - $interval = new CatalogCacheIntervalObject(-1);
154   - $interval->loadBin($handle, $this->paramsNumber, $this->paramsSizes, $this->paramsTypes);
155   - array_push($this->intervals, $interval);
156   - }
  83 + //Param Size
  84 + if (!$res = unpack('Lsize',fread($handle,4)))
  85 + return false;
  86 + $size = $res['size'];
  87 +
  88 + //Param Type
  89 + if (!$res = unpack('Ltype',fread($handle,4)))
  90 + return false;
  91 + $type = $res['type'];
  92 +
  93 + //Param Id
  94 + $id = "";
  95 + for ($j = 0; $j < $idlength; ++$j)
  96 + {
  97 + if (!$res = unpack('Cid',fread($handle,1)))
  98 + return false;
  99 + $id .= chr($res['id']);
  100 + }
157 101  
158   - //Indexes
159   - $res = unpack('Ldata',fread($handle,4));
160   - $nbIndexes = $res['data'];
161   - for ($i = 0; $i < $nbIndexes; ++$i)
162   - {
163   - $res = unpack('Lindex',fread($handle,4));
164   - array_push($this->indexes, $res['index']);
165   - }
  102 + //Param Name
  103 + $name = "";
  104 + for ($j = 0; $j < $namelength; ++$j)
  105 + {
  106 + if (!$res = unpack('Cname',fread($handle,1)))
  107 + return false;
  108 + $name .= chr($res['name']);
  109 + }
166 110  
167   - return true;
  111 + $this->addParameter($id, $name, $size, $type);
  112 + }
  113 + return true;
168 114 }
169 115  
170 116 public function modifyIntervalFromId($cacheId, $data) {
... ... @@ -178,7 +124,7 @@ class CatalogCacheObject extends TimeTableCacheObject
178 124 if (strpos($key, 'param') !== FALSE) {
179 125 $params = $interval->getParams();
180 126 $paramIndex = (int)substr($key,5);
181   - $params[$paramIndex-2] = $val;
  127 + $params[$paramIndex-2] = $value;
182 128 $interval->setParams($params);
183 129 $interval->setIsModified(true);
184 130 $this->isModified = true;
... ... @@ -190,6 +136,34 @@ class CatalogCacheObject extends TimeTableCacheObject
190 136  
191 137 return false;
192 138 }
  139 +
  140 + public function dump() {
  141 + echo " => CatalogCacheObject : token = ".$this->token.", nb intervals = ".count($this->intervals).", last id = ".$this->lastId.", nb indexes = ".count($this->indexes).PHP_EOL;
  142 + echo PHP_EOL;
  143 +
  144 + echo " => Parameters : ".PHP_EOL;
  145 + foreach ($this->parameters as $parameter) {
  146 + echo " * id = ".$parameter['id'].", name = ".$parameter['name'].", size = ".$parameter['size'].", type = ".$parameter['type'].PHP_EOL;
  147 + }
  148 + echo PHP_EOL;
  149 +
  150 + $this->filter->dump();
  151 + echo PHP_EOL;
  152 +
  153 + $this->sort->dump();
  154 + echo PHP_EOL;
  155 +
  156 + foreach ($this->intervals as $interval)
  157 + $interval->dump();
  158 + echo PHP_EOL;
  159 +
  160 + echo " => Indexes list : ";
  161 + foreach ($this->indexes as $index)
  162 + {
  163 + echo $index.", ";
  164 + }
  165 + echo PHP_EOL;
  166 + }
193 167 }
194 168  
195 169 ?>
... ...
php/classes/CatalogMgr.php
... ... @@ -21,18 +21,6 @@ class CatalogMgr extends TimeTableMgr
21 21 }
22 22 }
23 23  
24   - public function getUploadedObject($name, $format, $onlyDescription = false)
25   - {
26   - if ($format == 'VOT')
27   - {
28   - $attributesToReturn = $this->vot2amda(USERTEMPDIR.$name, $onlyDescription);
29   - }
30   - $attributesToReturn['objName'] = $name;
31   - $attributesToReturn['objFormat'] = $format;
32   -
33   - return $attributesToReturn;
34   - }
35   -
36 24 public function getTmpObject($folderId, $name, $onlyDescription = false)
37 25 {
38 26 $filePath = USERWORKINGDIR.$folderId.'/'.$name.'.xml';
... ... @@ -396,6 +384,36 @@ class CatalogMgr extends TimeTableMgr
396 384  
397 385 }
398 386  
  387 + public function getUploadedObject($name, $format, $onlyDescription = false)
  388 + {
  389 + $result = TimeTableMgr::getUploadedObject($name, $format, $onlyDescription);
  390 + if (!isset($result['success']) || !$result['success']) {
  391 +
  392 + return array('success' => FALSE, 'error' => 'Error to retrieve catalog info');
  393 + }
  394 +
  395 + $this->objectDom->load(USERTEMPDIR . $name . '.xml');
  396 +
  397 + $xpath = new DOMXPath($this->objectDom);
  398 +
  399 + // params header
  400 + $paramsNodes = $xpath->query('//parameters/parameter');
  401 + $paramsArray = array();
  402 +
  403 + if ($paramsNodes->length > 0)
  404 + {
  405 + foreach ($paramsNodes as $paramNode)
  406 + {
  407 + $oneParam = array();
  408 + foreach ($paramNode->attributes as $attr)
  409 + $oneParam[$attr->nodeName] = $attr->nodeValue;
  410 + $paramsArray[] = $oneParam;
  411 + }
  412 + }
  413 +
  414 + return $result + array('parameters' => $paramsArray);
  415 + }
  416 +
399 417 // public function modifyObject($p) {
400 418 // $folder = $this->getObjectFolder($p->id);
401 419 //
... ... @@ -428,98 +446,6 @@ class CatalogMgr extends TimeTableMgr
428 446 // }
429 447 // }
430 448  
431   -/*
432   -* Uploaded vot catalog => convert to AMDA tmp
433   -*/
434   - protected function vot2amda($tmp_file, $onlyDescription = false)
435   - {
436   - // Load Catalog
437   - $this->objectDom->load($tmp_file);
438   - $objToGet = $this->objectDom->getElementsByTagName('TABLEDATA')->item(0);
439   -
440   - $attributes = $objToGet->childNodes;
441   - $paramsNodes = $this->objectDom->getElementsByTagName('FIELD');
442   - $paramsNumber = $paramsNodes->length;
443   -
444   - foreach($attributes as $attribute)
445   - {
446   - if ($attribute->tagName == 'TR')
447   - {
448   - $start = $attribute->getElementsByTagName('TD')->item(0)->nodeValue;
449   - $stop = $attribute->getElementsByTagName('TD')->item(1)->nodeValue;
450   - $params = array();
451   -
452   - for ($i = 2; $i < $paramsNumber; $i++)
453   - {
454   - $param = $attribute->getElementsByTagName('TD')->item($i)->nodeValue;
455   -
456   - if ($paramsNodes->item($i)->hasAttribute('arraysize'))
457   - {
458   - $param = join(",",explode(" ",trim($param)));
459   - }
460   - $params[] = $param;
461   - }
462   - if (!$onlyDescription)
463   - $attributesToReturn['intervals'][] = array('start' => $start, 'stop' => $stop, 'paramTable' => $params);
464   - }
465   - }
466   -
467   - if ($paramsNumber > 2)
468   - {
469   - $paramsArray = array();
470   - for ($i = 2; $i < $paramsNumber; $i++)
471   - {
472   - $oneParam = array();
473   - $paramNode = $paramsNodes->item($i);
474   -
475   - if ($paramNode->hasAttribute('arraysize'))
476   - {
477   - $oneParam['size'] = $paramNode->getAttribute('arraysize');
478   - }
479   - else
480   - {
481   - $oneParam['size'] = 1;
482   - }
483   -
484   - foreach ($paramNode->attributes as $attr)
485   - {
486   - if ($attr->nodeName == 'datatype')
487   - {
488   - $datatype = $attr->nodeValue;
489 449  
490   - if ($paramNode->hasAttribute('xtype') && (($paramNode->getAttribute('xtype') == 'dateTime') || ($paramNode->getAttribute('xtype') == 'iso8601')))
491   - {
492   - $oneParam['type'] = 1;
493   - $paramNode->setAttribute('xtype','dateTime');
494   - $paramNode->setAttribute('size',1);
495   - }
496   - else if ($datatype == 'char')
497   - {
498   - $oneParam['type'] = 2;// string
499   - $paramNode->setAttribute('size', 1);
500   - }
501   - else
502   - {
503   - $oneParam['type'] = 0;//'Double';
504   - }
505   - }
506   - else if ($attr->nodeName != 'arraysize')
507   - {
508   - $oneParam[$attr->nodeName] = $attr->nodeValue;
509   - }
510   - }
511   - $paramsArray[] = $oneParam;
512   - }
513   - $attributesToReturn['success'] = true;
514   - $attributesToReturn['parameters'] = $paramsArray;
515   - }
516   -
517   - $suffix = explode('.', basename($tmp_file));
518   - $attributesToReturn['name'] = basename($tmp_file, '.'.$suffix[1]);
519   - $attributesToReturn['created'] = date('Y-m-d')."T".date('H:i:s');
520   - $attributesToReturn['description'] = htmlspecialchars($this->objectDom->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue);
521   -
522   - return($attributesToReturn);
523   - }
524 450 }
525 451 ?>
... ...
php/classes/TimeTableCacheIntervalObject.php
1 1 <?php
2 2 class TimeTableCacheIntervalObject
3 3 {
  4 + protected $cacheObject = NULL;
4 5 protected $id = -1;
5 6 protected $index = -1;
6 7 protected $start = 0;
... ... @@ -8,7 +9,8 @@ class TimeTableCacheIntervalObject
8 9 protected $isNew = false;
9 10 protected $isModified = false;
10 11  
11   - function __construct($id, $index = -1) {
  12 + function __construct($cacheObject, $id, $index = -1) {
  13 + $this->cacheObject = $cacheObject;
12 14 $this->id = $id;
13 15 $this->index = $index;
14 16 }
... ...
php/classes/TimeTableCacheMgr.php
... ... @@ -21,21 +21,27 @@
21 21 return USERTTDIR;
22 22 }
23 23  
24   - protected function resetCache() {
  24 + protected function resetCache($options = array()) {
25 25 $this->cache = new TimeTableCacheObject();
  26 + return array();
  27 + }
  28 +
  29 + protected function loadAdditionalDescription($id, $type) {
  30 + return array();
26 31 }
27 32  
28 33 public function initObjectCache($options = array()) {
29 34 //Create new cache
30   - $this->resetCache();
  35 + $info = $this->resetCache($options);
31 36  
32 37 //Save cache file
33   - return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus());
  38 + return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus()) + $info;
34 39 }
35 40  
36 41 public function initFromObject($id, $type) {
  42 + $options = $this->loadAdditionalDescription($id, $type);
37 43 //Create new cache
38   - $this->resetCache();
  44 + $info = $this->resetCache($options);
39 45  
40 46 //Load intervals from object file and add to cache
41 47 $intervals_res = $this->objectMgr->loadIntervalsFromObject($id,$type);
... ... @@ -55,7 +61,7 @@
55 61 $this->cache->updateIndexes();
56 62  
57 63 //Save cache file
58   - return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus());
  64 + return array('success' => $this->saveToFile(), 'token' => $this->cache->getToken(), 'status' => $this->cache->getStatus()) + $info;
59 65 }
60 66  
61 67 public function initFromCatalog($id, $type) {
... ...
php/classes/TimeTableCacheObject.php
... ... @@ -20,8 +20,7 @@ class TimeTableCacheObject
20 20  
21 21 function __construct() {
22 22 $this->token = $this->getRandomToken();
23   - $this->filter = new TimeTableCacheFilterObject();
24   - $this->sort = new TimeTableCacheSortObject();
  23 + $this->reset();
25 24 }
26 25  
27 26 public function reset() {
... ... @@ -39,8 +38,12 @@ class TimeTableCacheObject
39 38 $this->isModified = $isModified;
40 39 }
41 40  
  41 + protected function createNewIntervalObject($id, $index = -1) {
  42 + return new TimeTableCacheIntervalObject($this, $id, $index);
  43 + }
  44 +
42 45 public function addInterval($interval, $isNew = false, $index = -1) {
43   - $intervalObj = new TimeTableCacheIntervalObject($this->lastId, count($this->intervals));
  46 + $intervalObj = $this->createNewIntervalObject($this->lastId, count($this->intervals));
44 47 ++$this->lastId;
45 48 $intervalObj->setStartFromISO($interval['start']);
46 49 $intervalObj->setStopFromISO($interval['stop']);
... ... @@ -93,12 +96,12 @@ class TimeTableCacheObject
93 96 foreach ($data as $key => $value) {
94 97 switch ($key) {
95 98 case 'start' :
96   - $interval->setStartFromISO($start);
  99 + $interval->setStartFromISO($value);
97 100 $interval->setIsModified(true);
98 101 $this->isModified = true;
99 102 break;
100 103 case 'stop':
101   - $interval->setStopFromISO($stop);
  104 + $interval->setStopFromISO($value);
102 105 $interval->setIsModified(true);
103 106 $this->isModified = true;
104 107 break;
... ... @@ -174,7 +177,7 @@ class TimeTableCacheObject
174 177 $this->reset();
175 178  
176 179 foreach ($merged_intervals as $merged_interval) {
177   - $interval = new TimeTableCacheIntervalObject($this->lastId, count($this->intervals));
  180 + $interval = $this->createNewIntervalObject($this->lastId, count($this->intervals));
178 181 ++$this->lastId;
179 182 $interval->setStartFromStamp($merged_interval["start"]);
180 183 $interval->setStopFromStamp($merged_interval["stop"]);
... ... @@ -417,10 +420,14 @@ class TimeTableCacheObject
417 420 //Sort
418 421 $this->sort->writeBin($handle);
419 422  
  423 + //Additional info
  424 + $this->writeAdditionalHeaderBin($handle);
  425 +
420 426 //Intervals
421 427 fwrite($handle,pack('L2',count($this->intervals), $this->lastId));
422   - foreach($this->intervals as $interval)
  428 + foreach($this->intervals as $interval) {
423 429 $interval->writeBin($handle);
  430 + }
424 431  
425 432 //Indexes
426 433 fwrite($handle,pack('L',count($this->indexes)));
... ... @@ -463,13 +470,16 @@ class TimeTableCacheObject
463 470 //Sort
464 471 $this->sort->loadBin($handle);
465 472  
  473 + //Additional info
  474 + $this->loadAdditionalHeaderBin($handle);
  475 +
466 476 //Intervals
467 477 $res = unpack('L2data',fread($handle,2*4));
468 478 $nbIntervals = $res['data1'];
469 479 $this->lastId = $res['data2'];
470 480 for ($i = 0; $i < $nbIntervals; ++$i)
471 481 {
472   - $interval = new TimeTableCacheIntervalObject(-1);
  482 + $interval = $this->createNewIntervalObject(-1);
473 483 $interval->loadBin($handle);
474 484 array_push($this->intervals, $interval);
475 485 }
... ... @@ -486,6 +496,14 @@ class TimeTableCacheObject
486 496 return true;
487 497 }
488 498  
  499 + protected function loadAdditionalHeaderBin($handle) {
  500 + // Nothing to do for a TT
  501 + }
  502 +
  503 + protected function writeAdditionalHeaderBin($handle) {
  504 + // Nothing to do for a TT
  505 + }
  506 +
489 507 protected function getRandomToken() {
490 508 $letters = 'abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
491 509 return substr(str_shuffle($letters), 0, TimeTableCacheObject::$token_len);
... ...
php/classes/TimeTableMgr.php
... ... @@ -242,22 +242,6 @@ class TimeTableMgr extends AmdaObjectMgr
242 242 */
243 243 public function getUploadedObject($name, $format, $onlyDescription = false)
244 244 {
245   - if (strpos($name, '.txt') !== false || strpos($name, '.asc') !== false || strpos($name, '.') == false) {
246   - $attributesToReturn = $this->textToAmda(USERTEMPDIR . $name, $onlyDescription);
247   - $attributesToReturn['objName'] = $name;
248   - $attributesToReturn['objFormat'] = $format;
249   -
250   - return $attributesToReturn;
251   - }
252   -
253   - if ($format == 'VOT') {
254   - $attributesToReturn = $this->vot2amda(USERTEMPDIR . $name, $onlyDescription);
255   - $attributesToReturn['objName'] = $name;
256   - $attributesToReturn['objFormat'] = $format;
257   -
258   - return $attributesToReturn;
259   - }
260   -
261 245 if (strpos($name, '.xml') !== false) {
262 246 $temp = explode('.xml', $name);
263 247 $name = $temp[0];
... ... @@ -299,10 +283,12 @@ class TimeTableMgr extends AmdaObjectMgr
299 283 case 'created':
300 284 $attributesToReturn['created'] = $attribute->nodeValue;
301 285 break;
302   - case 'chain':
303   - case 'source':
  286 + case 'description':
304 287 $attributesToReturn['description'] = $attribute->nodeValue;
305 288 break;
  289 + case 'history':
  290 + $attributesToReturn['history'] = $attribute->nodeValue;
  291 + break;
306 292 default:
307 293 break;
308 294 }
... ... @@ -310,107 +296,10 @@ class TimeTableMgr extends AmdaObjectMgr
310 296 }
311 297 }
312 298 }
  299 + $attributesToReturn['success'] = TRUE;
313 300 return $attributesToReturn;
314 301 }
315 302  
316   - /*
317   - * Uploaded text file => convert to array
318   - */
319   -
320   - /**
321   - * Convert text to AMDA attributes
322   - * @param $tmp_file
323   - * @param bool $onlyDescription
324   - * @return mixed
325   - */
326   - protected function textToAmda($tmp_file, $onlyDescription = false)
327   - {
328   - $suffix = explode('.', basename($tmp_file));
329   - $lines = file($tmp_file, FILE_SKIP_EMPTY_LINES);
330   - $description = "Uploaded Time Table" . PHP_EOL;
331   -
332   - $recordsNumber = count($lines);
333   - $descNumber = 0;
334   -
335   - foreach ($lines as $line) {
336   - $line = trim($line);
337   - if ($line[0] == '#') { // Comment
338   - $description = $description . PHP_EOL . substr($line, 1, -1);
339   - } else {
340   - $line = preg_replace('/[-:\/T\s]+/', ' ', $line);
341   - $isoFormat = 'Y-m-dTH:i:s';
342   - $doyFormat = 'Y z H i s';
343   - $doyRegex = '(\d{4}) (\d{3}) (\d{2}) (\d{2}) (\d{2})( \d{2})?';
344   -
345   - if (preg_match('/^' . $doyRegex . ' ' . $doyRegex . '$/', $line)) {
346   - $start = DateTime::createFromFormat($doyFormat, substr($line, 0, 17));
347   - $stop = DateTime::createFromFormat($doyFormat, substr($line, 18));
348   - $startDate = $start->sub(new DateInterval('P1D'))->format($isoFormat);
349   - $stopDate = $stop->sub(new DateInterval('P1D'))->format($isoFormat);
350   - } else {
351   - $dateLength = round((strlen($line)-1) / 2);
352   -
353   - $start = explode(' ', substr($line, 0, $dateLength) . ' 00');
354   - $startTime = strtotime("$start[0]/$start[1]/$start[2] $start[3]:$start[4]:$start[5]");
355   -
356   - $stop = explode(' ', substr($line, $dateLength + 1) . ' 00');
357   - $stopTime = strtotime("$stop[0]/$stop[1]/$stop[2] $stop[3]:$stop[4]:$stop[5]");
358   - if (is_numeric($startTime) && is_numeric($stopTime)) {
359   - $startDate = date($isoFormat, $startTime);
360   - $stopDate = date($isoFormat, $stopTime);
361   - } else {
362   - $description = $description . PHP_EOL . $line;
363   - $descNumber++;
364   - continue;
365   - }
366   - }
367   -
368   - if (!$onlyDescription) {
369   - $attributesToReturn['intervals'][] = ['start' => $startDate, 'stop' => $stopDate];
370   - }
371   - }
372   - }
373   - if ($recordsNumber == $descNumber) {
374   - $description = 'Looks like we can not read your time format...' . PHP_EOL . $description;
375   - }
376   -
377   - $attributesToReturn['description'] = $description;
378   - $attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]);
379   - $attributesToReturn['created'] = date('Y-m-d\TH:i:s');
380   - return $attributesToReturn;
381   - }
382   -
383   - /**
384   - * Convert VOTable time table to AMDA attributes
385   - * @param $tmp_file
386   - * @param bool $onlyDescription
387   - * @return mixed
388   - */
389   - protected function vot2amda($tmp_file, $onlyDescription = false)
390   - {
391   - // Load Time table
392   - $this->objectDom->load($tmp_file);
393   - $objToGet = $this->objectDom->getElementsByTagName('TABLEDATA')->item(0);
394   - $attributesToReturn['name'] = $tmp_file;
395   - $attributes = $objToGet->childNodes;
396   -
397   - /** @var DOMElement $attribute */
398   - foreach ($attributes as $attribute) {
399   - if ($attribute->tagName == 'TR') {
400   - $start = $attribute->getElementsByTagName('TD')->item(0)->nodeValue;
401   - $stop = $attribute->getElementsByTagName('TD')->item(1)->nodeValue;
402   - if (!$onlyDescription) {
403   - $attributesToReturn['intervals'][] = ['start' => $start, 'stop' => $stop];
404   - }
405   - }
406   - }
407   - $suffix = explode('.', basename($tmp_file));
408   - $attributesToReturn['name'] = basename($tmp_file, '.' . $suffix[1]);
409   - $attributesToReturn['created'] = date('Y-m-d') . "T" . date('H:i:s');
410   - $description = $this->objectDom->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue;
411   - $attributesToReturn['description'] = htmlspecialchars($description);
412   - return ($attributesToReturn);
413   - }
414 303  
415 304 /*****************************************************************
416 305 * PUBLIC FUNCTIONS
... ...