Skip to content
Snippets Groups Projects
DatabaseDocument.java 54 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
            // PMID
            Pub pub = feature_cvterm.getPub();
            
            // internal check
            checkPubDbXRef(pubDbXRefs, pub.getPubId(), pub, feature_cvterm);
            
            attr_buff.append("db_xref="+ pub.getUniqueName());
            nfound_dbxref++;
          }
          
          if(featureCvTermDbXRefs != null &&
                  featureCvTermDbXRefs.size() > 0)
          {
            for(int i=0; i<featureCvTermDbXRefs.size(); i++)
    
    tjc's avatar
    tjc committed
              FeatureCvTermDbXRef featureCvTermDbXRef =
                (FeatureCvTermDbXRef)featureCvTermDbXRefs.get(i);
        
              if(feature_cvterm.getFeatureCvTermId() != 
                featureCvTermDbXRef.getFeatureCvTerm().getFeatureCvTermId())
                continue;
          
              if(nfound_dbxref == 0)
                attr_buff.append("db_xref=");
    
    tjc's avatar
    tjc committed
              else if(nfound_dbxref > 0)
    
    tjc's avatar
    tjc committed
                attr_buff.append("|");
    
    tjc's avatar
    tjc committed
              
    
    tjc's avatar
    tjc committed
              DbXRef fc_dbXRef = featureCvTermDbXRef.getDbXRef();
              attr_buff.append(fc_dbXRef.getDb().getName()+":");
              attr_buff.append(fc_dbXRef.getAccession());
              nfound_dbxref++;
    
    tjc's avatar
    tjc committed
            }
    
    tjc's avatar
    tjc committed
          }
          
          if(nfound_dbxref > 0)
            attr_buff.append("%3B");
          
          List feature_cvtermprops = (List) feature_cvterm.getFeatureCvTermProps();
          for(int i = 0; i < feature_cvtermprops.size(); i++)
          {
            FeatureCvTermProp feature_cvtermprop = 
              (FeatureCvTermProp)feature_cvtermprops.get(i);
            attr_buff.append(getCvtermName(feature_cvtermprop.getCvTerm()
                .getCvTermId(), dao));
            attr_buff.append("=");
            attr_buff.append(feature_cvtermprop.getValue());
            if(i < feature_cvtermprops.size()-1)
              attr_buff.append("%3B");
          }
          
          attr_buff.append(";");
        }
        else if(cvterm.getCv().getName().equals(DatabaseDocument.PRODUCTS_TAG_CVNAME))
        {
          attr_buff.append("product=");
          attr_buff.append(feature_cvterm.getCvTerm().getName()+";");
        }
        else
        {
          attr_buff.append("GO=");
    
    tjc's avatar
    tjc committed
          if(cvterm.getCv().getName().equals("molecular_function"))
            attr_buff.append("aspect=F%3B");
          else if(cvterm.getCv().getName().equals("cellular_component"))
            attr_buff.append("aspect=C%3B");
          else if(cvterm.getCv().getName().equals("biological_process"))
            attr_buff.append("aspect=P%3B");
    
    tjc's avatar
    tjc committed
          if(feature_cvterm.isNot())
            attr_buff.append("qualifier=NOT%3B");
    
    tjc's avatar
    tjc committed
          attr_buff.append("GOid="+dbXRef.getDb().getName() + ":"
              + dbXRef.getAccession() + "%3B");
          
          attr_buff.append("term="+feature_cvterm.getCvTerm().getName()+"%3B");
          
          // PMID
    
    tjc's avatar
    tjc committed
          int nfound_pub = 0;
    
    tjc's avatar
    tjc committed
          if(feature_cvterm.getPub() != null &&
             feature_cvterm.getPub().getUniqueName() != null &&
    
    tjc's avatar
    tjc committed
             !feature_cvterm.getPub().getUniqueName().equals("NULL"))
          {
            Pub pub = feature_cvterm.getPub();
            attr_buff.append("db_xref="+
    
    tjc's avatar
    tjc committed
                pub.getUniqueName());
            nfound_pub++;
          }
          
          if(featureCvTermPubs != null &&
              featureCvTermPubs.size() > 0)
          {
            for(int i=0; i<featureCvTermPubs.size(); i++)
            {
              FeatureCvTermPub featureCvTermPub =
                (FeatureCvTermPub)featureCvTermPubs.get(i);
              
              if(feature_cvterm.getFeatureCvTermId() != 
                featureCvTermPub.getFeatureCvTerm().getFeatureCvTermId())
                continue;
              
              if(nfound_pub == 0)
                attr_buff.append("db_xref=");
    
    tjc's avatar
    tjc committed
              else if(nfound_pub > 0)
    
    tjc's avatar
    tjc committed
                attr_buff.append("|");
    
              attr_buff.append(featureCvTermPub.getPub().getUniqueName());
              nfound_pub++;
            }
    
    tjc's avatar
    tjc committed
          }
          
    
    tjc's avatar
    tjc committed
          if(nfound_pub > 0)
            attr_buff.append("%3B");
          
    
    tjc's avatar
    tjc committed
          if(featureCvTermDbXRefs != null &&
              featureCvTermDbXRefs.size() > 0 )
          {  
            int nfound = 0;
            for(int i=0; i<featureCvTermDbXRefs.size(); i++)
            {
              FeatureCvTermDbXRef featureCvTermDbXRef =
                (FeatureCvTermDbXRef)featureCvTermDbXRefs.get(i);
    
    tjc's avatar
    tjc committed
              
    
    tjc's avatar
    tjc committed
              
    
    tjc's avatar
    tjc committed
              if(feature_cvterm.getFeatureCvTermId() != 
                featureCvTermDbXRef.getFeatureCvTerm().getFeatureCvTermId())
    
    tjc's avatar
    tjc committed
              {
    
    tjc's avatar
    tjc committed
                continue;
    
    tjc's avatar
    tjc committed
              }
    
    tjc's avatar
    tjc committed
              
    
    tjc's avatar
    tjc committed
              if(nfound == 0)
                attr_buff.append("with=");
              else if(nfound > 1)
                attr_buff.append("|");
              
              DbXRef fc_dbXRef = featureCvTermDbXRef.getDbXRef();
              attr_buff.append(fc_dbXRef.getDb().getName()+":");
              attr_buff.append(fc_dbXRef.getAccession());
              nfound++;
    
    tjc's avatar
    tjc committed
            
            if(nfound > 0)
              attr_buff.append("%3B");
    
    tjc's avatar
    tjc committed
          List feature_cvtermprops = (List)feature_cvterm
              .getFeatureCvTermProps();
          for(int i = 0; i < feature_cvtermprops.size(); i++)
          {
            FeatureCvTermProp feature_cvtermprop = 
              (FeatureCvTermProp)feature_cvtermprops.get(i);
    
            
            if(feature_cvtermprop.getValue() == null)
              continue;
            
    
    tjc's avatar
    tjc committed
            attr_buff.append(getCvtermName(feature_cvtermprop.getCvTerm()
                .getCvTermId(), dao));
            attr_buff.append("=");
            attr_buff.append(feature_cvtermprop.getValue());
            if(i < feature_cvtermprops.size()-1)
              attr_buff.append("%3B");
          }
          
          attr_buff.append(";");
        }
    
    tjc's avatar
    tjc committed
      }
    
    tjc's avatar
    tjc committed
      /**
       * Check the PubDbXref contains the Pub in FeatureCvTerm
       * @param pubDbXRefs
       * @param pubId
       * @param pub
       * @param feature_cvterm
       */
      private static void checkPubDbXRef(final List pubDbXRefs, final int pubId,
                                         final Pub pub, final FeatureCvTerm feature_cvterm)
      {
        PubDbXRef pubDbXRef = null;
        for(int i = 0; i < pubDbXRefs.size(); i++)
        {
          pubDbXRef = (PubDbXRef) pubDbXRefs.get(i);
          if(pubDbXRef.getPub().getPubId() == pubId)
          {
            DbXRef dbxref = pubDbXRef.getDbXRef();
            Splash.logger4j.debug("Checking PubDbXRef and found Pub "+dbxref.getDb().getName()+
                                  ":"+dbxref.getAccession());
            break;
          }
        }
        
        if(pubDbXRef == null || 
            !pub.getUniqueName().endsWith(pubDbXRef.getDbXRef().getAccession()))
         {
           Splash.logger4j.debug("Checking PubDbXRef and not found Pub "+
                               feature_cvterm.getPub().getUniqueName());
          
           JOptionPane.showMessageDialog(null, "Cannot find pub_dbxref for:\n"+
               feature_cvterm.getPub().getUniqueName(), 
               "Database Error",
               JOptionPane.ERROR_MESSAGE);
         }
      }
    
    tjc's avatar
    tjc committed
      /**
       * Look up the cvterm_id for a controlled vocabulary name.
       * @param name  
       * @return
       */
    
    tjc's avatar
    tjc committed
      public static Integer getCvtermID(String name)
    
    tjc's avatar
    tjc committed
      {
    
        Enumeration enum_cvterm = cvterms.keys();
    
    tjc's avatar
    tjc committed
        while(enum_cvterm.hasMoreElements())
        {
    
    tjc's avatar
    tjc committed
          Integer key = (Integer)enum_cvterm.nextElement();
    
          if(name.equals( ((CvTerm)cvterms.get(key)).getName() ))
    
    tjc's avatar
    tjc committed
            return key;
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        return null;
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * Look up a cvterm name from the collection of cvterms.
       * @param id  a cvterm_id  
       * @return    the cvterm name
    
    tjc's avatar
    tjc committed
       */
    
    tjc's avatar
    tjc committed
      private static String getCvtermName(int id, GmodDAO dao)
    
      {
        return getCvTerm(id, dao).getName();
      }
      
    
    tjc's avatar
    tjc committed
      private static CvTerm getCvTerm(int id, GmodDAO dao)
    
    tjc's avatar
    tjc committed
      {
    
        if(cvterms == null)
    
          getCvterms(dao);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        return (CvTerm)cvterms.get(new Integer(id));
    
    tjc's avatar
    tjc committed
      }
    
    tjc's avatar
    tjc committed
      
      public static CvTerm getCvTermByCvTermName(String cvterm_name)
      {
        Enumeration enum_cvterm = cvterms.elements();
        while(enum_cvterm.hasMoreElements())
        {
          CvTerm cvterm = (CvTerm)enum_cvterm.nextElement();
          if(cvterm_name.equals( cvterm.getName() ))
            return cvterm;
        }
        
        return null;
      }
    
    tjc's avatar
    tjc committed
      
      public static CvTerm getCvTermByCvAndCvTerm(final String cvterm_name,
                                                  final String cvName)
      {
        Enumeration enum_cvterm = cvterms.elements();
        while(enum_cvterm.hasMoreElements())
        {
          CvTerm cvterm = (CvTerm)enum_cvterm.nextElement();
          if(cvName.equals( cvterm.getCv().getName() ) &&
             cvterm_name.equals( cvterm.getName() ))
            return cvterm;
        }
        return null;
      }
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * Look up cvterms names and id and return in a hashtable.
    
    tjc's avatar
    tjc committed
       * @param dao the data access object
       * @return    the cvterm <code>Hashtable</code>
    
    tjc's avatar
    tjc committed
       */
    
      private static Hashtable getCvterms(GmodDAO dao)
    
    tjc's avatar
    tjc committed
      {
    
        cvterms = new Hashtable();
    
    tjc's avatar
    tjc committed
    
        try
        {
    
          List cvterm_list = dao.getCvTerms();
          Iterator it = cvterm_list.iterator();
    
    tjc's avatar
    tjc committed
    
          while(it.hasNext())
          {
    
            CvTerm cvterm = (CvTerm)it.next();
    
    tjc's avatar
    tjc committed
            cvterms.put(new Integer(cvterm.getCvTermId()), cvterm);
    
    tjc's avatar
    tjc committed
          }
        }
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          System.err.println("SQLException retrieving CvTerms");
    
    tjc's avatar
    tjc committed
          System.err.println(sqle);
        }
    
    
    tjc's avatar
    tjc committed
      }
    
    tjc's avatar
    tjc committed
      
      public static Vector getCvterms(final String search_str, final String cv_name)
      {
        final Vector cvterm_match = new Vector();
        
        Enumeration enum_cvterm = cvterms.keys();
        while(enum_cvterm.hasMoreElements())
        {
          Integer key = (Integer)enum_cvterm.nextElement();
          CvTerm cvterm = (CvTerm)cvterms.get(key);
          
          if(cvterm.getCv().getName().startsWith(cv_name))
          {
            if(cvterm.getName().indexOf(search_str) > -1)
              cvterm_match.add(cvterm);
          }
        }
        
        return cvterm_match;
      }
    
    tjc's avatar
    tjc committed
    
    
      /**
       * Look up synonym type names e.g. synonym, systematic_id.
       * @return    the synonym tag names
       */
      public static String[] getSynonymTypeNames(String cv_name)
      {
        Vector synonym_names = new Vector();
        Enumeration cvterm_enum = cvterms.elements();
        while(cvterm_enum.hasMoreElements())
        {
          CvTerm cvterm = (CvTerm)cvterm_enum.nextElement();
          if(cvterm.getCv().getName().equals(cv_name))
            synonym_names.add(cvterm.getName());
        }
        
        return (String[])synonym_names.toArray(
                           new String[synonym_names.size()]);
      }
      
      
    
    tjc's avatar
    tjc committed
      /**
       * Get the sequence for a feature.
       * @param dao   the data access object
       * @param buff  the buffer to add the sequence to
       * @return      the resulting buffer
       * @throws java.sql.SQLException
       */
    
      private ByteBuffer getChadoSequence(GmodDAO dao, ByteBuffer buff)
    
    tjc's avatar
    tjc committed
      {
    
        Feature feature = dao.getFeatureById(Integer.parseInt(srcFeatureId));
    
    tjc's avatar
    tjc committed
     
    
    tjc's avatar
    tjc committed
        buff.append("##FASTA\n>");
    
    tjc's avatar
    tjc committed
        buff.append(feature.getUniqueName());
    
    tjc's avatar
    tjc committed
        buff.append("\n");
    
    tjc's avatar
    tjc committed
        buff.append(feature.getResidues());
    
    tjc's avatar
    tjc committed
        return buff;
      }
    
    
      /**
       * Get the <code>List</code> of available schemas.
       * @return  the <code>List</code> of available schemas
       */
    
      public List getSchema()
      {
        return schema_list;
      }
    
    tjc's avatar
    tjc committed
      
    
    tjc's avatar
    tjc committed
      /**
       * Create a hashtable of the available entries with residues.
       * @return a <code>Hashtable</code> of the <code>String</code>
       *          representation (schema-type-feature_name) and the
       *          corresponding feature_id
       * @throws ConnectException
       * @throws java.sql.SQLException
       */
    
    tjc's avatar
    tjc committed
      public HashMap getDatabaseEntries()
    
    tjc's avatar
    tjc committed
                       throws ConnectException, java.sql.SQLException
      {
        String schema = null;
    
        try
        { 
    
          GmodDAO dao = null;
    
    tjc's avatar
    tjc committed
          dao = getDAO();
          schema_list = dao.getOrganisms();
    
    tjc's avatar
    tjc committed
          
          
    /*      Organism o = new Organism();
          o.setCommonName("web");
          schema_list.add(o);*/
          
          
    
    tjc's avatar
    tjc committed
          Iterator it = schema_list.iterator();
    
    tjc's avatar
    tjc committed
          db = new HashMap();
    
    tjc's avatar
    tjc committed
          
    
    tjc's avatar
    tjc committed
          while(it.hasNext())
          {
            Organism organism = (Organism)it.next();
            schema = organism.getCommonName();
            
            reset((String)getLocation(),  schema);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            try
            {
              dao = getDAO();
    
    tjc's avatar
    tjc committed
              List list_residue_features = dao.getResidueFeatures();
    
    tjc's avatar
    tjc committed
              Iterator it_residue_features = list_residue_features.iterator();
              while(it_residue_features.hasNext())
              {
                Feature feature = (Feature)it_residue_features.next();
                String typeName = getCvtermName(feature.getCvTerm().getCvTermId(), getDAO()); 
              
                db.put(schema + " - " + typeName + " - " + feature.getUniqueName(),
                       Integer.toString(feature.getFeatureId()));
              }
            }
            catch(RuntimeException e)
            { 
            }
            catch(java.sql.SQLException sqlExp)
            {
            }
          }
          
        }
        catch(RuntimeException sqlExp)
        {
          JOptionPane.showMessageDialog(null, "SQL Problems...\n"+
                                        sqlExp.getMessage(), 
                                        "SQL Error",
                                        JOptionPane.ERROR_MESSAGE);
          sqlExp.printStackTrace();
        }
        catch(ConnectException exp)
        {
          JOptionPane.showMessageDialog(null, "Connection Problems...\n"+
                exp.getMessage(), 
                "Connection Error",
                JOptionPane.ERROR_MESSAGE);
          throw exp;
        }
        catch(java.sql.SQLException sqlExp)
        {
          JOptionPane.showMessageDialog(null, "SQL Problems....\n"+
                                        sqlExp.getMessage(), 
                                        "SQL Error",
                                        JOptionPane.ERROR_MESSAGE);
          throw sqlExp;
        }
        
        return db;
      }
      
      
    
    tjc's avatar
    tjc committed
      /**
       * Get the data access object (DAO).
       * @return data access object
       */
    
      private GmodDAO getDAO()
    
    tjc's avatar
    tjc committed
         throws java.net.ConnectException, SQLException
    
    tjc's avatar
    tjc committed
      { 
    
    tjc's avatar
    tjc committed
        if(!iBatis)
        {
          if(jdbcDAO == null)
           jdbcDAO = new JdbcDAO((String)getLocation(), pfield); 
          return jdbcDAO;
        }
        else
    
    tjc's avatar
    tjc committed
        {   
    
    tjc's avatar
    tjc committed
          if(connIB == null)
    
    tjc's avatar
    tjc committed
          {
            System.setProperty("chado", (String)getLocation());
    
    tjc's avatar
    tjc committed
            connIB = new IBatisDAO(pfield);
    
    tjc's avatar
    tjc committed
          }
    
    tjc's avatar
    tjc committed
          return connIB;
        }
      }
    
    
    
    tjc's avatar
    tjc committed
      /**
    
    tjc's avatar
    tjc committed
       * Create a new OutputStream object from this Document. The contents of the
       * Document can be written from the stream.
       * 
       * @exception IOException
       *              Thrown if the Document can't be written.
       */
    
    tjc's avatar
    tjc committed
      public OutputStream getOutputStream() throws IOException
      {
    
    tjc's avatar
    tjc committed
        final File write_file = new File(System.getProperty("user.dir")+
                                         System.getProperty("file.separator")+
                                         getName());
    
        final FileOutputStream file_output_stream =
          new FileOutputStream(write_file);
    
        if(write_file.getName().endsWith(".gz")) 
        {
          // assume this file should be gzipped
          return new java.util.zip.GZIPOutputStream (file_output_stream);
        } 
        else 
          return file_output_stream;
    
    tjc's avatar
    tjc committed
      }
    
    
    tjc's avatar
    tjc committed
      /**
       * Commit the <code>ChadoTransaction</code> SQL back to the
       * database.
       * @param sql the collection of <code>ChadoTransaction</code> objects
       * @return
       */
      public int commit(Vector sql)
    
    tjc's avatar
    tjc committed
      {
    
    tjc's avatar
    tjc committed
        int i = 0;
    
    tjc's avatar
    tjc committed
        try
        {
    
          GmodDAO dao = getDAO();
    
          if(dao instanceof IBatisDAO)
            ((IBatisDAO) dao).startTransaction();
    
          boolean unchanged;
          
          //
          // check feature timestamps have not changed
    
          Vector names_checked = new Vector();
    
    tjc's avatar
    tjc committed
          for(i = 0; i < sql.size(); i++)
    
    tjc's avatar
    tjc committed
          {
    
    tjc's avatar
    tjc committed
            ChadoTransaction tsn = (ChadoTransaction)sql.get(i);
            if( (tsn.getType() == ChadoTransaction.INSERT ||
                 tsn.getType() == ChadoTransaction.DELETE) && 
                 tsn.getFeatureObject() instanceof Feature )
              continue;
    
    tjc's avatar
    tjc committed
            final String uniquename = tsn.getUniquename();
            
            if(uniquename == null)
              continue;
    
    tjc's avatar
    tjc committed
            if(names_checked.contains(uniquename))
              continue;
    
    tjc's avatar
    tjc committed
            names_checked.add(uniquename);
            
    
            String keyName = tsn.getFeatureKey();
    
    tjc's avatar
    tjc committed
            unchanged = checkFeatureTimestamp(schema, 
                             uniquename, 
    
                             tsn.getLastModified(), dao, 
                             keyName, tsn.getFeatureObject());
    
    tjc's avatar
    tjc committed
            if(!unchanged)
              return 0;
    
            //
            // commit to database
            for(i = 0; i < sql.size(); i++)
    
              ChadoTransaction tsn = (ChadoTransaction) sql.get(i);
    
              if(tsn.getType() == ChadoTransaction.UPDATE)
    
    tjc's avatar
    tjc committed
              {
                if(tsn.getFeatureObject() instanceof Feature)
                {
                  Feature feature = (Feature)tsn.getFeatureObject();
                  
    
    tjc's avatar
    tjc committed
                  if(feature.getUniqueName() != null)
    
    tjc's avatar
    tjc committed
                  {
                    final String uniquename;
                    if(tsn.getOldUniquename() != null)
                      uniquename = (String)tsn.getOldUniquename();
                    else
    
    tjc's avatar
    tjc committed
                      uniquename = feature.getUniqueName();
    
    tjc's avatar
    tjc committed
                    
                    Feature old_feature
    
                        = dao.getFeatureByUniqueName(uniquename, tsn.getFeatureKey());
    
    tjc's avatar
    tjc committed
                    
                    if(old_feature != null)
                      feature.setFeatureId( old_feature.getFeatureId() );
    
    tjc's avatar
    tjc committed
                    tsn.setOldUniquename(feature.getUniqueName());
    
    tjc's avatar
    tjc committed
                  }
                }
                dao.merge(tsn.getFeatureObject());
                //dao.updateAttributes(tsn);
              }
    
              else if(tsn.getType() == ChadoTransaction.INSERT)
    
    tjc's avatar
    tjc committed
                if(tsn.getFeatureObject() instanceof FeatureCvTerm)
                  ArtemisUtils.inserFeatureCvTerm(dao, (FeatureCvTerm)tsn.getFeatureObject());
                else
    
    tjc's avatar
    tjc committed
                  // set srcfeature_id
                  if(tsn.getFeatureObject() instanceof Feature)
                  {
                    FeatureLoc featureloc = ((Feature) tsn.getFeatureObject()).getFeatureLoc();
                    Feature featureBySrcFeatureId = new Feature();
    
                    featureBySrcFeatureId.setFeatureId(Integer.parseInt(srcFeatureId));
    
    tjc's avatar
    tjc committed
                    featureloc.setFeatureBySrcFeatureId(featureBySrcFeatureId);
                  }
                  dao.persist(tsn.getFeatureObject());
    
              else if(tsn.getType() == ChadoTransaction.DELETE)
    
    tjc's avatar
    tjc committed
              {
                if(tsn.getFeatureObject() instanceof FeatureCvTerm)
                  ArtemisUtils.deleteFeatureCvTerm(dao, (FeatureCvTerm)tsn.getFeatureObject());
                else
                  dao.delete(tsn.getFeatureObject());
              }
    
            }
    
            //
            // update timelastmodified timestamp
    
    tjc's avatar
    tjc committed
            Timestamp ts = new Timestamp(new java.util.Date().getTime());
    
            names_checked = new Vector();
            for(int j = 0; j < sql.size(); j++)
            {
    
    tjc's avatar
    tjc committed
              ChadoTransaction tsn = (ChadoTransaction)sql.get(j);
              
              if( (tsn.getType() == ChadoTransaction.INSERT ||
                  tsn.getType() == ChadoTransaction.DELETE) && 
                  tsn.getFeatureObject() instanceof Feature )
               continue;
              
              final String uniquename = tsn.getUniquename();
              if(uniquename == null)
                continue;
                
              if(names_checked.contains(uniquename))
                continue;
    
    tjc's avatar
    tjc committed
              names_checked.add(uniquename);
    
              Feature feature = dao.getFeatureByUniqueName(uniquename, tsn.getFeatureKey());
    
    tjc's avatar
    tjc committed
              if(feature != null)
              {
    
                feature.setTimeLastModified(ts);
    
    tjc's avatar
    tjc committed
                dao.merge(feature);
    
    tjc's avatar
    tjc committed
    
              GFFStreamFeature gff_feature = (GFFStreamFeature) tsn
                  .getGff_feature();
              gff_feature.setLastModified(ts);
    
            
            if(dao instanceof IBatisDAO && 
               System.getProperty("nocommit") == null)
               ((IBatisDAO) dao).commitTransaction();
          }
          finally
          {
            if(dao instanceof IBatisDAO)
              ((IBatisDAO) dao).endTransaction();
          }
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        catch (java.sql.SQLException sqlExp)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          JOptionPane.showMessageDialog(null, "Problems Writing...\n" +
    
    tjc's avatar
    tjc committed
                                        sqlExp.getMessage(),
    
    tjc's avatar
    tjc committed
                                        "Problems Writing to Database ",
    
    tjc's avatar
    tjc committed
                                        JOptionPane.ERROR_MESSAGE);
    
    tjc's avatar
    tjc committed
          sqlExp.printStackTrace();
        }
    
    tjc's avatar
    tjc committed
        catch (java.net.ConnectException conn_ex)
    
    tjc's avatar
    tjc committed
        {
    
    tjc's avatar
    tjc committed
          JOptionPane.showMessageDialog(null, "Problems connecting..."+
                                        conn_ex.getMessage(),
    
    tjc's avatar
    tjc committed
                                        "Database Connection Error - Check Server",
                                        JOptionPane.ERROR_MESSAGE);
    
    tjc's avatar
    tjc committed
          conn_ex.printStackTrace();
    
    tjc's avatar
    tjc committed
        }
    
    tjc's avatar
    tjc committed
        return i;
    
    tjc's avatar
    tjc committed
      }
    
      
      /**
       * Check the <code>Timestamp</code> on a feature (for versioning).
       * @param schema      the schema
       * @param uniquename  the feature uniquename
       * @param timestamp   the last read feature timestamp
       */
    
      public boolean checkFeatureTimestamp(final String schema,
    
    tjc's avatar
    tjc committed
                                           final String uniquename,
                                           final Timestamp timestamp,
    
                                           final GmodDAO dao,
                                           final String keyName,
                                           final Object featureObject)
    
        Feature feature = dao.getFeatureByUniqueName(uniquename, keyName);
    
    tjc's avatar
    tjc committed
        if(feature == null)
          return true;
    
        
        if(featureObject instanceof FeatureProp)
          ((FeatureProp)featureObject).setFeature(feature);
        else if(featureObject instanceof FeatureLoc)
          ((FeatureLoc)featureObject).setFeatureByFeatureId(feature);
          
    
        Timestamp now = feature.getTimeLastModified();
    
    tjc's avatar
    tjc committed
        if(now != null && timestamp != null)
    
        {
          now.setNanos(0);
          timestamp.setNanos(0);
          
    
          if(now.compareTo(timestamp) != 0)
          {
            SimpleDateFormat date_format = 
                       new SimpleDateFormat("dd.MM.yyyy hh:mm:ss z");
    
            //System.out.println(date_format.format(now)+"   "+
            //                   date_format.format(timestamp));
    
            int select = JOptionPane.showConfirmDialog(null, uniquename +
    
                                          " has been altered at :\n"+
    
                                          date_format.format(now)+"\nOverwite?", 
                                          "Feature Changed", 
                                          JOptionPane.OK_CANCEL_OPTION);
            if(select == JOptionPane.OK_OPTION)
              return true;
            else
              return false;
    
        return true;
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
      public static void main(String args[])
      {
        try
        {
    
          GmodDAO dao;
    
    tjc's avatar
    tjc committed
          DatabaseEntrySource src = new DatabaseEntrySource();
          src.setLocation(true);
          
          if(System.getProperty("ibatis") == null)
            dao = new JdbcDAO(src.getLocation(), src.getPfield()); 
          else
            dao = new IBatisDAO(src.getPfield());
          
    
    tjc's avatar
    tjc committed
          Feature feature = new Feature();
    
    tjc's avatar
    tjc committed
          feature.setUniqueName(args[0]);
    
    tjc's avatar
    tjc committed
          List schemas = new Vector();
          schemas.add(args[1]);
    
    tjc's avatar
    tjc committed
          List featureList = new Vector();
    
          featureList.add(dao.getFeatureByUniqueName(args[0], "polypeptide")); 
    
    tjc's avatar
    tjc committed
          System.out.println("FINISHED getFeature()");
    
    tjc's avatar
    tjc committed
          for(int i = 0; i < featureList.size(); i++)
          {
    
    tjc's avatar
    tjc committed
            feature = (Feature)featureList.get(i);
    
    tjc's avatar
    tjc committed
            
    
    tjc's avatar
    tjc committed
            String abb  = feature.getOrganism().getAbbreviation();
    
    tjc's avatar
    tjc committed
            String type = feature.getCvTerm().getName();
    
            int fmin    = feature.getFeatureLoc().getFmin().intValue() + 1;
            int fmax    = feature.getFeatureLoc().getFmax().intValue();
    
    tjc's avatar
    tjc committed
            String featprop = 
    
    tjc's avatar
    tjc committed
              ((FeatureProp)(new Vector(feature.getFeatureProps()).get(0))).getCvTerm().getName();
    
    tjc's avatar
    tjc committed
            System.out.print(fmin+".."+fmax);
    
    tjc's avatar
    tjc committed
            System.out.print(" "+type);
    
    tjc's avatar
    tjc committed
            System.out.print(" "+featprop);
    
            System.out.print(" "+feature.getFeatureLoc().getStrand());
    
    tjc's avatar
    tjc committed
            System.out.print(" "+feature.getUniqueName());
    
    tjc's avatar
    tjc committed
            System.out.print(" "+abb);
    
    tjc's avatar
    tjc committed
            System.out.println(" "+Integer.toString(feature.getFeatureId()));
    
    tjc's avatar
    tjc committed
            
    
    tjc's avatar
    tjc committed
    /*      Hashtable synonyms = getAllFeatureSynonyms(dao, null);
    
    tjc's avatar
    tjc committed
            Vector syns = (Vector)synonyms.get(new Integer(feature.getId()));
            for(int j=0; j<syns.size(); j++)
            {
    
    tjc's avatar
    tjc committed
              FeatureSynonym alias = (FeatureSynonym)syns.get(j);
    
    tjc's avatar
    tjc committed
              System.out.print(" "+alias.getSynonym().getCvterm().getName()+
                               "="+alias.getSynonym().getName());
    
    tjc's avatar
    tjc committed
            }*/
    
    tjc's avatar
    tjc committed
            
            System.out.println(" "); 
    
    tjc's avatar
    tjc committed
          }
    
    tjc's avatar
    tjc committed
        }
        catch(SQLException sqle)
        {
          sqle.printStackTrace();
        }
    
        catch(RuntimeException re)
        {
          re.printStackTrace();
        }
    
    tjc's avatar
    tjc committed
        catch(ConnectException e)
        {
          e.printStackTrace();
        }
    
    tjc's avatar
    tjc committed
      }
    
    
      public Document getParent()
      {
        return null;
      }
    
      
      public static FeatureLoc getFeatureLoc(List locs, int srcfeature_id)
      {
        for(int i=0; i<locs.size(); i++)
        {
          FeatureLoc loc = (FeatureLoc)locs.get(i);
          if(loc.getFeatureBySrcFeatureId().getFeatureId() == srcfeature_id)
            return loc;
        }
        return null;
      }
    
    
    
      public String getSrcFeatureId()
      {
        return srcFeatureId;
      }
    
    tjc's avatar
    tjc committed
    }