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 } else {
00123 log().warn("Tried to attach myself to a null Paper");
00124 }
00125 return this;
00126 }
00127
00128
00129
00131 public List<String> getComments() {
00132 return _comments;
00133 }
00135 public Dataset setComments(List<String> comments) {
00137 _comments = comments;
00138 return this;
00139 }
00141 public Dataset addComment(String comment) {
00142 _comments.add(comment);
00143 return this;
00144 }
00146 public Dataset removeComment(String comment) {
00147 _comments.remove(comment);
00148 return this;
00149 }
00150
00151
00152
00154 public Set<DatasetProperty> getProperties() {
00155 return _properties;
00156 }
00157
00159 public Dataset setProperties(Set<DatasetProperty> properties) {
00161 _properties = properties;
00162 return this;
00163 }
00164
00166 public Dataset addProperty(DatasetProperty property) {
00167 if (property != null) {
00168 property.setDataset(this);
00169 _properties.add(property);
00170 } else {
00171 log().warn("Tried to add a null DatasetProperty to a Dataset");
00172 }
00173 return this;
00174 }
00175
00177 public Dataset removeProperty(DatasetProperty property) {
00178 if (property != null) {
00179 _properties.remove(property);
00180 property.setDataset(null);
00181 } else {
00182 log().warn("Tried to remove a null DatasetProperty from a Dataset");
00183 }
00184 return this;
00185 }
00186
00187
00188
00190 public SortedSet<DatasetError> getErrors() {
00191 return _errors;
00192 }
00193
00195 public Dataset setErrors(SortedSet<DatasetError> datasetErrors) {
00196 getErrors().clear();
00197 for (DatasetError e : datasetErrors) addError(e);
00198 return this;
00199 }
00200
00202 public Dataset setErrors(Collection<DatasetError> datasetErrors) {
00203 getErrors().clear();
00204 for (DatasetError e : datasetErrors) addError(e);
00205 return this;
00206 }
00207
00209 public Dataset setErrors(DatasetError[] datasetErrors) {
00210 setErrors(Arrays.asList(datasetErrors));
00211 return this;
00212 }
00213
00215 public Dataset addError(DatasetError datasetError) {
00216 if (datasetError != null) {
00217 getErrors().add(datasetError);
00218
00219 if (datasetError.getId() == null) {
00220 if (getErrors().last() != null && getErrors().last().getId() != null) {
00221 datasetError.setId(getErrors().last().getId() + 1);
00222 } else {
00223 datasetError.setId(getErrors().size() + 1);
00224 }
00225 }
00226 } else {
00227 log().warn("Tried to add a null DatasetError to a Dataset");
00228 }
00229 return this;
00230 }
00231
00233 public Dataset removeError(DatasetError datasetError) {
00234 if (datasetError != null) {
00235 getErrors().remove(datasetError);
00236 } else {
00237 log().warn("Tried to remove a null DatasetError from a Dataset");
00238 }
00239 return this;
00240 }
00241
00242
00243
00245 public SortedSet<XAxis> getXAxes() {
00246 return _xAxes;
00247 }
00248
00250 public XAxis getXAxis(Integer xAxisId) {
00251 XAxis theXAxis = null;
00252 for (XAxis x : getXAxes()) {
00253 if (x.getId().equals(xAxisId)) {
00254 theXAxis = x;
00255 break;
00256 }
00257 }
00258 return theXAxis;
00259 }
00260
00262 public Dataset setXAxes(SortedSet<XAxis> xAxes) {
00264 _xAxes = xAxes;
00265 return this;
00266 }
00267
00269 public Dataset setXAxes(Collection<XAxis> xAxes) {
00270 _xAxes.clear();
00271 for (XAxis x : xAxes) addXAxis(x);
00272 return this;
00273 }
00274
00276 public Dataset setXAxes(XAxis[] xAxes) {
00277 setXAxes(Arrays.asList(xAxes));
00278 return this;
00279 }
00280
00282 public Dataset addXAxis(XAxis xAxis) {
00283 if (xAxis != null) {
00284 if (xAxis.getId() == null) {
00285 int highestId = 0;
00286 if (getXAxes().size() > 0) {
00287 highestId = getXAxes().last().getId();
00288 }
00289 log().debug("Incrementing x-axis ID: " + (highestId + 1));
00290 xAxis.setId(highestId + 1);
00291 }
00292 log().debug("Adding x-axis: ID = " + xAxis.getId());
00293 _xAxes.add(xAxis);
00294 xAxis.setDataset(this);
00295 } else {
00296 log().warn("Tried to add a null XAxis to a Dataset");
00297 }
00298 return this;
00299 }
00300
00302 public Dataset removeXAxis(XAxis xAxis) {
00303 if (xAxis != null) {
00304 xAxis.setDataset(null);
00305 _xAxes.remove(xAxis);
00306 } else {
00307 log().warn("Tried to remove a null XAxis from a Dataset");
00308 }
00309 return this;
00310 }
00311
00312
00313
00314
00316 public SortedSet<YAxis> getYAxes() {
00317 return _yAxes;
00318 }
00319
00321 public YAxis getYAxis(Integer yAxisId) {
00322 YAxis theYAxis = null;
00323 for (YAxis y : getYAxes()) {
00324 if (y.getId().equals(yAxisId)) {
00325 theYAxis = y;
00326 break;
00327 }
00328 }
00329 return theYAxis;
00330 }
00331
00333 public Dataset setYAxes(SortedSet<YAxis> yAxes) {
00334 getYAxes().clear();
00335 for (YAxis y : yAxes) addYAxis(y);
00336 return this;
00337 }
00338
00340 public Dataset setYAxes(Collection<YAxis> yAxes) {
00341 getYAxes().clear();
00342 for (YAxis y : yAxes) addYAxis(y);
00343 return this;
00344 }
00345
00347 public Dataset setYAxes(YAxis[] yAxes) {
00348 setYAxes(Arrays.asList(yAxes));
00349 return this;
00350 }
00351
00353 public Dataset addYAxis(YAxis yAxis) {
00354 if (yAxis != null) {
00355 if (yAxis.getId() == null) {
00356 int highestId = 0;
00357 if (getYAxes().size() > 0) {
00358 highestId = getYAxes().last().getId();
00359 }
00360 log().debug("Incrementing y-axis ID: " + (highestId + 1));
00361 yAxis.setId(highestId + 1);
00362 }
00363 log().debug("Adding y-axis to dataset " + getId() + ": ID = " + yAxis.getId());
00364 _yAxes.add(yAxis);
00365 yAxis.setDataset(this);
00366 } else {
00367 log().warn("Tried to add a null YAxis to a Dataset");
00368 }
00369 return this;
00370 }
00371
00373 public Dataset removeYAxis(YAxis yAxis) {
00374 if (yAxis != null) {
00375 yAxis.setDataset(null);
00376 _yAxes.add(yAxis);
00377 } else {
00378 log().warn("Tried to remove a null XAxis from a Dataset");
00379 }
00380 return this;
00381 }
00382
00383
00384
00386
00387
00388
00391 public SortedSet<Bin> getBins(Integer binId) {
00392 SortedSet<Bin> matchingBins = new TreeSet<Bin>();
00393 for (XAxis x : getXAxes()) {
00394 Bin mybin = x.getBin(binId);
00395 if (mybin != null) {
00396 matchingBins.add(mybin);
00397 }
00398 }
00399 return matchingBins;
00400 }
00401
00404 public SortedSet<Point> getPoints(Integer pointId) {
00405 SortedSet<Point> matchingPoints = new TreeSet<Point>();
00406 for (YAxis y : getYAxes()) {
00407 Point mypoint = y.getPoint(pointId);
00408 if (mypoint != null) {
00409 matchingPoints.add(mypoint);
00410 }
00411 }
00412 return matchingPoints;
00413 }
00414
00415
00416
00418
00419
00420
00422 public int compareTo(Dataset other) {
00423 log().debug("Comparing datasets...");
00424 if (getId() == null) {
00425 log().warn("Null dataset ID");
00426 return 1;
00427 } else if (getId() > other.getId()) {
00428 log().debug("Greater than");
00429 return 1;
00430 } else if (getId() < other.getId()) {
00431 log().debug("Less than");
00432 return -1;
00433 } else {
00434 log().debug("Equal to");
00435 return 0;
00436 }
00437 }
00438
00439
00440
00442
00443
00444
00446 public String toString() {
00447 return toString(0);
00448 }
00449
00451 public String toString(Integer indentBy) {
00452 log().debug("Writing out dataset as a string");
00453 String indent = "";
00454 for (int i = 0; i < indentBy; ++i) indent += " ";
00455 StringBuffer s = new StringBuffer();
00456
00457
00458 s.append(indent + "Dataset ID: " + getId() + "\n");
00459 if (! getComments().isEmpty()) {
00460 s.append(indent + "Comments: ");
00461 for (String comment : getComments()) {
00462 s.append("\n" + indent + comment);
00463 }
00464 s.append("\n");
00465 }
00466
00467
00468 s.append(indent + getXAxes().size() + " x-axes, ");
00469 s.append(indent + getYAxes().size() + " y-axes");
00470 for (XAxis xaxis : getXAxes()) {
00471 log().debug("Getting x-axis string representation");
00472 s.append("\n" + xaxis.toString(indentBy + 2));
00473 }
00474 for (YAxis yaxis : getYAxes()) {
00475 log().debug("Getting y-axis string representation");
00476 s.append("\n" + yaxis.toString(indentBy + 2));
00477 }
00478 s.append("\n");
00479
00480
00481 if (! getErrors().isEmpty()) {
00482 s.append(indent + "Dataset errors:");
00483 for (Uncertainty err : getErrors()) {
00484 log().debug("Getting dataset error string representation");
00485 s.append("\n" + err.toString(indentBy + 2));
00486 }
00487 }
00488
00489 return s.toString();
00490 }
00491
00492 }