/*
  * (non-Javadoc)
  * @see org.jasig.schedassist.MutableRelationshipDao#createRelationship(org.jasig.schedassist.model.IScheduleOwner, org.jasig.schedassist.model.IScheduleVisitor, java.lang.String)
  */
 @Override
 public Relationship createRelationship(
     IScheduleOwner owner, IScheduleVisitor visitor, String relationship) {
   final String ownerIdentifier = getIdentifyingAttribute(owner.getCalendarAccount());
   final String visitorIdentifier = getIdentifyingAttribute(visitor.getCalendarAccount());
   OwnerDefinedRelationship stored =
       internalRetrieveRelationship(ownerIdentifier, visitorIdentifier);
   if (null == stored) {
     this.simpleJdbcTemplate.update(
         "insert into owner_adhoc_authz (owner_username, relationship, visitor_username) values (?, ?, ?)",
         ownerIdentifier,
         relationship,
         visitorIdentifier);
     LOG.info(
         "stored owner defined relationship: " + owner + ", " + relationship + ", " + visitor);
   } else {
     this.simpleJdbcTemplate.update(
         "update owner_adhoc_authz set relationship = ? where owner_username = ? and visitor_username = ?",
         relationship,
         ownerIdentifier,
         visitorIdentifier);
     LOG.info(
         "relationship already exists for " + owner + " and " + visitor + ", updated description");
   }
   Relationship result = new Relationship();
   result.setOwner(owner);
   result.setVisitor(visitor);
   result.setDescription(relationship);
   return result;
 }
  /*
   * (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;
  }
  /**
   * @param visitor
   * @param ownerId
   * @return
   * @throws OwnerNotFoundException
   */
  private IScheduleOwner findOwnerForVisitor(final IScheduleVisitor visitor, final long ownerId)
      throws OwnerNotFoundException {
    List<Relationship> relationships = relationshipDao.forVisitor(visitor);
    for (Relationship potential : relationships) {
      if (potential.getOwner().getId() == ownerId) {
        return potential.getOwner();
      }
    }

    throw new OwnerNotFoundException("owner id " + ownerId + " not found");
  }