Beispiel #1
0
  /**
   * Adds a link.
   *
   * @param owner the owning Entity
   * @param pathIn the path (url, filepath, or entity key)
   * @param linkType the link type
   * @throws Exception the exception
   */
  public void addLink(KeyedEntity<?> owner, String pathIn, LinkType linkType) throws Exception {
    String path = pathIn;
    if (owner == null) {
      Errmsg.getErrorHandler().notice(Resource.getResourceString("att_owner_null"));
      return;
    }

    if (linkType == LinkType.ATTACHMENT) {

      String atfolder = attachmentFolder();
      if (atfolder == null) throw new Exception("attachments not supported");

      // need to copy file and create new path
      File orig = new File(path);
      String fname = orig.getName();
      String newpath = atfolder + "/" + fname;

      int i = 1;
      while (true) {
        File newfile = new File(newpath);
        if (!newfile.exists()) break;

        fname = Integer.toString(i) + orig.getName();
        newpath = atfolder + "/" + fname;
        i++;
      }

      copyFile(path, newpath);
      path = fname;
    }

    Link at = newLink();
    at.setKey(-1);
    at.setOwnerKey(new Integer(owner.getKey()));
    LinkType type = typemap.get(owner.getClass());
    if (type == null) throw new Exception("illegal link owner type");
    at.setOwnerType(type.toString());
    at.setPath(path);
    at.setLinkType(linkType.toString());
    saveLink(at);

    // * modification for backtrace/2way link

    if (linkType == LinkType.ADDRESS
        || linkType == LinkType.APPOINTMENT
        || linkType == LinkType.PROJECT
        || linkType == LinkType.TASK) {
      Link at2way = newLink();
      at2way.setKey(-1);
      at2way.setOwnerKey(new Integer(at.getPath()));
      at2way.setOwnerType(at.getLinkType());
      at2way.setPath(at.getOwnerKey().toString());
      at2way.setLinkType(at.getOwnerType());
      saveLink(at2way);
    }
    // end of modification

  }
Beispiel #2
0
 /**
  * Delete a link
  *
  * @param l the Link
  * @throws Exception the exception
  */
 public void delete(Link l) throws Exception {
   if (l.getLinkType().equals(LinkType.ATTACHMENT.toString())) {
     // delete attached file
     File f = new File(attachmentFolder() + "/" + l.getPath());
     f.delete();
   }
   db_.delete(l.getKey());
   refresh();
 }
Beispiel #3
0
  /* (non-Javadoc)
   * @see net.sf.borg.model.db.EntityDB#addObj(net.sf.borg.model.entity.KeyedEntity)
   */
  @Override
  public void addObj(Link att) throws Exception {
    PreparedStatement stmt =
        JdbcDB.getConnection()
            .prepareStatement(
                "INSERT INTO links ( id, linktype, ownerkey, ownertype, path) "
                    + " VALUES "
                    + "( ?, ?, ?, ?, ?)");

    stmt.setInt(1, att.getKey());
    stmt.setString(2, att.getLinkType());
    stmt.setInt(3, JdbcDB.toInt(att.getOwnerKey()));
    stmt.setString(4, att.getOwnerType());
    stmt.setString(5, att.getPath());

    stmt.executeUpdate();
    stmt.close();

    writeCache(att);
  }
Beispiel #4
0
  /* (non-Javadoc)
   * @see net.sf.borg.model.db.EntityDB#updateObj(net.sf.borg.model.entity.KeyedEntity)
   */
  @Override
  public void updateObj(Link att) throws Exception {

    PreparedStatement stmt =
        JdbcDB.getConnection()
            .prepareStatement(
                "UPDATE links SET linktype = ?, ownerkey = ?, ownertype = ?, path = ?"
                    + " WHERE id = ?");

    stmt.setString(1, att.getLinkType());
    stmt.setInt(2, att.getOwnerKey().intValue());
    stmt.setString(3, att.getOwnerType());
    stmt.setString(4, att.getPath());
    stmt.setInt(5, att.getKey());

    stmt.executeUpdate();
    stmt.close();

    delCache(att.getKey());
    writeCache(att);
  }
Beispiel #5
0
  /**
   * Delete links that target a given entity
   *
   * @param target the target entity object
   * @throws Exception the exception
   */
  public void deleteLinksToEntity(Object target) throws Exception {

    if (target == null) return;

    LinkType type = typemap.get(target.getClass());
    if (type == null) return;

    Collection<Link> links = getLinks();
    for (Link link : links) {
      if (link.getLinkType().equals(type.toString())) {
        if ((type == LinkType.MEMO && ((Memo) target).getMemoName().equals(link.getPath()))
            || (type == LinkType.CHECKLIST
                && ((CheckList) target).getCheckListName().equals(link.getPath()))) {
          delete(link);
        } else if (target instanceof KeyedEntity<?>) {
          int key = ((KeyedEntity<?>) target).getKey();
          if (link.getPath().equals(Integer.toString(key))) {
            delete(link);
          }
        }
      }
    }
  }