/**
  * Create an asset object, bridging this class and the Asset classes
  * @param string 
  * @param string $type The type of the asset
  * @param string $id_path Either the ID string or the path of the asset
  * @param binary $site_name The site name
  *
  * @return mixed The asset object
  * @throw Exception if the asset cannot be retrieved
  */
 public function getAsset($type, $id_path, $site_name = NULL)
 {
     if (!in_array($type, c\T::getTypeArray())) {
         throw new e\NoSuchTypeException(S_SPAN . "The type {$type} does not exist." . E_SPAN);
     }
     $class_name = c\T::$type_class_name_map[$type];
     // get class name
     $class_name = a\Asset::NAME_SPACE . "\\" . $class_name;
     try {
         return new $class_name($this, $this->createId($type, $id_path, $site_name));
     } catch (\Exception $e) {
         if (self::DEBUG && self::DUMP) {
             u\DebugUtility::out($e->getMessage());
         }
         throw $e;
     }
 }
 public static function assetTreeUpdateTemplate(aohs\AssetOperationHandlerService $service, p\Child $child, $params = NULL, &$results = NULL)
 {
     if (isset($params['source-site'])) {
         $source_site = $params['source-site'];
     } else {
         echo c\M::TARGET_SITE_NOT_SET . BR;
         return;
     }
     if (isset($params['target-cascade'])) {
         $target_cascade = $params['target-cascade'];
     } else {
         echo c\M::TARGET_CASCADE_NOT_SET . BR;
         return;
     }
     if (isset($params['target-site'])) {
         $target_site = $params['target-site'];
     } else {
         echo c\M::TARGET_SITE_NOT_SET . BR;
         return;
     }
     if (isset($params['exception-thrown'])) {
         $exception_thrown = $params['exception-thrown'];
     } else {
         echo c\M::EXCEPTION_THROWN_NOT_SET . BR;
         return;
     }
     $source_t = $child->getAsset($service);
     $source_content = $source_t->getXml();
     $source_t_path = u\StringUtility::removeSiteNameFromPath($source_t->getPath());
     $source_t_path_array = u\StringUtility::getExplodedStringArray("/", $source_t_path);
     $source_t_path = $source_t_path_array[count($source_t_path_array) - 1];
     $source_t_parent_path = $source_t->getParentContainerPath();
     // create template
     $target_site_name = $target_site->getName();
     // parent must be there
     $target_parent = $target_cascade->getAsset(Folder::TYPE, $source_t_parent_path, $target_site_name);
     $target_t = $target_cascade->createTemplate($target_parent, $source_t_path, $source_content);
     // set xml, must call edit because there may not be blocks and formats
     $target_t->setXml($source_content)->edit();
     // set format
     $source_format = $source_t->getFormat();
     if (isset($source_format)) {
         $source_format_path = u\StringUtility::removeSiteNameFromPath($source_format->getPath());
         $source_format_site = $source_format->getSiteName();
         $target_format_site = $source_format_site;
         try {
             if ($exception_thrown) {
                 $format = $target_cascade->getAsset(XsltFormat::TYPE, $source_format_path, $target_format_site);
             } else {
                 $format = $target_cascade->getXsltFormat($source_format_path, $target_format_site);
             }
             $target_t->setFormat($format)->edit();
         } catch (\Exception $e) {
             $msg = "The format {$source_format_path} does not exist in {$target_format_site}. ";
             throw new e\CascadeInstancesErrorException(S_SPAN . $msg . E_SPAN . $e);
         }
     }
     // set region formats and blocks
     $region_names = $source_t->getPageRegionNames();
     if (count($region_names) > 0) {
         // block
         foreach ($region_names as $region) {
             $block = $source_t->getPageRegionBlock($region);
             if (!isset($block)) {
                 continue;
             }
             $type = $block->getType();
             $source_block_path = u\StringUtility::removeSiteNameFromPath($block->getPath());
             $source_block_site = $block->getSiteName();
             $target_block_site = $source_block_site;
             try {
                 if ($exception_thrown) {
                     $target_block = $target_cascade->getAsset($type, $source_block_path, $target_block_site);
                 } else {
                     $class_name = c\T::getClassNameByType($type);
                     $method = 'get' . $class_name;
                     $target_block = $target_cascade->{$method}($source_block_path, $target_block_site);
                 }
                 $target_t->setPageRegionBlock($region, $target_block)->edit();
             } catch (\Exception $e) {
                 $msg = "The block {$source_block_path} does not exist in {$target_block_site}. ";
                 throw new e\CascadeInstancesErrorException(S_SPAN . $msg . E_SPAN . $e);
             }
         }
         // format
         foreach ($region_names as $region) {
             $format = $source_t->getPageRegionFormat($region);
             if (!isset($format)) {
                 continue;
             }
             $type = $format->getType();
             $source_format_path = u\StringUtility::removeSiteNameFromPath($format->getPath());
             $source_format_site = $format->getSiteName();
             $target_format_site = $source_format_site;
             try {
                 if ($exception_thrown) {
                     $target_format = $target_cascade->getAsset($type, $source_format_path, $target_format_site);
                 } else {
                     $class_name = c\T::getClassNameByType($type);
                     $method = 'get' . $class_name;
                     $target_format = $target_cascade->{$method}($source_format_path, $target_format_site);
                 }
                 $target_t->setPageRegionFormat($region, $target_format)->edit();
             } catch (\Exception $e) {
                 $msg = "The format {$source_format_path} does not exist in {$target_format_site}. ";
                 throw new e\CascadeInstancesErrorException(S_SPAN . $msg . E_SPAN . $e);
             }
         }
     }
 }