Пример #1
0
 public function get($functionName, $linkName, $params = array(), $singleResult = false, $checkSession = true)
 {
     // in diesem Beispiel wird der Name, welcher in der Commands.json für diesen Funktionsaufruf angegeben wurde,
     // direkt Verwendet, um einen gleichnamigen Ausgang der Component.json anzusprechen und den dort
     // angegeben Aufruf auzulösen (wobei courseid entsprechend ersetzt wird)
     // diese Funktion soll aufgerufen werden, wenn unsere Anfrage an die Datenbank positiv war
     $positive = function ($input, $singleResult) {
         $result = Model::isEmpty();
         $result['content'] = array();
         foreach ($input as $inp) {
             if ($inp->getNumRows() > 0) {
                 // extract course data from db answer
                 $result['content'] = array_merge($result['content'], Course::ExtractCourse($inp->getResponse(), $singleResult));
                 $result['status'] = 200;
             }
         }
         return $result;
     };
     // hier wird eine MySql stored-procedure aufgerufen
     // dabei haben die aufzurufen Befehle die Form /funktionsname/:idA/:idB  (stehen in der Component.json)
     // dabei werden idA und idB durch die Werte in $params ersetzt Bsp.: $params = array("idA"=>2, "idB"=>3)
     $params = DBJson::mysql_real_escape_string($params);
     return $this->_component->call($linkName, $params, '', 200, $positive, array($singleResult), 'Model::isProblem', array(), 'Query');
 }
Пример #2
0
 /**
  * Adds an Setting.
  */
 public function addSetting($pre = '', $courseid)
 {
     $this->loadConfig($pre);
     $pre = ($pre === '' ? '' : '_') . $pre;
     Logger::Log('starts POST AddSetting', LogLevel::DEBUG);
     // decode the received Setting data, as an object
     $insert = Setting::decodeSetting($this->_app->request->getBody());
     // always been an array
     $arr = true;
     if (!is_array($insert)) {
         $insert = array($insert);
         $arr = false;
     }
     $courseid = DBJson::mysql_real_escape_string($courseid);
     $pre = DBJson::mysql_real_escape_string($pre);
     // this array contains the indices of the inserted objects
     $res = array();
     foreach ($insert as $in) {
         // generates the insert data for the object
         $data = $in->getInsertData();
         // starts a query, by using a given file
         $result = DBRequest::getRoutedSqlFile($this->query, dirname(__FILE__) . '/Sql/AddSetting.sql', array('object' => $in, 'pre' => $pre, 'courseid' => $courseid));
         // checks the correctness of the query
         if ($result['status'] >= 200 && $result['status'] <= 299) {
             $queryResult = Query::decodeQuery($result['content']);
             // sets the new auto-increment id
             $obj = new Setting();
             $course = Course::ExtractCourse($queryResult[count($queryResult) - 1]->getResponse(), true);
             $insertId = $queryResult[count($queryResult) - 2]->getInsertId();
             if ($insertId == 0 && $in->getId() > 0) {
                 $insertId = Setting::getIdFromSettingId($in->getId());
             }
             if ($insertId != 0) {
                 $obj->setId($course->getId() . '_' . $insertId);
             }
             $res[] = $obj;
             $this->_app->response->setStatus(201);
             if (isset($result['headers']['Content-Type'])) {
                 $this->_app->response->headers->set('Content-Type', $result['headers']['Content-Type']);
             }
         } else {
             Logger::Log('POST AddSetting failed', LogLevel::ERROR);
             $this->_app->response->setStatus(isset($result['status']) ? $result['status'] : 409);
             $this->_app->response->setBody(Setting::encodeSetting($res));
             $this->_app->stop();
         }
     }
     if (!$arr && count($res) == 1) {
         $this->_app->response->setBody(Setting::encodeSetting($res[0]));
     } else {
         $this->_app->response->setBody(Setting::encodeSetting($res));
     }
 }
