<?php
/*
 * $RCSfile: GalleryEntityHelper_simple.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @version $Revision: 1.12 $ $Date: 2005/08/23 03:49:04 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * Helper class for GalleryEntities
 *
 * @package GalleryCore
 * @subpackage Helpers
 * @abstract
 */
class GalleryEntityHelper_simple {

    /**
     * Load the GalleryEntities with the ids specified
     *
     * @param mixed the ids (or id) of the GalleryEntities to load
     * @return array object GalleryStatus a status code,
     *               mixed one GalleryEntity or an array of GalleryEntities
     * @static
     */
    function loadEntitiesById($ids) {
	global $gallery;
	$gallery->guaranteeTimeLimit(5);

	if (empty($ids)) {
	    return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__), null);
	}

	if (is_array($ids)) {
	    $returnArray = true;
	} else {
	    $returnArray = false;
	    $ids = array($ids);
	}

	/* Grab what we can from the cache and make up a list of what's missing */
	$cached = $missing = $onLoad = array();
	foreach ($ids as $id) {
	    $cacheKey = "GalleryEntityHelper::loadEntitiesById($id)";
	    if (GalleryDataCache::containsKey($cacheKey)) {
		$cached[$id] = GalleryDataCache::get($cacheKey);
	    } else {
		$data =& GalleryDataCache::getFromDisk(array('type' => 'entity', 'itemId' => $id));
		if (isset($data)) {
		    $cached[$id] =& $data;
		} else {
		    $missing[] = $id;
		}
		$onLoad[] = $id;
	    }
	}

	if (!empty($missing)) {
	    /* Get our storage object */
	    $storage =& $gallery->getStorage();

	    /* Load the entities from our persistent store */
	    list ($ret, $missing) = $storage->loadEntities($missing);
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	}

	/* Move the new entities into the cache */
	foreach ($missing as $entity) {
	    $id = $entity->getId();
	    GalleryDataCache::putToDisk(array('type' => 'entity', 'itemId' => $id),
					$entity, array($entity->getClassFile()));
	    $cached[$id] = $entity;
	}

	/* Let each entity do its post-load procedure */
	foreach ($onLoad as $id) {
	    $ret = $cached[$id]->onLoad();
	    if ($ret->isError()) {
		return array($ret->wrap(__FILE__, __LINE__), null);
	    }
	    GalleryDataCache::put("GalleryEntityHelper::loadEntitiesById($id)", $cached[$id]);
	}

	/* Build up the results from the cache */
	$results = array();
	foreach ($ids as $id) {
	    $results[] = $cached[$id];
	}

	if (!$returnArray) {
	    $results = $results[0];
	}

	return array(GalleryStatus::success(), $results);
    }

    /**
     * Map external id to G2 id and then load the entity
     *
     * @param string external id
     * @param string entity type
     * @return array object GalleryStatus a status code
     *               object GalleryEntity
     * @static
     */
    function loadEntityByExternalId($externalId, $entityType) {
	global $gallery;

	$query = 'SELECT [ExternalIdMap::entityId] FROM [ExternalIdMap] ' .
		 'WHERE [ExternalIdMap::externalId] = ? AND [ExternalIdMap::entityType] = ?';
	list ($ret, $results) = $gallery->search($query, array($externalId, $entityType));
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}
	if (!($result = $results->nextResult())) {
	    return array(GalleryStatus::error(ERROR_MISSING_OBJECT, __FILE__, __LINE__,
					      "$externalId $entityType"), null);
	}

	list ($ret, $entity) = GalleryEntityHelper_simple::loadEntitiesById($result[0]);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	return array(GalleryStatus::success(), $entity);
    }
}
?>
