00001 package cedar.hepdata.model;
00002
00003 import java.util.*;
00004 import org.apache.log4j.*;
00005
00006 import javax.persistence.*;
00007
00016 @Entity
00017 @Table(name="Datasets")
00018 public class Dataset extends Storeable implements Comparable<Dataset> {
00020 @Id @GeneratedValue
00021 @Column(name="DATASET_ID")
00022 private Long _id;
00023
00025 @ManyToOne
00026 private Paper _paper;
00027
00029 @Column(name="LocalId", nullable=false)
00030 private Integer _localId;
00031
00033 @OneToMany(mappedBy="_dataset")
00034 @org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL,
00035 org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
00036 private Set<DatasetProperty> _properties = new HashSet<DatasetProperty>();
00037
00039 @org.hibernate.annotations.CollectionOfElements
00040 @JoinTable(name="DatasetErrors", joinColumns=@JoinColumn(name="DATASET_ID"))
00041 @org.hibernate.annotations.CollectionId(columns=@Column(name="DatasetErrorId"),
00042 type=@org.hibernate.annotations.Type(type="long"),
00043 generator="sequence")
00044 @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL)
00045 private SortedSet<DatasetError> _errors = new TreeSet<DatasetError>();
00046
00048 @org.hibernate.annotations.CollectionOfElements
00049 @JoinTable(name="DatasetComments", joinColumns=@JoinColumn(name="DATASET_ID"))
00050 @org.hibernate.annotations.IndexColumn(name="Posn")
00051 @Column(name="Comments", length=10000)
00052 private List<String> _comments = new Vector<String>();
00053
00055 @OneToMany(mappedBy="_dataset")
00056 @org.hibernate.annotations.Fetch(value=org.hibernate.annotations.FetchMode.SUBSELECT)
00057 @org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL,
00058 org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
00059 @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL)
00060 private SortedSet<XAxis> _xAxes = new TreeSet<XAxis>();
00061
00063 @OneToMany(mappedBy="_dataset")
00064 @org.hibernate.annotations.Fetch(value=org.hibernate.annotations.FetchMode.SUBSELECT)
00065 @org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL,
00066 org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
00067 @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL)
00068 private SortedSet<YAxis> _yAxes = new TreeSet<YAxis>();
00069
00070
00071
00073
00074
00075
00077 public Dataset() { }
00078
00079
00081 public Dataset(Paper paper) {
00082 this();
00083 setPaper(paper);
00084 }
00085
00086
00088
00089
00090
00092 public Integer getId() {
00093 return _localId;
00094 }
00095
00097 public Dataset setId(Integer datasetId) {
00098 _localId = datasetId;
00099 return this;
00100 }
00101
00102
00103
00105 public Paper getPaper() {
00106 return _paper;
00107 }
00108
00110 public Dataset setPaper(Paper paper) {
00111 if (paper != null) {
00112 if (getId() == null) {
00113 int highestId = 0;
00114 if (paper.getDatasets().size() > 0) {
00115 highestId = paper.getDatasets().last().getId();
00116 }
00117 log().debug("Incrementing dataset ID: " + (highestId + 1));
00118 setId(highestId + 1);
00119 }
00120 paper.getDatasets().add(this);
00121 _paper = paper;
00122 }
00123 return this;
00124 }
00125
00126
00127
00129 public List<String> getComments() {
00130 return _comments;
00131 }
00133 public Dataset setComments(List<String> comments) {
00135 _comments = comments;
00136 return this;
00137 }
00139 public Dataset addComment(String comment) {
00140 _comments.add(comment);
00141 return this;
00142 }
00144 public Dataset removeComment(String comment) {
00145 _comments.remove(comment);
00146 return this;
00147 }
00148
00149
00150
00152 public Set<DatasetProperty> getProperties() {
00153 return _properties;
00154 }
00155
00157 public Dataset setProperties(Set<DatasetProperty> properties) {
00159 _properties = properties;
00160 return this;
00161 }
00162
00164 public Dataset addProperty(DatasetProperty property) {
00165 if (property != null) {
00166 property.setDataset(this);
00167 _properties.add(property);
00168 } else {
00169 log().warn("Tried to add a null DatasetProperty to a Dataset");
00170 }
00171 return this;
00172 }
00173
00175 public Dataset removeProperty(DatasetProperty property) {
00176 if (property != null) {
00177 _properties.remove(property);
00178 property.setDataset(null);
00179 } else {
00180 log().warn("Tried to remove a null DatasetProperty from a Dataset");
00181 }
00182 return this;
00183 }
00184
00185
00186
00188 public SortedSet<DatasetError> getErrors() {
00189 return _errors;
00190 }
00191
00193 public Dataset setErrors(SortedSet<DatasetError> datasetErrors) {
00194 getErrors().clear();
00195 for (DatasetError e : datasetErrors) addError(e);
00196 return this;
00197 }
00198
00200 public Dataset setErrors(Collection<DatasetError> datasetErrors) {
00201 getErrors().clear();
00202 for (DatasetError e : datasetErrors) addError(e);
00203 return this;
00204 }
00205
00207 public Dataset setErrors(DatasetError[] datasetErrors) {
00208 setErrors(Arrays.asList(datasetErrors));
00209 return this;
00210 }
00211
00213 public Dataset addError(DatasetError datasetError) {
00214 if (datasetError != null) {
00215 datasetError.setDataset(this);
00216 } else {
00217 log().warn("Tried to add a null DatasetError to a Dataset");
00218 }
00219 return this;
00220 }
00221
00223 public Dataset removeError(DatasetError datasetError) {
00224 if (datasetError != null) {
00225 getErrors().remove(datasetError);
00226 } else {
00227 log().warn("Tried to remove a null DatasetError from a Dataset");
00228 }
00229 return this;
00230 }
00231
00232
00233
00235 public SortedSet<XAxis> getXAxes() {
00236 return _xAxes;
00237 }
00238
00240 public XAxis getXAxis(Integer xAxisId) {
00241 XAxis theXAxis = null;
00242 for (XAxis x : getXAxes()) {
00243 if (x.getId().equals(xAxisId)) {
00244 theXAxis = x;
00245 break;
00246 }
00247 }
00248 return theXAxis;
00249 }
00250
00252 public Dataset setXAxes(SortedSet<XAxis> xAxes) {
00253 getXAxes().clear();
00254 for (XAxis x : xAxes) addXAxis(x);
00255 return this;
00256 }
00257
00259 public Dataset setXAxes(Collection<XAxis> xAxes) {
00260 getXAxes().clear();
00261 for (XAxis x : xAxes) addXAxis(x);
00262 return this;
00263 }
00264
00266 public Dataset setXAxes(XAxis[] xAxes) {
00267 setXAxes(Arrays.asList(xAxes));
00268 return this;
00269 }
00270
00272 public Dataset addXAxis(XAxis xAxis) {
00273 if (xAxis != null) {
00274 xAxis.setDataset(this);
00275 } else {
00276 log().warn("Tried to add a null XAxis to a Dataset");
00277 }
00278 return this;
00279 }
00280
00282 public Dataset removeXAxis(XAxis xAxis) {
00283 if (xAxis != null) {
00284 _xAxes.remove(xAxis);
00285 } else {
00286 log().warn("Tried to remove a null XAxis from a Dataset");
00287 }
00288 return this;
00289 }
00290
00291
00292
00293
00295 public SortedSet<YAxis> getYAxes() {
00296 return _yAxes;
00297 }
00298
00300 public YAxis getYAxis(Integer yAxisId) {
00301 YAxis theYAxis = null;
00302 for (YAxis y : getYAxes()) {
00303 if (y.getId().equals(yAxisId)) {
00304 theYAxis = y;
00305 break;
00306 }
00307 }
00308 return theYAxis;
00309 }
00310
00312 public Dataset setYAxes(SortedSet<YAxis> yAxes) {
00313 getYAxes().clear();
00314 for (YAxis y : yAxes) addYAxis(y);
00315 return this;
00316 }
00317
00319 public Dataset setYAxes(Collection<YAxis> yAxes) {
00320 getYAxes().clear();
00321 for (YAxis y : yAxes) addYAxis(y);
00322 return this;
00323 }
00324
00326 public Dataset setYAxes(YAxis[] yAxes) {
00327 setYAxes(Arrays.asList(yAxes));
00328 return this;
00329 }
00330
00332 public Dataset addYAxis(YAxis yAxis) {
00333 if (yAxis != null) {
00334 yAxis.setDataset(this);
00335 } else {
00336 log().warn("Tried to add a null YAxis to a Dataset");
00337 }
00338 return this;
00339 }
00340
00342 public Dataset removeYAxis(YAxis yAxis) {
00343 if (yAxis != null) {
00344 _yAxes.add(yAxis);
00345 } else {
00346 log().warn("Tried to remove a null XAxis from a Dataset");
00347 }
00348 return this;
00349 }
00350
00351
00352
00354
00355
00356
00359 public SortedSet<Bin> getBins(Integer binId) {
00360 SortedSet<Bin> matchingBins = new TreeSet<Bin>();
00361 for (XAxis x : getXAxes()) {
00362 Bin mybin = x.getBin(binId);
00363 if (mybin != null) {
00364 matchingBins.add(mybin);
00365 }
00366 }
00367 return matchingBins;
00368 }
00369
00372 public SortedSet<Point> getPoints(Integer pointId) {
00373 SortedSet<Point> matchingPoints = new TreeSet<Point>();
00374 for (YAxis y : getYAxes()) {
00375 Point mypoint = y.getPoint(pointId);
00376 if (mypoint != null) {
00377 matchingPoints.add(mypoint);
00378 }
00379 }
00380 return matchingPoints;
00381 }
00382
00383
00384
00386
00387
00388
00390 public int compareTo(Dataset other) {
00391 log().debug("Comparing datasets...");
00392 if (getId() == null) {
00393 log().warn("Null dataset ID");
00394 return 1;
00395 } else if (getId() > other.getId()) {
00396 log().debug("Greater than");
00397 return 1;
00398 } else if (getId() < other.getId()) {
00399 log().debug("Less than");
00400 return -1;
00401 } else {
00402 log().debug("Equal to");
00403 return 0;
00404 }
00405 }
00406
00407
00408
00410
00411
00412
00414 public String toString() {
00415 return toString(0);
00416 }
00417
00419 public String toString(Integer indentBy) {
00420 log().debug("Writing out dataset as a string");
00421 String indent = "";
00422 for (int i = 0; i < indentBy; ++i) indent += " ";
00423 StringBuffer s = new StringBuffer();
00424
00425
00426 s.append(indent + "Dataset ID: " + getId() + "\n");
00427 if (! getComments().isEmpty()) {
00428 s.append(indent + "Comments: ");
00429 for (String comment : getComments()) {
00430 s.append("\n" + indent + comment);
00431 }
00432 s.append("\n");
00433 }
00434
00435
00436 s.append(indent + getXAxes().size() + " x-axes, ");
00437 s.append(indent + getYAxes().size() + " y-axes");
00438 for (XAxis xaxis : getXAxes()) {
00439 log().debug("Getting x-axis string representation");
00440 s.append("\n" + xaxis.toString(indentBy + 2));
00441 }
00442 for (YAxis yaxis : getYAxes()) {
00443 log().debug("Getting y-axis string representation");
00444 s.append("\n" + yaxis.toString(indentBy + 2));
00445 }
00446 s.append("\n");
00447
00448
00449 if (! getErrors().isEmpty()) {
00450 s.append(indent + "Dataset errors:");
00451 for (Uncertainty err : getErrors()) {
00452 log().debug("Getting dataset error string representation");
00453 s.append("\n" + err.toString(indentBy + 2));
00454 }
00455 }
00456
00457 return s.toString();
00458 }
00459
00460
00462
00463
00465 public boolean equals(Object other) {
00466 if (this == other) return true;
00467 if (! (other instanceof Dataset)) return false;
00468 return hashCode() == other.hashCode();
00469 }
00470
00472 public int hashCode() {
00473 int code = 31;
00474 if (getPaper() != null) code ^= getPaper().hashCode();
00475 if (getId() != null) code ^= getId().hashCode();
00476 for (String c : getComments()) code ^= c.hashCode();
00477 return code;
00478 }
00479
00480
00481 }