00001 package cedar.jetweb.job; 00002 import cedar.jetweb.db.DBJobManager; 00003 import cedar.jetweb.Storeable; 00004 import cedar.jetweb.JetWebException; 00005 00006 import java.util.Vector; 00007 00008 public class CutCollection implements Storeable{ 00009 00010 private Vector<Cut> cuts_ = new Vector<Cut>(); 00011 private Integer id_=-1; 00012 00013 public CutCollection(){ 00014 00015 } 00016 00017 public CutCollection(Integer id){ 00018 id_ = id; 00019 try{ 00020 DBJobManager.selectFromDB(this); 00021 }catch(JetWebException err){ 00022 System.out.println 00023 ("CutCollection: Unable to query database "+ 00024 "to instantiate CutCollection from Id"); 00025 err.printStackTrace(); 00026 System.out.println(err.getMessage()); 00027 } 00028 } 00029 00030 00038 public boolean addCut(Cut cut){ 00039 Vector<Cut> cutsToRemove = new Vector<Cut>(); 00040 for(Cut existingCut: cuts_){ 00041 if(cut.lt(existingCut)) return false; 00042 String name1 = existingCut.getName(); 00043 String name2 = cut.getName(); 00044 if(name1!=null && name2!=null &&name1.equals(name2)){ 00045 cutsToRemove.add(existingCut); 00046 } 00047 } 00048 00049 for(Cut remove: cutsToRemove){ 00050 cuts_.remove(remove); 00051 } 00052 00053 setId(-1); 00054 cuts_.add(cut); 00055 return true; 00056 } 00057 00058 public CutCollection clear(){ 00059 cuts_.clear(); 00060 return this; 00061 } 00062 00063 public boolean equals(CutCollection cuts){ 00064 try{ 00065 if(id_< 0 && cuts_.size()!=0) retrieve(); 00066 if(cuts.getId()<0 && cuts.getCuts().size()!=0)cuts.retrieve(); 00067 }catch(JetWebException err){ 00068 System.out.println 00069 ("CutCollection.equals(): Unable to retrieve cut."); 00070 err.printStackTrace(); 00071 System.out.println(err.getMessage()); 00072 } 00073 if(id_>0 && id_ == cuts.getId()) return true; 00074 if(bothEmpty(cuts)) return true; 00075 return false; 00076 } 00077 00078 public Vector<Cut> getCuts(){ 00079 try{ 00080 if(id_>0 &&cuts_.size()==0) retrieve(); 00081 }catch(JetWebException err){ 00082 System.out.println 00083 ("CutCollection.getCuts(): Unable to retrieve CutCollection from DB"); 00084 err.printStackTrace(); 00085 } 00086 return cuts_; 00087 } 00088 00089 public int getIdDontRetrieve(){ 00090 return id_; 00091 } 00092 00093 public int getId(){ 00094 00095 try{ 00096 if(id_<0 && cuts_.size()!=0) retrieve(); 00097 }catch(JetWebException err){ 00098 System.out.println 00099 ("Unable to retrieve cut_collection_id from database"); 00100 System.out.println(err.getMessage()); 00101 err.printStackTrace(); 00102 } 00103 return id_; 00104 } 00105 00106 public CutCollection setId(Integer id){ 00107 id_ = id; 00108 return this; 00109 } 00110 00117 public boolean isWithin(CutCollection cuts){ 00118 00119 if(bothEmpty(cuts)) return true; 00120 00121 for(Cut cut1: cuts_){ 00122 for(Cut cut2: cuts.getCuts()){ 00123 if( cut1.lt(cut2) )return false; 00124 } 00125 } 00126 return true; 00127 } 00128 00139 public CutCollection union(CutCollection cuts){ 00140 00141 CutCollection ps = new CutCollection(); 00142 Vector<String> foundNames = new Vector<String>(); 00143 for(Cut cut1: cuts.getCuts()){ 00144 String name1 = cut1.getName(); 00145 boolean found = false; 00146 for(Cut cut2: cuts_){ 00147 String name2 = cut2.getName(); 00148 if(name1.equals(name2)){ 00149 found = true; 00150 foundNames.add(name2); 00151 if(cut1.getValue()<cut2.getValue()){ 00152 ps.addCut(cut1); 00153 }else{ 00154 ps.addCut(cut2); 00155 } 00156 } 00157 } 00158 if(!found) ps.addCut(cut1); 00159 } 00160 00161 if(foundNames.size()!=cuts_.size()){ 00162 for(Cut cut: cuts_){ 00163 if(!foundNames.contains(cut.getName())){ 00164 ps.addCut(cut); 00165 } 00166 } 00167 } 00168 00169 return ps; 00170 } 00171 00172 00173 public boolean store() 00174 throws JetWebException{ 00175 for(Cut cut: cuts_){ 00176 cut.store(); 00177 } 00178 00179 DBJobManager.addToDB(this); 00180 00181 return true; 00182 } 00183 00184 public boolean retrieve() 00185 throws JetWebException{ 00186 00187 DBJobManager.selectFromDB(this); 00188 00189 return true; 00190 } 00191 00192 private boolean bothEmpty(CutCollection cuts){ 00193 return 00194 (id_==-1 && 00195 cuts.getId()==-1 && 00196 cuts_.size()==0 && 00197 cuts.getCuts().size()==0 00198 ); 00199 } 00200 00201 }
Generated Wed Jan 17 09:14:27 GMT 2007