1 /* ChadoTransaction
2  *
3  * created: July 2006
4  *
5  * This file is part of Artemis
6  *
7  * Copyright (C) 2006  Genome Research Limited
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  */
24 
25 package uk.ac.sanger.artemis.chado;
26 
27 import java.sql.Timestamp;
28 
29 import uk.ac.sanger.artemis.io.GFFStreamFeature;
30 import uk.ac.sanger.artemis.util.DatabaseDocument;
31 
32 
33 /**
34  * Store information about a SQL transaction <i>e.g</i> UPDATE, INSERT, DELETE
35  * a feature, featureloc, featureprop, feature_dbxref, feature_synonym.
36  **/
37 public class ChadoTransaction
38 {
39   //
40   // FEATURE TRANSACTIONS
41   /** update statement */
42   public static final int UPDATE = 1;
43   /** insert statement */
44   public static final int INSERT = 2;
45   /** delete statement */
46   public static final int DELETE = 3;
47 
48   /** type of statement <i>e.g.</i> UPDATE, INSERT, DELETE, ... */
49   protected int type;
50   /** feature unique name */
51   protected String old_uniquename;
52   private String uniquename;
53 
54   /** last time feature was modified */
55   private Timestamp lastmodified;
56   /** the feature object */
57   private Object feature_obj;
58   /** the feature key */
59   private String featureKey;
60 
61   private GFFStreamFeature gff_feature;
62 
63   private String logComment;
64 
65   /**
66    * Create a transaction to represent a single insert, delete or
67    * update.
68    * @param type          the type of transcation, e.g. insert, delete, update.
69    * @param feature_obj   the <code>Feature</code>, <code>FeatureLoc</code>,
70    * <code>FeatureProp</code>, <code>FeatureRelationship</code>, <code>FeatureDbxref</code>,
71    * or <code>FeatureSynonym</code>.
72    * @param lastmodified  the last modified timestamp
73    * @param gff_feature   the artemis GFF feature effected
74    */
ChadoTransaction(final int type, final Object feature_obj, final Timestamp lastmodified, final GFFStreamFeature gff_feature, final String featureKey, final String logComment)75   public ChadoTransaction(final int type,
76                           final Object feature_obj,
77                           final Timestamp lastmodified,
78                           final GFFStreamFeature gff_feature,
79                           final String featureKey,
80                           final String logComment)
81   {
82     this.type = type;
83     this.lastmodified = lastmodified;
84     this.feature_obj  = feature_obj;
85     this.gff_feature  = gff_feature;
86     this.logComment   = logComment;
87 
88     if(featureKey != null &&
89        featureKey.equals(DatabaseDocument.EXONMODEL))
90       this.featureKey = "exon";
91     else
92       this.featureKey   = featureKey;
93   }
94 
95   /**
96    * Copy this transaction
97    * @return
98    */
copy()99   public ChadoTransaction copy()
100   {
101     final ChadoTransaction tsn = new ChadoTransaction(getType(), getFeatureObject(),
102         getLastModified(), getGff_feature(),
103         getFeatureKey(), logComment);
104 
105     if(uniquename != null)
106       tsn.setUniquename(uniquename);
107 
108     tsn.setOldUniquename(old_uniquename);
109     return tsn;
110   }
111 
112   /**
113    * The type of SQL transaction
114    * @return 	the transaction type
115    */
getType()116   public int getType()
117   {
118     return type;
119   }
120 
getTypeAsString()121   public String getTypeAsString()
122   {
123     if(type == UPDATE)
124       return "UPDATE";
125     else if(type == INSERT)
126       return "INSERT";
127     else if(type == DELETE)
128       return "DELETE";
129     return "";
130   }
131 
132   /**
133    * Set the old uniquename, used when updating the uniquename
134    * @param uniquename
135    */
setOldUniquename(final String old_uniquename)136   public void setOldUniquename(final String old_uniquename)
137   {
138     this.old_uniquename = old_uniquename;
139   }
140 
141   /**
142    * Get the unique names of features to change.
143    */
getOldUniquename()144   public String getOldUniquename()
145   {
146     return old_uniquename;
147   }
148 
149   /**
150    * Set a uniquename, e.g. to be used when changing a featureprop
151    * @param uniquename
152    */
setUniquename(final String uniquename)153   public void setUniquename(final String uniquename)
154   {
155     this.uniquename = uniquename;
156   }
157 
getUniquename()158   public String getUniquename()
159   {
160     if(uniquename != null)
161       return uniquename;
162 
163     if(getGff_feature() == null)
164       return null;
165 
166     return (String)
167       getGff_feature().getQualifierByName("ID").getValues().get(0);
168   }
169 
170   /**
171    * Get the last time modified time stamp.
172    * @return  the <code>Timestamp</code>
173    */
getLastModified()174   public Timestamp getLastModified()
175   {
176     return this.lastmodified;
177   }
178 
getFeatureObject()179   public Object getFeatureObject()
180   {
181     return feature_obj;
182   }
183 
getGff_feature()184   public GFFStreamFeature getGff_feature()
185   {
186     return gff_feature;
187   }
188 
189 
getFeatureKey()190   public String getFeatureKey()
191   {
192     return featureKey;
193   }
194 
195 
getLogComment()196   public String getLogComment()
197   {
198     String key = "";
199     if(getFeatureKey() != null)
200       key = " KEY=" + getFeatureKey();
201     return "["+getTypeAsString()+"] "+logComment+key;
202   }
203 
204 }
205