/* License see bottom */
package jtrackbase.db;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.TableGenerator;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
@Entity
public class Medium extends CommentedEntity implements Serializable {
@Column(nullable=false)
private int yearReleased;
@Column(nullable=false)
private int value;
@OneToMany(cascade=CascadeType.ALL)
private List<Track> tracks;
@OneToOne(cascade=CascadeType.ALL)
private Artist artist;
@OneToOne(cascade=CascadeType.ALL)
private Genre genre;
@OneToOne(cascade=CascadeType.ALL)
private MediumType mediumType;
@OneToOne(cascade=CascadeType.ALL)
private Label label;
@OneToOne(cascade=CascadeType.ALL)
private StockArea stockArea;
@OneToMany(cascade=CascadeType.ALL)
private List<Tag> tags;
@Lob
@Basic(fetch=FetchType.LAZY)
private byte[] imageData;
@Id
@GeneratedValue(generator = "MediumIdGenerator")
@TableGenerator(name = "MediumIdGenerator",
table = "ID_GENERATOR",
pkColumnName = "ID_NAME",
valueColumnName = "ID_VAL",
pkColumnValue = "Medium")
private long id;
public Medium() {
this(MediumType.UNSPECIFIED);
}
public Medium(MediumType mt) {
mediumType=mt;
tracks=new ArrayList<Track>();
}
/**
* Don't try to modify this list, it'll be
* punished.
*
* @return
*/
public List<Track> getTracks() {
return tracks==null?null:Collections.unmodifiableList(tracks);
}
public void addTrack(Track t) {
tracks.add(t.getPosition(), t);
}
public void addTracks(Collection<Track> tracks) {
for(Track t:tracks) {
this.tracks.add(t.getPosition(), t);
}
}
public void removeTrack(Track t) {
tracks.remove(t);
}
public void removeTrackAt(int pos) {
tracks.remove(pos);
}
/**
* Performs a copy-constructor call, so
* the caller can keep it's list.
*
* @param tracks
*/
public void setTracks(List<Track> tracks) {
this.tracks = new ArrayList<Track>(tracks);
}
public Artist getArtist() {
return artist;
}
public void setArtist(Artist artist) {
this.artist = artist;
}
public Genre getGenre() {
return genre;
}
public void setGenre(Genre genre) {
this.genre = genre;
}
public MediumType getMediumType() {
return mediumType;
}
public void setMediumType(MediumType mediumType) {
this.mediumType = mediumType;
}
/**
* Use as full digit year, e.g. 2009 is saved
* as 2009.
*
* @return
*/
public int getYearReleased() {
return yearReleased;
}
/**
* Use as full digit year, e.g. 2009 is saved
* as 2009.
*
* @return
*/
public void setYearReleased(int year) {
this.yearReleased = year;
}
/**
* Value in smallest unit like Euro- or
* Dollar cents.
*
* @return
*/
public int getValue() {
return value;
}
/**
* Value in smallest unit like Euro- or
* Dollar cents.
*
* @return
*/
public void setValue(int value) {
this.value = value;
}
public void setImage(ImageIcon ii) {
int resizeWidth = ii.getIconWidth();
int resizeHeight = ii.getIconHeight();
JPanel p = new JPanel();
BufferedImage bi = new BufferedImage(resizeWidth, resizeHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.drawImage(ii.getImage(), 0, 0, p);
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
try {
encoder.encode(bi);
} catch (IOException ex) {
throw new IllegalArgumentException(ex);
}
imageData = os.toByteArray();
}
public ImageIcon getImage() {
return new ImageIcon(imageData);
}
public Label getLabel() {
return label;
}
public void setLabel(Label label) {
this.label = label;
}
public StockArea getStockArea() {
return stockArea;
}
public void setStockArea(StockArea stockArea) {
this.stockArea = stockArea;
}
/**
*
* @return An unmodifiable list of all tags
* available. Can be <code>null</code>.
*/
public List<Tag> getTags() {
return tags==null?null:Collections.unmodifiableList(tags);
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public void addTag(Tag t) {
if (t.getTarget()!=TagTarget.MEDIUM) {
throw new IllegalArgumentException("TagTarget "+t.getTarget()+" not allowed for medium");
}
if (tags==null) {
tags=new ArrayList<Tag>();
}
tags.add(t);
}
public void removeTag(Tag t) {
if (!tags.contains(t)) {
return;
}
tags.remove(t);
}
@Override
public long getId() {
return id;
}
@Override
public void setId(long id) {
this.id=id;
}
}
/*
Copyright (C) 2008 Onkobu Tanaake
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (gplv3.txt).
*/