/* * (c) Copyright 2005-2012 JAXIO, www.jaxio.com * Source code generated by Celerio, a Jaxio product * Want to use Celerio within your company? email us at info@jaxio.com * Follow us on twitter: @springfuse * Template pack-backend-sd:src/main/java/project/domain/Entity.e.vm.java */ package com.company.demo.domain; import com.google.common.base.Objects; import javax.xml.bind.annotation.XmlTransient; import com.company.demo.domain.PersistableHashBuilder; import static javax.persistence.CascadeType.PERSIST; import static javax.persistence.FetchType.LAZY; import java.io.File; import java.io.IOException; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.Lob; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; import javax.persistence.Version; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import org.hibernate.annotations.GenericGenerator; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.data.domain.Persistable; import org.springframework.web.multipart.commons.CommonsMultipartFile; import com.company.demo.domain.Account; @Entity @Table(name = "DOCUMENT") public class Document implements Persistable<String> { private static final long serialVersionUID = 1L; @SuppressWarnings("unused") private static final Logger log = Logger.getLogger(Document.class); // Raw attributes private String id; // pk private String documentContentType; // not null private Integer documentSize; // not null private String documentFileName; // not null private byte[] documentBinary; private Integer version; // Technical attributes for query by example private String accountId; // not null // Many to one private Account account; // not null (accountId) // --------------------------- // Constructors // --------------------------- public Document() { } public Document(String primaryKey) { setId(primaryKey); } @Override @Transient @XmlTransient public boolean isNew() { return getId() == null; } // ------------------------------- // Getter & Setter // ------------------------------- // -- [id] ------------------------ @Column(name = "ID", length = 32) @GeneratedValue(generator = "strategy-uuid") @GenericGenerator(name = "strategy-uuid", strategy = "uuid") @Id public String getId() { return id; } public void setId(String id) { this.id = id; } // -- [accountId] ------------------------ @Column(name = "account_id", nullable = false, length = 32, insertable = false, updatable = false) public String getAccountId() { return accountId; } private void setAccountId(String accountId) { this.accountId = accountId; } // -- [documentContentType] ------------------------ @NotEmpty @Size(max = 255) @Column(name = "DOCUMENT_CONTENT_TYPE", nullable = false) public String getDocumentContentType() { return documentContentType; } public void setDocumentContentType(String documentContentType) { this.documentContentType = documentContentType; } // -- [documentSize] ------------------------ @NotNull @Column(name = "DOCUMENT_SIZE", nullable = false, precision = 10) public Integer getDocumentSize() { return documentSize; } public void setDocumentSize(Integer documentSize) { this.documentSize = documentSize; } // -- [documentFileName] ------------------------ @NotEmpty @Size(max = 255) @Column(name = "DOCUMENT_FILE_NAME", nullable = false) public String getDocumentFileName() { return documentFileName; } public void setDocumentFileName(String documentFileName) { this.documentFileName = documentFileName; } // -- [documentBinary] ------------------------ @Basic(fetch = FetchType.LAZY) @Column(name = "DOCUMENT_BINARY") @Lob public byte[] getDocumentBinary() { return documentBinary; } public void setDocumentBinary(byte[] documentBinary) { this.documentBinary = documentBinary; } /** * Helper method to convert the passed file to a byte[] and set it using {@link #setDocumentBinary(byte[])} * @param localFile to read the content from * @see #setDocumentBinary(byte[]) */ public void setDocumentBinaryFromFile(File localFile) { try { setDocumentBinary(FileUtils.readFileToByteArray(localFile)); } catch (Exception e) { throw new RuntimeException("Could not read from file", e); } } /** * Helper method to copy documentBinary byte array to the passed target file. * * @return the passed targetFile as a convenience. * @throws IllegalStateException when no binary is set * @see #getDocumentBinary() */ @Transient @XmlTransient public File getDocumentBinaryToFile(File targetFile) throws IOException { if (getDocumentBinary() != null) { throw new IllegalStateException("Empty binary"); } FileUtils.writeByteArrayToFile(targetFile, getDocumentBinary()); return targetFile; } /** * Helper method to copy documentBinary byte array to a temporary file. * * @throws IllegalStateException when no binary is set * @return a temporary file holding a copy of the documentBinary byte array * @see #getDocumentBinary() */ @Transient @XmlTransient public File getDocumentBinaryToTempFile() throws IOException { return getDocumentBinaryToFile(File.createTempFile("documentBinary", "file")); } // -- [version] ------------------------ @Column(name = "VERSION", precision = 10) @Version public Integer getVersion() { return version; } public void setVersion(Integer version) { this.version = version; } // -------------------------------------------------------------------- // Many to One support // -------------------------------------------------------------------- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // many-to-one: Document.accountId ==> Account.id // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @JoinColumn(name = "account_id", nullable = false) @ManyToOne(cascade = PERSIST, fetch = LAZY) public Account getAccount() { return account; } /** * Set the account without adding this Document instance on the passed account * If you want to preserve referential integrity we recommend to use * instead the corresponding adder method provided by {@link Account} */ public void setAccount(Account account) { this.account = account; // We set the foreign key property so it can be used by Hibernate search by Example facility. if (account != null) { setAccountId(account.getId()); } else { setAccountId(null); } } /** * Set the default values. */ public void initDefaultValues() { } @Override public boolean equals(Object other) { return this == other || (other instanceof Document && hashCode() == other.hashCode()); } private PersistableHashBuilder persistableHashBuilder = new PersistableHashBuilder(); @Override public int hashCode() { return persistableHashBuilder.hash(this); } /** * Construct a readable string representation for this {@link Document} instance. */ @Override public String toString() { return Objects.toStringHelper(this) // .add("id", getId()) // .add("accountId", getAccountId()) // .add("documentContentType", getDocumentContentType()) // .add("documentSize", getDocumentSize()) // .add("documentFileName", getDocumentFileName()) // .add("documentBinary", getDocumentBinary()) // .add("version", getVersion()) // .toString(); } }