Пример #3
0
 public function add($name = '', $id, $idName, $functionName, $sqlFile)
 {
     $this->loadConfig($name);
     $name = ($name === '' ? '' : '_') . $name;
     Logger::Log('starts ' . $functionName, LogLevel::DEBUG);
     // decode the received attachment data, as an object
     $insert = Transaction::decodeTransaction($this->_app->request->getBody());
     // always been an array
     $arr = true;
     if (!is_array($insert)) {
         $insert = array($insert);
         $arr = false;
     }
     $name = DBJson::mysql_real_escape_string($name);
     $id = DBJson::mysql_real_escape_string($id);
     $uuid = new uuid();
     // this array contains the indices of the inserted objects
     $res = array();
     foreach ($insert as $in) {
         $random = str_replace('-', '', $uuid->get());
         $in->setTransactionId(null);
         // generates the insert data for the object
         $data = $in->getInsertData();
         // starts a query, by using a given file
         $result = DBRequest::getRoutedSqlFile($this->query, $sqlFile, array('object' => $in, 'name' => $name, $idName => $id, 'random' => $random));
         // checks the correctness of the query
         if ($result['status'] >= 200 && $result['status'] <= 299) {
             $queryResult = Query::decodeQuery($result['content']);
             // sets the new auto-increment id
             $obj = new Transaction();
             $course = Course::ExtractCourse($queryResult[count($queryResult) - 1]->getResponse(), true);
             $obj->setTransactionId($course->getId() . '_' . $queryResult[count($queryResult) - 2]->getInsertId() . '_' . $random);
             $res[] = $obj;
             $this->_app->response->setStatus(201);
             if (isset($result['headers']['Content-Type'])) {
                 $this->_app->response->headers->set('Content-Type', $result['headers']['Content-Type']);
             }
         } else {
             Logger::Log($functionName . ' failed', LogLevel::ERROR);
             $this->_app->response->setStatus(isset($result['status']) ? $result['status'] : 409);
             $this->_app->response->setBody(Transaction::encodeTransaction($res));
             $this->_app->stop();
         }
     }
     if (!$arr && count($res) == 1) {
         $this->_app->response->setBody(Transaction::encodeTransaction($res[0]));
     } else {
         $this->_app->response->setBody(Transaction::encodeTransaction($res));
     }
 }
Пример #4
0
 public function get($functionName, $linkName, $params = array(), $checkSession = true)
 {
     $positive = function ($input) {
         $result = Model::isEmpty();
         $result['content'] = array();
         foreach ($input as $inp) {
             if ($inp->getNumRows() > 0) {
                 // extract Course data from db answer
                 $res = Course::ExtractCourse($inp->getResponse(), false);
                 $result['content'] = array_merge($result['content'], is_array($res) ? $res : array($res));
                 $result['status'] = 200;
             }
         }
         return $result;
     };
     $params = DBJson::mysql_real_escape_string($params);
     return $this->_component->call($linkName, $params, '', 200, $positive, array(), 'Model::isProblem', array(), 'Query');
 }
Пример #5
0
 /**
  * Adds a process.
  *
  * Called when this component receives an HTTP POST request to
  * (/$pre)/process(/).
  *
  * @param int $pre A optional prefix for the process table.
  */
 public function addProcess($pre = '')
 {
     $this->loadConfig($pre);
     $pre = ($pre === '' ? '' : '_') . $pre;
     Logger::Log('starts POST AddProcess', LogLevel::DEBUG);
     // decode the received choice data, as an object
     $insert = Process::decodeProcess($this->_app->request->getBody());
     // always been an array
     $arr = true;
     if (!is_array($insert)) {
         $insert = array($insert);
         $arr = false;
     }
     // this array contains the indices of the inserted objects
     $res = array();
     foreach ($insert as $in) {
         // starts a query, by using a given file
         $result = DBRequest::getRoutedSqlFile($this->query, dirname(__FILE__) . '/Sql/AddProcess.sql', array('object' => $in, 'pre' => $pre));
         // checks the correctness of the query
         if ($result['status'] >= 200 && $result['status'] <= 299) {
             $queryResult = Query::decodeQuery($result['content']);
             // sets the new auto-increment id
             $obj = new Process();
             $course = Course::ExtractCourse($queryResult[count($queryResult) - 1]->getResponse(), true);
             $obj->setProcessId($course->getId() . '_' . $queryResult[count($queryResult) - 2]->getInsertId());
             $res[] = $obj;
             $this->_app->response->setStatus(201);
             if (isset($result['headers']['Content-Type'])) {
                 $this->_app->response->headers->set('Content-Type', $result['headers']['Content-Type']);
             }
         } else {
             Logger::Log('POST AddProcess failed', LogLevel::ERROR);
             $this->_app->response->setStatus(isset($result['status']) ? $result['status'] : 409);
             $this->_app->response->setBody(Process::encodeProcess($res));
             $this->_app->stop();
         }
     }
     if (!$arr && count($res) == 1) {
         $this->_app->response->setBody(Process::encodeProcess($res[0]));
     } else {
         $this->_app->response->setBody(Process::encodeProcess($res));
     }
 }