Java tutorial
/* * South Face Software * Copyright 2012, South Face Software, Inc. and individual contributors * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package com.sfs.captor.model; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import org.apache.commons.lang.StringUtils; import org.hibernate.envers.Audited; import org.hibernate.search.annotations.Analyze; import org.hibernate.search.annotations.DocumentId; import org.hibernate.search.annotations.Field; import org.hibernate.search.annotations.Index; import org.hibernate.search.annotations.Indexed; import org.hibernate.search.annotations.Store; import com.sfs.captor.data.Constants; import com.sfs.captor.data.Literal; import com.sfs.captor.util.ModelUtils; /** * GlossaryTerm * * @author lbbishop * */ @Entity @Indexed @Audited @Table(name = "glossaryterm") public class GlossaryTerm extends EntityBase implements Serializable { private static final long serialVersionUID = 1L; @Id @DocumentId @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES) @NotNull(message = "Term is required") @Size(max = 100) @Column(name = "term", length = 100, nullable = false) private String term; @NotNull(message = "Definition is required") @Size(max = 1000) @Column(name = "definition", length = 1000, nullable = false) private String definition; @ManyToOne private Project project; /** * Default constructor */ public GlossaryTerm() { super(); } /** * Identifier constructor */ public GlossaryTerm(int identifier) { super(); this.identifier = Integer.valueOf(identifier); } /** * @param term * @param definition */ public GlossaryTerm(String term, String definition) { super(); this.term = term; this.definition = definition; } /** * PrePersist method */ @PrePersist public void prePersist() { if (this.modifiedBy == null) { this.modifiedBy = Literal.APPNAME.toString(); } this.modifiedDate = new Date(); } /** * PreUpdate method */ @PreUpdate public void preUpdate() { if (this.modifiedBy == null) { this.modifiedBy = Literal.APPNAME.toString(); } this.modifiedDate = new Date(); } /** * @return the id */ public Long getId() { return id; } /** * @param id * the id to set */ public void setId(Long id) { this.id = id; } /** * @return the term */ public String getTerm() { return term; } /** * @param term * the term to set */ public void setTerm(String term) { if (term != null) { this.term = term.trim(); } } /** * @return the definition */ public String getDefinition() { return definition; } /** * @return the definition abbreviated */ public String getDefinitionAbbrv() { return StringUtils.abbreviate(this.definition, Constants.ABBRV_DESC_LEN); } /** * @param definition * the definition to set */ public void setDefinition(String definition) { this.definition = definition; } /** * @return the project */ public Project getProject() { return project; } /** * @param project * the project to set */ public void setProject(Project project) { this.project = project; } /** * @return the identifier string (PREFIX concatenated with identifier) */ public String getArtifact() { return ModelUtils.buildArtifactIdentifier(Literal.PREFIX_GLOSSARY.toString(), this.identifier); } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("GlossaryTerm [id="); builder.append(id); builder.append(", identifier="); builder.append(identifier); builder.append(", term="); builder.append(term); builder.append(", definition="); builder.append(definition); builder.append(", project="); builder.append(project); builder.append("]"); return builder.toString(); } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((term == null) ? 0 : term.hashCode()); return result; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; GlossaryTerm other = (GlossaryTerm) obj; if (term == null) { if (other.term != null) return false; } else if (!term.equals(other.term)) return false; return true; } }