/**********************************************************************************
 * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-3-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/shared/mgt/impl/FileArtifactFinder.java $
 * $Id: FileArtifactFinder.java 14225 2006-09-05 17:39:44Z chmaurer@iupui.edu $
 ***********************************************************************************
 *
 * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
 * 
 * Licensed under the Educational Community License, Version 1.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *      http://www.opensource.org/licenses/ecl1.php
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *
 **********************************************************************************/

package org.sakaiproject.metaobj.shared.mgt.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.sakaiproject.content.api.ContentHostingService;
import org.sakaiproject.content.api.ContentResource;
import org.sakaiproject.entity.api.ResourceProperties;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.exception.PermissionException;
import org.sakaiproject.exception.TypeException;
import org.sakaiproject.metaobj.shared.ArtifactFinder;
import org.sakaiproject.metaobj.shared.mgt.AgentManager;
import org.sakaiproject.metaobj.shared.mgt.IdManager;
import org.sakaiproject.metaobj.shared.mgt.ReadableObjectHome;
import org.sakaiproject.metaobj.shared.model.Agent;
import org.sakaiproject.metaobj.shared.model.Artifact;
import org.sakaiproject.metaobj.shared.model.ContentResourceArtifact;
import org.sakaiproject.metaobj.shared.model.Id;
import org.sakaiproject.metaobj.shared.model.MimeType;

/**
 * Created by IntelliJ IDEA.
 * User: John Ellis
 * Date: Aug 17, 2005
 * Time: 2:08:27 PM
 * To change this template use File | Settings | File Templates.
 */
public class FileArtifactFinder implements ArtifactFinder {

   private ContentHostingService contentHostingService;
   private AgentManager agentManager;
   private IdManager idManager;
   private ReadableObjectHome contentResourceHome = null;

   public Collection findByOwnerAndType(Id owner, String type) {
      return findByOwnerAndType(owner, type, null);
   }

   public Collection findByOwnerAndType(Id owner, String type, MimeType mimeType) {
      String primaryMimeType = null;
      String subMimeType = null;

      if (mimeType != null) {
         primaryMimeType = mimeType.getPrimaryType();
         subMimeType = mimeType.getSubType();
      }

      List artifacts = getContentHostingService().findResources(ResourceProperties.FILE_TYPE,
            primaryMimeType, subMimeType);

      Collection returned = new ArrayList();

      for (Iterator i = artifacts.iterator(); i.hasNext();) {
         ContentResource resource = (ContentResource) i.next();
         ContentResourceArtifact resourceArtifact = createArtifact(resource);
         returned.add(resourceArtifact);
      }

      return returned;
   }

   protected ContentResourceArtifact createArtifact(ContentResource resource) {
      Agent resourceOwner = getAgentManager().getAgent(resource.getProperties().getProperty(ResourceProperties.PROP_CREATOR));
      Id resourceId = getIdManager().getId(getContentHostingService().getUuid(resource.getId()));
      ContentResourceArtifact resourceArtifact = new ContentResourceArtifact(resource, resourceId, resourceOwner);
      return resourceArtifact;
   }

   public Collection findByOwner(Id owner) {
      return null;
   }

   public Collection findByWorksiteAndType(Id worksiteId, String type) {
      return null;
   }

   public Collection findByWorksite(Id worksiteId) {
      return null;
   }

   public Artifact load(Id artifactId) {
      String resourceId = getContentHostingService().resolveUuid(artifactId.getValue());

      if (resourceId == null) {
         return null;
      }

      try {
         ContentResource resource = getContentHostingService().getResource(resourceId);
         Artifact returned = createArtifact(resource);
         returned.setHome(getContentResourceHome());
         return returned;
      }
      catch (PermissionException e) {
         throw new RuntimeException(e);
      }
      catch (IdUnusedException e) {
         throw new RuntimeException(e);
      }
      catch (TypeException e) {
         throw new RuntimeException(e);
      }
   }

   public Collection findByType(String type) {
      return null;
   }

   public boolean getLoadArtifacts() {
      return false;
   }

   public void setLoadArtifacts(boolean loadArtifacts) {
   }

   public ContentHostingService getContentHostingService() {
      return contentHostingService;
   }

   public void setContentHostingService(ContentHostingService contentHostingService) {
      this.contentHostingService = contentHostingService;
   }

   public AgentManager getAgentManager() {
      return agentManager;
   }

   public void setAgentManager(AgentManager agentManager) {
      this.agentManager = agentManager;
   }

   public IdManager getIdManager() {
      return idManager;
   }

   public void setIdManager(IdManager idManager) {
      this.idManager = idManager;
   }

   public ReadableObjectHome getContentResourceHome() {
      return contentResourceHome;
   }

   public void setContentResourceHome(ReadableObjectHome contentResourceHome) {
      this.contentResourceHome = contentResourceHome;
   }

}
