/**
  * Returns the root view controller for this current instance of the application.
  * The root view controller is returned depending on which parameters are called.
  * The Application class parses the request URL and breaks down the different elements
  * into controller url, and parameters. This determines which view controller should
  * be instanced and called. The appropriate view controller is instanced according to
  * those parameters and returned by this method.
  *
  * @return MViewController The root view controller for this instance of the application
  */
 protected function rootViewController()
 {
     if (!$this->_rootViewController) {
         $this->_rootViewController = $this->defaultNamespace()->viewControllerForPath(MHTTPRequest()->arguments());
     }
     return $this->_rootViewController;
 }
 /**
  * 
  *
  * @return MString
  */
 public function address()
 {
     return MHTTPRequest()->url();
 }
 /**
  * 
  */
 public function loadView()
 {
     parent::loadView();
     if (MHTTPRequest()->method() == MHTTPRequest::REQUEST_METHOD_GET) {
         $objects = null;
         if ($this->objectID()) {
             $object = $this->context()->objectWithObjectID($this->entity(), $this->objectID()->intValue());
             if ($object) {
                 $arr = A($object);
                 if ($this->shouldReturnObjects($arr)) {
                     $object->fireFault();
                     $objects = $arr;
                 } else {
                     $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                 }
             } else {
                 $this->setResponseCode(MHTTPResponse::RESPONSE_NOT_FOUND);
             }
         } else {
             $request = new MFetchRequest($this->entity(), $this->context());
             if ($this->predicate()) {
                 $request->setPredicate($this->predicate());
             }
             $arr = $this->context()->executeFetchRequest($request);
             if ($arr->count() > 0) {
                 if ($this->shouldReturnObjects($arr)) {
                     $objects = $arr;
                 } else {
                     $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                 }
             } else {
                 $this->setResponseCode(MHTTPResponse::RESPONSE_NOT_FOUND);
             }
         }
         if ($objects) {
             $xml = new MXMLDocumentView();
             $objectsXML = new MXMLEntityCollectionView($this->entity());
             foreach ($objects->toArray() as $object) {
                 $objectsXML->addManagedObject($object);
             }
             $xml->addSubview($objectsXML);
             $this->setView($xml);
         }
     } else {
         if (MHTTPRequest()->method() == MHTTPRequest::REQUEST_METHOD_PUT) {
             if ($this->objectID()) {
                 $object = $this->context()->objectWithObjectID($this->entity(), $this->objectID()->intValue());
                 if ($object) {
                     if ($this->shouldUpdateObject($object)) {
                         $this->_updateObjectWithInputData($object);
                         $this->context()->save();
                     } else {
                         $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                     }
                 } else {
                     $this->setResponseCode(MHTTPResponse::RESPONSE_NOT_FOUND);
                 }
             } else {
                 $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
             }
         } else {
             if (MHTTPRequest()->method() == MHTTPRequest::REQUEST_METHOD_POST) {
                 if ($this->objectID()) {
                     $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                 } else {
                     $object = $this->_createNewObjectWithInputData($this->entity());
                     if ($object) {
                         if ($this->shouldInsertObject($object)) {
                             $this->context()->save();
                             $this->setView(new MPlainTextView(Sf("%s/%s", $this->url(), N($object->objectID())->toString())));
                         } else {
                             $this->context()->deleteObject($object);
                             $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                         }
                     } else {
                         $this->setResponseCode(MHTTPResponse::RESPONSE_BAD_REQUEST);
                     }
                 }
             } else {
                 if (MHTTPRequest()->method() == MHTTPRequest::REQUEST_METHOD_DELETE) {
                     if ($this->objectID()) {
                         $object = $this->context()->objectWithObjectID($this->entity(), $this->objectID()->intValue());
                         if ($object) {
                             if ($this->shouldDeleteObject($object)) {
                                 $this->context()->deleteObject($object);
                                 $this->context()->save();
                             } else {
                                 $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                             }
                         } else {
                             $this->setResponseCode(MHTTPResponse::RESPONSE_NOT_FOUND);
                         }
                     } else {
                         $this->setResponseCode(MHTTPResponse::RESPONSE_FORBIDDEN);
                     }
                 }
             }
         }
     }
 }