/* Copyright 2006 - 2010 Under Dusken Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package no.dusken.aranea.model; import org.compass.annotations.Searchable; import org.compass.annotations.SearchableComponent; import org.compass.annotations.SearchableConstant; import org.compass.annotations.SearchableProperty; import javax.persistence.*; import javax.validation.constraints.Size; import java.util.Collection; import java.util.Date; import java.util.LinkedList; import static javax.persistence.FetchType.LAZY; /** * This should be JPG RGB version of the image */ @Entity @Table(name = "image", uniqueConstraints = {@UniqueConstraint(columnNames = {"hash"})}) @SequenceGenerator(name = "image_seq", sequenceName = "image_id_seq") @NamedQueries({ @NamedQuery(name = "image_byPerson", query = "select i from Image i " + "where i.photographer = :person order by i.timeCreated desc"), @NamedQuery(name = "image_byHash", query = "select i from Image i where i.hash = :hash"), @NamedQuery(name = "imagenumber_byPerson", query = "select count(i) from Image i where i.photographer = :person"), @NamedQuery(name = "image_all", query = "select i from Image i order by i.timeCreated desc"), @NamedQuery(name = "image_byhash", query = "select i from Image i where i.hash = :hash") }) @Searchable @SearchableConstant(name = "type", values = {"image"}) @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "type") @DiscriminatorValue("IMAGE") public class Image extends AraneaObject { /** * who has taken this image, if an internal person */ @ManyToOne(fetch = FetchType.LAZY) @SearchableComponent private Person photographer; /** * rating of the image, done by the uploader */ @Enumerated(EnumType.STRING) @Column(nullable = true) private Rating rating; /** * If photographer is null, an external source should be spesified */ @Column @SearchableProperty @Size(max = 255) private String externalSource; /** * Is this a drawing? Default is no */ @Basic public Boolean illustration = false; /** * Date this image was taken */ @Temporal(TemporalType.TIMESTAMP) private Date created = new Date(); /** * This is a description of the image. People who are in it etc */ @Column(length = 500) @Size(max = 500) @SearchableProperty(boost = 2) public String description = ""; @ManyToMany(fetch = LAZY) @JoinTable(name = "image_tag") @SearchableComponent private Collection<Tag> tags; /** * the md5 hash of this file. Used as unique name, and as part of the url */ @Column(nullable = false) private String hash; /** * The size of the image in bytes */ @Basic private Long fileSize; /** * Height of the image */ @Basic private Integer height; /** * Width of the image */ @Basic private Integer width; /** * Get the photographer of this image * * @return the photographer */ public Person getPhotographer() { return photographer; } public void setPhotographer(Person photographer) { this.photographer = photographer; } public String getExternalSource() { return externalSource; } /** * Returns height / width. * * @return height / width, or null if that is not possible */ public Double getRatio() { if (getHeight() == null || getWidth() == null) { return null; } return (double) getHeight() / (double) getWidth(); } public void setExternalSource(String externalSource) { this.externalSource = externalSource; } public Boolean getIllustration() { return illustration; } public void setIllustration(Boolean illustration) { this.illustration = illustration; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Rating getRating() { return rating; } public void setRating(Rating rating) { this.rating = rating; } public Collection<Tag> getTags() { return tags; } public void setTags(Collection<Tag> tags) { this.tags = new LinkedList<Tag>(tags); } public String toString() { return "Image (ID=" + getID() + ") at " + getUrl(); } public String getUrl() { if (hash == null) return null; return getHash().substring(0, 1) + "/" + hash + ".image"; } public String getHash() { return hash; } public void setHash(String hash) { this.hash = hash; } public Long getFileSize() { return fileSize; } public void setFileSize(Long fileSize) { this.fileSize = fileSize; } public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } public Integer getWidth() { return width; } public void setWidth(Integer width) { this.width = width; } }