Skip to content

Commit

Permalink
Refactored according to new schedule. Weekends are made for refactori…
Browse files Browse the repository at this point in the history
…ng, and that's just what I'll do, oh datatank I can't get over you.
  • Loading branch information
coreation committed Sep 24, 2011
1 parent 5d2c929 commit 315fb7f
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 227 deletions.
10 changes: 3 additions & 7 deletions model/resources/GenericResource.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
include_once("model/resources/strategies/AResourceStrategy.class.php");
include_once("model/resources/AResource.class.php");

class GenericResource extends AResource {
private $strategyname;
private $strategy; //this contains the right strategy to handle the call
class GenericResource{

private $package;
private $resource;
private $strategyname;
private $strategy;

public function __construct($package,$resource){
$this->package = $package;
Expand All @@ -37,10 +37,6 @@ public function call(){
$strat = $this->getStrategy();
return $strat->onCall($this->package,$this->resource);
}

public function setParameter($name,$val){
$this->strategy->$name = $val;
}
}

?>
56 changes: 28 additions & 28 deletions model/resources/create/ACreator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,51 @@ abstract class ACreator{

protected $parameters = array();
protected $requiredParameters = array();
protected $optionalParameters = array();

public function __construct(){
$this->parameters["resource_type"] = "The type of the resource.";
$this->parameters["package"] = "Name of the package to add the resource to.";
$this->parameters["resource"] = "Name of the resource.";

$this->requiredParameters["resource_type"] = "";
$this->requiredParameters["package"] = "";
$this->requiredParameters["resource"] = "";
$this->requiredParameters[] = "resource_type";
$this->requiredParameters[] = "package";
$this->requiredParameters[] = "resource";
}

/**
* process the parameters
*/
public function processCreateParameters($parameters){
// process every parameters passed along with the creation requests
// and assign them to the correct parameter belonging to the Creator-class
$allowedParameters = array_keys($this->parameters);
foreach($allowedParameters as $key => $value){
foreach($parameters as $key => $value){
//check whether this parameter is in the documented parameters
if(isset($this->requiredParameters[$key])){
$this->optionalParameters[$key] = $value;
}else if(isset($this->optionalParameters[$key])){
$this->requiredParameters[$key] = $value;
}else{
if(!isset($this->parameters[$key])){
throw new ParameterDoesntExistTDTException($key);
}else if(in_array($key,$this->requiredParameters)){
$this->$key = $value;
}
}
// check if all requiredparameters have been set

foreach($this->requiredParameters as $key => $value){
if($value == ""){
/*
* check if all requiredparameters have been set
*/
foreach($this->requiredParameters as $key){
if($this->$key == ""){
throw new ParameterTDTException("Required parameter ".$key ." has not been passed");
}
}

/*
* set the parameters
*/
foreach($parameters as $key => $value){
$this->setParameter($key,$value);
}
}

/**
* set parameters, we leave this to the subclass
*/
abstract protected function setParameter($key,$value);

/**
* execution method
*/
Expand All @@ -70,13 +77,6 @@ public function getCreateRequiredParameters(){
return $this->requiredParameters;
}

/**
* get the optional parameters
*/
public function getOptionalParameters(){
return $this->optionalParameters;
}

/**
* get the documentation about the addition of a resource
* @return string containing a description about the class
Expand All @@ -93,7 +93,7 @@ protected function makePackage(){
"SELECT package.id as id
FROM package
WHERE package_name=:package_name",
array(":package_name"=>$this->requiredParameters["package"])
array(":package_name"=>$this->package)
);

if(sizeof($result) == 0){
Expand All @@ -117,16 +117,16 @@ protected function makeResource($package_id){
"SELECT resource.id
FROM resource, package
WHERE :package_id = package.id and resource.resource_name =:resource and resource.package_id = package.id",
array(":package_id" => $package_id, ":resource" => $this->requiredParameters["resource"])
array(":package_id" => $package_id, ":resource" => $this->resource)
);

if(sizeof($checkExistence) == 0){
$newResource = R::dispense("resource");
$newResource->package_id = $package_id;
$newResource->resource_name = $this->requiredParameters["resource"];
$newResource->resource_name = $this->resource;
$newResource->creation_timestamp = time();
$newResource->last_update_timestamp = time();
$newResource->type = $this->requiredParameters["resource_type"];
$newResource->type = $this->resource_type;
return R::store($newResource);
}
return $checkExistence[0]["id"];
Expand Down
46 changes: 26 additions & 20 deletions model/resources/create/GenericResourceCreator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,42 @@ public function __construct($resource_type){
/**
* Add the parameters
*/
$this->parameters["generic_type"] = "The generic type of the generic resource.";
$this->parameters["documentation"] = "Some documentation about the generic resource.";
$this->parameters["generic_type"] = "The type of the generic resource.";
$this->parameters["documentation"] = "Some descriptional documentation about the generic resource.";
$this->parameters["printmethods"] = "The allowed formats in which the resulting object may be represented in.";

/**
* Add the required parameters
*/
$this->requiredParameters["documentation"] = "";
$this->requiredParameters["printmethods"] = "";
$this->requiredParameters["generic_type"] = $resource_type;
$this->requiredParameters[]= "documentation";
$this->requiredParameters[] = "printmethods";
$this->requiredParameters[] = "generic_type";
$this->generic_type = $generic_type;

/**
* Add the parameters of the strategy!
*/
if(!file_exists("model/resources/strategies/" . $resource_type . ".class.php")){
if(!file_exists("model/resources/strategies/" . $generic_type . ".class.php")){
throw new ResourceAdditionTDTException("Generic type does not exist");
}
include_once("model/resources/strategies/" . $resource_type . ".class.php");
include_once("model/resources/strategies/" . $generic_type . ".class.php");
// add all the parameters to the $parameters
// and all of the requiredParameters to the $requiredParameters
$strategy = new $resource_type();
$this->parameters[] = $strategy->getParameters();
$this->requiredParameters = array_merge($this->requiredParameters,$strategy->getRequiredParameters());
$this->optionalParameters = array_merge($this->optionalParameters,$strategy->getOptionalParameters());

$this->strategy = new $generic_type();
$this->parameters[] = $this->strategy->getParameters();
$this->requiredParameters[] = $strategy->getRequiredParameters();
}

protected function setParameter($key,$value){
/*
* set the correct parameters, to the this class or the strategy
* we're sure that every key,value passed is correct
*/
if(isset($this->strategy->getParameters[$key])){
$this->strategy->$key = $value;
}else{
$this->$key = $value;
}
}

/**
Expand All @@ -60,15 +71,10 @@ public function create(){
* Then pick the correct strategy, and pass along the parameters!
*/
$package_id = parent::makePackage();
$resource_id = parent::makeResource($package_id);

$generic_type = $this->requiredParameters["generic_type"];
$documentation = $this->requiredParameters["documentation"];
$printmethods = $this->requiredParameters["printmethods"];
$resource_id = parent::makeResource($package_id);

$generic_id = DBQueries::storeGenericResource($resource_id,$generic_type,$documentation,$printmethods);
$allParams = array_merge($this->requiredParameters,$this->optionalParameters);
$strategy->onAdd($package_id,$generic_id,$allParams);
$generic_id= DBQueries::storeGenericResource($resource_id,$this->generic_type,$this->documentation,$this->printmethods);
$strategy->onAdd($package_id,$generic_id);
}

/**
Expand Down
21 changes: 3 additions & 18 deletions model/resources/create/NoCreator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,16 @@
* @author Jan Vansteenlandt
*/

include_once("model/ICreator.php");

/**
* When creating a resource, we always expect a PUT method!
*/
abstract class NoCreator implements ICreator{
class NoCreator extends ACreator{

protected $parameters = array();
protected $requiredParameters = array();

/**
* process the parameters
*/
public function processCreateParameters($parameters){
// process every parameters passed along with the creation requests
// and assign them to the correct parameter belonging to the Creator-class
$allowedParameters = array_keys($this->parameters);
foreach($allowedParameters as $key => $value){
//check whether this parameter is in the documented parameters
if(isset($allowedParameters[$key])){
$this->setParameter($key,$value);
}else{
throw new ParameterDoesntExistTDTException($key);
}
}
public function setParameter($key,$value){

}


Expand Down
14 changes: 8 additions & 6 deletions model/resources/create/RemoteResourceCreator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ public function __construct(){
/**
* Add the required parameters
*/
$this->requiredParameters["base_url"] = "";
$this->requiredParameters["package_name"] = "";
$this->requiredParameters[] = "base_url";
$this->requiredParameters[] = "package_name";
}

protected function setParameter($key,$value){
$this->$key = $value;
}

/**
Expand All @@ -38,15 +42,13 @@ public function __construct(){
public function create(){

// format the base url
$base_url = $this->requiredParameters["base_url"];
$base_url = $this->base_url;
if(substr(strrev($base_url),0,1) != "/"){
$base_url .= "/";
}

$packagename = $this->requiredParameters["package_name"];

// 1. First check if it really exists on the remote server
$url = $base_url."TDTInfo/Resources/" . $content["package_name"] . "/". $resource .".php";
$url = $base_url."TDTInfo/Resources/" . $this->package_name . "/". $this->resource .".php";
$options = array("cache-time" => 1); //cache for 1 second
$request = TDT::HttpRequest($url, $options);
if(isset($request->error)){
Expand Down
36 changes: 25 additions & 11 deletions model/resources/read/AReader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
* @author Jan Vansteenlandt
*/

class AReader{
abstract class AReader{

public static $BASICPARAMS = array("callback", "filterBy","filterValue","filterOp");
// package and resource are always the two minimum parameters
protected $parameters = array();
protected $requiredParameters = array();
protected $optionalParameters = array();
protected $package;
protected $resource;

Expand All @@ -28,21 +27,36 @@ public function __construct($package,$resource){
*/
abstract public function read();

public function processParameters(){
public function processParameters($parameters){
// Check all GET parameters and give them to setParameter, which needs to be handled by the extended method.
foreach($_GET as $key => $value){
foreach($parameters as $key => $value){
//the method and module will already be parsed by another system
//we don't need the format as well, this is used by printer
if(!in_array($key,self::$BASICPARAMS)){
//check whether this parameter is in the documented parameters
$params = $this->getParameters();
if(isset($params[$key])){
$this->setParameter($key,$value);
}else{
throw new ParameterDoesntExistTDTException($key);
}
if(!isset($this->parameters[$key])){
throw new ParameterDoesntExistTDTException($key);
}else if(in_array($key,$this->requiredParameters)){
$this->$key = $value;
}
}
}

/*
* check if all requiredparameters have been set
*/
foreach($this->requiredParameters as $key){
if($this->$key == ""){
throw new ParameterTDTException("Required parameter ".$key ." has not been passed");
}
}

/*
* set the parameters
*/
foreach($parameters as $key => $value){
$this->setParameter($key,$value);
}
}

abstract protected function setParameter($key, $value);
Expand All @@ -60,7 +74,7 @@ public function getRequiredParameters(){
* @return Array with all the optional Read parameters
*/
public function getParameters(){
return $this->optionalParameters;
return $this->parameters;
}

/**
Expand Down
18 changes: 12 additions & 6 deletions model/resources/read/RemoteResourceReader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,32 @@ class RemoteResourceReader extends AReader{
public function __construct($package,$resource){
parent::__construct($package,$resource);
$this->fetchResource();
/*
* add the necessary parameters (optional and required)
*/
$this->parameters[] = $this->remoteResource->parameters;
$this->requiredParameters[] = $this->remoteResource->requiredParameters;

}

/**
* execution method
*/
public function read(){
//extract the right parameters and concatenate them to create the right URL
//extract the right parameters (the non optional ones) and concatenate them to create the right URL
$params = "?";
foreach($this->optionalparams as $key => $val){
$params .= $key . "=" . urlencode($val) . "&";
foreach(array_keys($this->parameters) as $key){
if(!isset($this->requiredParameters[$key]) && isset($this->$key)){
$params .= $key . "=" . urlencode($this->$key) . "&";
}
}
$params = rtrim($params, "&");

//the url consists of the baseurl (this has a trailing slash and contains the subdir) - the resource is a specifier in the baseurl
//params is a url containing the possible
$url = $this->base_url . $this->package . "/".$this->resource . "/";
foreach($this->requiredparametervalues as $param){
$url = $url . $param."/";
foreach($this->requiredParameters as $param){
$url = $url . $this->$param."/";
}
$url= rtrim($url, "/");
//add format: php because we're going to deserialize this
Expand Down Expand Up @@ -77,7 +84,6 @@ private function fetchResource(){
}

protected function setParameter($name,$val){
//add the parameters to $this
$this->$name = $val;
}

Expand Down
Loading

0 comments on commit 315fb7f

Please sign in to comment.