Ejemplo n.º 1
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();
 }
Ejemplo n.º 2
0
 /**
  * Move links from one object to another
  *
  * @param oldOwner the old owner
  * @param newOwner the new owner
  * @throws Exception the exception
  */
 public void moveLinks(KeyedEntity<?> oldOwner, KeyedEntity<?> newOwner) throws Exception {
   Collection<Link> atts = getLinks(oldOwner);
   Iterator<Link> it = atts.iterator();
   while (it.hasNext()) {
     Link at = it.next();
     at.setOwnerKey(new Integer(newOwner.getKey()));
     LinkType type = typemap.get(newOwner.getClass());
     if (type == null) throw new Exception("illegal link owner type");
     at.setOwnerType(type.toString());
     db_.updateObj(at);
   }
 }
Ejemplo n.º 3
0
  /**
   * Save a link.
   *
   * @param link the link
   * @throws Exception the exception
   */
  public void saveLink(Link link) throws Exception {
    int num = link.getKey();

    if (num == -1) {
      int newkey = db_.nextkey();
      link.setKey(newkey);
      db_.addObj(link);
    } else {
      db_.updateObj(link);
    }

    // inform views of data change
    refresh();
  }
Ejemplo n.º 4
0
  /**
   * Import xml.
   *
   * @param is the input stream containing the XML
   * @throws Exception the exception
   */
  @Override
  public void importXml(InputStream is) throws Exception {

    JAXBContext jc = JAXBContext.newInstance(XmlContainer.class);
    Unmarshaller u = jc.createUnmarshaller();

    XmlContainer container = (XmlContainer) u.unmarshal(is);

    if (container.Link == null) return;

    // use key from import file if importing into empty db
    int nextkey = db_.nextkey();
    boolean use_keys = (nextkey == 1) ? true : false;
    for (Link link : container.Link) {
      if (!use_keys) link.setKey(nextkey++);
      db_.addObj(link);
    }

    refresh();
  }
Ejemplo n.º 5
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);
  }
Ejemplo n.º 6
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);
          }
        }
      }
    }
  }
Ejemplo n.º 7
0
 /* (non-Javadoc)
  * @see net.sf.borg.model.db.jdbc.JdbcBeanDB#createFrom(java.sql.ResultSet)
  */
 @Override
 Link createFrom(ResultSet r) throws SQLException {
   Link att = new Link();
   att.setKey(r.getInt("id"));
   att.setLinkType(r.getString("linktype"));
   att.setOwnerKey(new Integer(r.getInt("ownerkey")));
   att.setOwnerType(r.getString("ownertype"));
   att.setPath(r.getString("path"));
   return att;
 }
Ejemplo n.º 8
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);
  }
Ejemplo n.º 9
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

  }