/** Format the Date as String, using the specified DateFormat. */
 public String getAsText() {
   if (!(getValue() instanceof Id)) {
     return null;
   }
   Id value = (Id) getValue();
   return value.getValue();
 }
  public Collection findByOwnerAndType(Id owner, String type) {

    if (owner == null) {
      log.info("Null owner passed to findByOwnerAndType -- returning all users' forms");
      return findByType(type);
    }

    Set siteIds = new TreeSet();
    Site site = null;
    List siteList =
        org.sakaiproject.site.cover.SiteService.getSites(
            org.sakaiproject.site.api.SiteService.SelectionType.ACCESS,
            null,
            null,
            null,
            org.sakaiproject.site.api.SiteService.SortType.NONE,
            null);

    // find all sites user has access to
    for (Iterator it = siteList.iterator(); it.hasNext(); ) {
      site = (Site) it.next();
      siteIds.add(site.getId());
    }

    // add user MyWorkspace site
    try {
      site = SiteService.getSite(SiteService.getUserSiteId(owner.getValue()));
      siteIds.add(site.getId());
    } catch (Exception e) {
      log.info("findOwnerAndType", e);
    }

    Collection<ContentResource> artifacts =
        getContentHostingService().getContextResourcesOfType(ResourceType.TYPE_METAOBJ, siteIds);

    ArrayList<ContentResourceArtifact> returned = new ArrayList<ContentResourceArtifact>();

    for (Iterator<ContentResource> i = artifacts.iterator(); i.hasNext(); ) {
      ContentResource resource = i.next();
      Agent resourceOwner =
          getAgentManager()
              .getAgent(resource.getProperties().getProperty(ResourceProperties.PROP_CREATOR));
      String actualType =
          resource.getProperties().getProperty(ResourceProperties.PROP_STRUCTOBJ_TYPE);

      // filter list for owner and form type
      if (owner == null
          || owner.equals(resourceOwner.getId()) && (type == null || type.equals(actualType))) {

        Id resourceId = getIdManager().getId(getContentHostingService().getUuid(resource.getId()));
        returned.add(new ContentResourceArtifact(resource, resourceId, resourceOwner));
      }
    }
    return returned;
  }
  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();
      // FIXME: Refactor to avoid double trips to AgentManager on every artifact -- Agent user type
      // gives performance hits
      Agent resourceOwner =
          getAgentManager()
              .getAgent(resource.getProperties().getProperty(ResourceProperties.PROP_CREATOR));
      if (owner == null || owner.equals(resourceOwner.getId())) {
        Artifact resourceArtifact = createArtifact(resource);
        returned.add(resourceArtifact);
      }
    }

    return returned;
  }
  /* (non-Javadoc)
   * @see org.theospi.utils.mvc.intf.Controller#handleRequest(java.lang.Object, java.util.Map, java.util.Map, java.util.Map, org.springframework.validation.Errors)
   */
  public ModelAndView handleRequest(
      Object requestModel, Map request, Map session, Map application, Errors errors) {
    WizardPage page = (WizardPage) session.get(WizardPageHelper.WIZARD_PAGE);
    Id cellId = idManager.getId((String) request.get("page_id"));
    Id formId = idManager.getId((String) request.get("current_form_id"));
    Cell cell = getMatrixManager().getCellFromPage(cellId);
    boolean sessionPage = true;
    if (page == null) {
      sessionPage = false;
      page = cell.getWizardPage();
    }

    String submitAction = (String) request.get("submit");
    session.remove(WizardPageHelper.WIZARD_PAGE);
    getMatrixManager().removeFromSession(page);
    if (submitAction.equals(DELETE_FORM)) {
      getMatrixManager().detachForm(page.getId(), formId);
    } else // (submitAction.equals(DELETE_FEEDBACK))
    {
      Id reviewId = idManager.getId((String) request.get("review_id"));
      Review review = getReviewManager().getReview(reviewId);
      if (review != null) getReviewManager().deleteReview(review);
      else logger.warn("Null feedback form (perhaps multiple submits):" + reviewId);
    }

    if (sessionPage)
      session.put(WizardPageHelper.WIZARD_PAGE, getMatrixManager().getWizardPage(page.getId()));

    try {
      // unlock and delete content
      String reviewContentId = contentHosting.getUuid(formId.getValue());
      if (getLockManager().isLocked(reviewContentId))
        getLockManager().removeLock(reviewContentId, cellId.getValue());
      getContentHosting().removeResource(formId.getValue());
    } catch (Exception e) {
      logger.warn("Error removing form: " + e.toString());
    }

    // if not submit, then cancel, but both submit and cancel have the some view, so
    return new ModelAndView("continue", "page_id", page.getId().getValue());
  }
  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, artifactId);
      return returned;
    } catch (PermissionException e) {
      throw new RuntimeException(e);
    } catch (IdUnusedException e) {
      throw new RuntimeException(e);
    } catch (TypeException e) {
      throw new RuntimeException(e);
    }
  }
  /* (non-Javadoc)
   * @see org.theospi.portfolio.security.app.ApplicationAuthorizer#isAuthorized(org.theospi.portfolio.security.AuthorizationFacade, org.theospi.portfolio.shared.model.Agent, java.lang.String, org.theospi.portfolio.shared.model.Id)
   */
  public Boolean isAuthorized(AuthorizationFacade facade, Agent agent, String function, Id id) {
    logger.debug("isAuthorized?(...) invoked in MatrixAuthorizer");

    if (MatrixFunctionConstants.EVALUATE_MATRIX.equals(function)
        || MatrixFunctionConstants.REVIEW_MATRIX.equals(function)
        || MatrixFunctionConstants.USE_SCAFFOLDING.equals(function)) {
      return new Boolean(facade.isAuthorized(function, id));
    } else if (MatrixFunctionConstants.DELETE_SCAFFOLDING.equals(function)) {
      Scaffolding scaffolding = getMatrixManager().getScaffolding(id);
      if (scaffolding == null) return new Boolean(facade.isAuthorized(agent, function, id));

      if (!scaffolding.isPublished() && (scaffolding.getOwner().equals(agent))
          || facade.isAuthorized(agent, function, scaffolding.getWorksiteId()))
        return new Boolean(true);
    } else if (ContentHostingService.EVENT_RESOURCE_READ.equals(function)) {
      return isFileAuth(facade, agent, id);
    } else if (function.equals(MatrixFunctionConstants.CREATE_SCAFFOLDING)) {
      return new Boolean(facade.isAuthorized(agent, function, id));
    } else if (function.equals(MatrixFunctionConstants.EDIT_SCAFFOLDING)) {
      return new Boolean(facade.isAuthorized(agent, function, id));
    } else if (function.equals(MatrixFunctionConstants.EXPORT_SCAFFOLDING)) {
      return new Boolean(facade.isAuthorized(agent, function, id));
    } else if (function.equals(MatrixFunctionConstants.VIEW_SCAFFOLDING_GUIDANCE)) {
      // If I can eval, review, or own it
      ScaffoldingCell sCell = getMatrixManager().getScaffoldingCellByWizardPageDef(id);
      // sCell.getWizardPageDefinition().get

      if (sCell == null)
        throw new NullPointerException(
            "The cell was not found.  Wizard Page Def for cell: " + id.getValue());

      Boolean returned = null;

      Id worksiteId = sCell.getScaffolding().getWorksiteId();

      // first check global perms for the site
      if (checkPerms(
          facade,
          new String[] {
            MatrixFunctionConstants.USE_SCAFFOLDING,
            MatrixFunctionConstants.EVALUATE_MATRIX,
            MatrixFunctionConstants.REVIEW_MATRIX
          },
          worksiteId)) {
        return Boolean.valueOf(true);
      }

      for (Iterator iter = sCell.getCells().iterator(); iter.hasNext(); ) {
        Cell cell = (Cell) iter.next();
        if (checkPerms(
            facade,
            new String[] {
              MatrixFunctionConstants.EVALUATE_MATRIX, MatrixFunctionConstants.REVIEW_MATRIX
            },
            cell.getId())) {
          return Boolean.valueOf(true);
        }
      }
      returned = Boolean.valueOf(sCell.getScaffolding().getOwner().equals(agent));
      if (returned.booleanValue()) return returned;
    } else if (function.equals(MatrixFunctionConstants.EDIT_SCAFFOLDING_GUIDANCE)) {
      ScaffoldingCell sCell = getMatrixManager().getScaffoldingCellByWizardPageDef(id);
      Agent owner = null;
      if (sCell != null) {
        owner = sCell.getScaffolding().getOwner();
      }
      return new Boolean(agent.equals(owner));
    } else if (function.equals(MatrixFunctionConstants.EVALUATE_SPECIFIC_MATRIXCELL)) {
      WizardPage page = getMatrixManager().getWizardPage(id);
      Id siteId = idManager.getId(page.getPageDefinition().getSiteId());
      //       make sure that the target site gets tested

      facade.pushAuthzGroups(siteId.getValue());
      return new Boolean(
          facade.isAuthorized(agent, MatrixFunctionConstants.EVALUATE_MATRIX, siteId));
    }

    return null; // don't care
  }