View Javadoc
1   package org.imageconverter.domain.imagetype;
2   
3   import static javax.persistence.GenerationType.IDENTITY;
4   import static org.apache.commons.lang3.StringUtils.isNoneBlank;
5   import static org.imageconverter.util.BeanUtil.getBeanFrom;
6   
7   import java.time.LocalDateTime;
8   import java.util.Objects;
9   import java.util.Optional;
10  
11  import javax.persistence.Column;
12  import javax.persistence.Entity;
13  import javax.persistence.GeneratedValue;
14  import javax.persistence.Id;
15  import javax.persistence.Table;
16  import javax.persistence.UniqueConstraint;
17  import javax.validation.ConstraintViolationException;
18  import javax.validation.Validator;
19  import javax.validation.constraints.NotEmpty;
20  
21  @Entity
22  @Table(name = "IMAGE_TYPE", uniqueConstraints = @UniqueConstraint(columnNames = "IMT_EXTENSION", name = "UK_IMT_EXTENSION"))
23  public class ImageType {
24  
25      @Id
26      @GeneratedValue(strategy = IDENTITY)
27      @Column(name = "IMT_ID", nullable = false)
28      private Long id;
29  
30      // PNG, BMP, JPEG, JPG ...
31      @NotEmpty(message = "{imagetype.extension}")
32      @Column(name = "IMT_EXTENSION", nullable = false, unique = true)
33      private String extension;
34  
35      @NotEmpty(message = "{imagetype.name}")
36      @Column(name = "IMT_NAME", nullable = false)
37      private String name;
38  
39      @Column(name = "IMT_DESC", nullable = true)
40      private String description;
41  
42      @Column(name = "IMT_CREATED", nullable = false)
43      private LocalDateTime created;
44  
45      @Column(name = "IMT_UPDATED", nullable = true)
46      private LocalDateTime updated;
47  
48      ImageType() {
49  	super();
50      }
51  
52      public ImageType( //
53  		    final String extension, //
54  		    final String name, //
55  		    final String description) {
56  	super();
57  	this.extension = extension;
58  	this.name = name;
59  	this.description = description;
60  	this.created = LocalDateTime.now();
61  
62  	final var validator = getBeanFrom(Validator.class);
63  
64  	final var violations = validator.validate(this);
65  	if (!violations.isEmpty()) {
66  	    throw new ConstraintViolationException(violations);
67  	}
68      }
69  
70      public void update(final String extension, final String name, final String description) {
71  
72  	if (isNoneBlank(extension)) {
73  	    this.extension = extension;
74  	}
75  
76  	if (isNoneBlank(name)) {
77  	    this.name = name;
78  	}
79  
80  	if (isNoneBlank(description)) {
81  	    this.description = description;
82  	}
83  
84  	this.updated = LocalDateTime.now();
85      }
86  
87      public Long getId() {
88  	return id;
89      }
90  
91      public String getExtension() {
92  	return extension;
93      }
94  
95      public String getName() {
96  	return name;
97      }
98  
99      public Optional<String> getDescription() {
100 	return Optional.ofNullable(description);
101     }
102 
103     public LocalDateTime getCreated() {
104 	return LocalDateTime.from(created);
105     }
106 
107     public Optional<LocalDateTime> getUpdated() {
108 	return Optional.ofNullable(updated);
109     }
110 
111     @Override
112     public int hashCode() {
113 	return Objects.hash(this.id);
114     }
115 
116     @Override
117     public boolean equals(final Object obj) {
118 
119 	final boolean result;
120 
121 	if (this == obj) {
122 	    result = true;
123 	    
124 	} else if (obj instanceof ImageType other) {
125 	    result = Objects.equals(this.id, other.id);
126 	    
127 	} else {
128 	    result = false;
129 	}
130 
131 	return result;
132     }
133 
134     @Override
135     public String toString() {
136 	final var builder = new StringBuilder(34);
137 	builder.append("ImageType [id=").append(id) //
138 			.append(", extension=").append(extension) //
139 			.append(", name=").append(name) //
140 			.append(']');
141 	return builder.toString();
142     }
143 }