/*
   * (non-Javadoc)
   * @see org.jasig.schedassist.RelationshipDao#forOwner(org.jasig.schedassist.model.IScheduleOwner)
   */
  @Override
  public List<Relationship> forOwner(final IScheduleOwner owner) {
    final String ownerIdentifier = getIdentifyingAttribute(owner.getCalendarAccount());

    List<OwnerDefinedRelationship> relationships =
        this.simpleJdbcTemplate.query(
            "select * from owner_adhoc_authz where owner_username = ?",
            new OwnerDefinedRelationshipRowMapper(),
            ownerIdentifier);

    List<Relationship> results = new ArrayList<Relationship>();
    for (OwnerDefinedRelationship stored : relationships) {
      ICalendarAccount calendarUser =
          calendarAccountDao.getCalendarAccount(
              identifyingAttributeName, stored.getVisitorUsername());
      if (null == calendarUser) {
        LOG.info("calendarAccount not found for owner in " + stored);
        continue;
      }
      try {
        IScheduleVisitor visitor = visitorDao.toVisitor(calendarUser);

        Relationship relationship = new Relationship();
        relationship.setOwner(owner);
        relationship.setVisitor(visitor);
        relationship.setDescription(stored.getRelationship());

        results.add(relationship);
      } catch (NotAVisitorException e) {
        LOG.info("calendarAccount found but not a visitor " + stored);
      }
    }
    return results;
  }
  /*
   * (non-Javadoc)
   * @see org.jasig.schedassist.RelationshipDao#forVisitor(org.jasig.schedassist.model.IScheduleVisitor)
   */
  @Override
  public List<Relationship> forVisitor(final IScheduleVisitor visitor) {
    final String visitorIdentifier = getIdentifyingAttribute(visitor.getCalendarAccount());
    List<OwnerDefinedRelationship> relationships =
        this.simpleJdbcTemplate.query(
            "select * from owner_adhoc_authz where visitor_username = ?",
            new OwnerDefinedRelationshipRowMapper(),
            visitorIdentifier);

    List<Relationship> results = new ArrayList<Relationship>();
    for (OwnerDefinedRelationship stored : relationships) {
      ICalendarAccount calendarUser =
          calendarAccountDao.getCalendarAccount(
              identifyingAttributeName, stored.getOwnerUsername());
      if (null == calendarUser) {
        LOG.info("calendarAccount not found for owner in " + stored);
        continue;
      }

      IScheduleOwner owner = ownerDao.locateOwner(calendarUser);
      if (null != owner) {
        Relationship relationship = new Relationship();
        relationship.setOwner(owner);
        relationship.setVisitor(visitor);
        relationship.setDescription(stored.getRelationship());

        results.add(relationship);
      } else {
        LOG.warn("no ScheduleOwner registered for record " + stored);
      }
    }
    return results;
  }