BrightSide Workbench Full Report + Source Code
DaoEntity.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.jpa.entity;
19 
20 import java.io.Serializable;
21 import java.lang.reflect.ParameterizedType;
22 import java.util.ArrayList;
23 import java.util.List;
24 import javax.persistence.Id;
25 import org.turro.string.Strings;
26 import org.turro.entities.Entities;
27 import org.turro.entities.IElephantEntity;
28 import org.turro.jpa.Dao;
29 import org.turro.json.Jsons;
30 import org.turro.log.SystemLogType;
31 import org.turro.log.SystemLogger;
32 import org.turro.reflection.Beans;
33 import org.turro.reflection.stub.Stubs;
34 
41 public abstract class DaoEntity<T extends IDaoEntity, ID extends Serializable> {
42 
43  private final Class<T> persistentClass;
44  private Dao dao;
45  protected T entity;
46  protected ID id;
47  protected List<String> messages = new ArrayList<>();
48 
49  public DaoEntity() {
50  this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
51  .getGenericSuperclass()).getActualTypeArguments()[0];
52  }
53 
54  public DaoEntity(Class<T> persistentClass) {
55  this.persistentClass = persistentClass;
56  }
57 
58  public DaoEntity(T entity) {
59  this.entity = entity;
60  this.persistentClass = (Class<T>) entity.getClass();
61  }
62 
63  public Dao getDao() {
64  if(dao == null) {
65  dao = createDao();
66  }
67  return dao;
68  }
69 
70  public T getEntity() {
71  return entity;
72  }
73 
76  }
77 
78  protected abstract Dao createDao();
79  protected abstract boolean shouldLog();
80 
81  @SuppressWarnings("unchecked")
82  public T find(ID id) {
83  this.id = id;
84  entity = getDao().find(persistentClass, id);
85  return entity;
86  }
87 
88  @SuppressWarnings("unchecked")
89  public T save() {
90  initOperation();
91  if(entity != null && canSave()) {
92  entity.prepareSave();
93  entity = getDao().saveObject(entity);
95  }
96  return entity;
97  }
98 
99  public boolean delete() {
100  initOperation();
101  if(entity != null && canDelete()) {
102  entity.prepareDelete();
105  return true;
106  }
107  return false;
108  }
109 
110  public ID getId() {
111  if(entity == null) {
112  id = null;
113  } else if(id == null) {
114  id = (ID) Beans.of(entity).silentGetter(f -> f.isAnnotationPresent(Id.class));
115  }
116  return id;
117  }
118 
119  public List<String> getMessages() {
120  return messages;
121  }
122 
123  protected void logEntity(SystemLogType logType, Object entity, String action, String data) {
124  if(shouldLog()) {
125  SystemLogger.type(logType).entity(entity).comment(action).object(data).log();
126  }
127  }
128 
129  protected String dataEntity(Object entity) {
130  String entityData = Stubs.json(entity);
131  if(Jsons.isEmpty(entityData)) {
132  entityData = new XMLSerializer(entity).data();
133  }
134  return entityData;
135  }
136 
137  public boolean canDelete() {
138  return !entity.isNew();
139  }
140 
141  public boolean canSave() {
142  return !entity.isEmpty();
143  }
144 
145  protected void initOperation() {
146  messages.clear();
147  }
148 
149  protected void addMessage(String message) {
150  messages.add(message);
151  }
152 
153  @Override
154  public boolean equals(Object obj) {
155  if (obj == null) {
156  return false;
157  }
158  if (getClass() != obj.getClass() && entity.getClass() != obj.getClass()) {
159  return false;
160  }
161  if(obj.equals(entity)) {
162  return true;
163  }
164  if(obj instanceof DaoEntity && ((DaoEntity) obj).getEntity().equals(entity)) {
165  return true;
166  }
167  return (getId() == null && obj == null) ||
168  (getId() != null && (obj instanceof DaoEntity) && getId().equals(((DaoEntity) obj).getId()));
169  }
170 
171  @Override
172  public int hashCode() {
173  return getId() == null ? 0 : getId().hashCode();
174  }
175 
176  public static Object getEntityId(Object entity) {
177  if(entity != null) {
178  return Beans.of(entity).silentGetter(f -> f.isAnnotationPresent(Id.class));
179  }
180  return null;
181  }
182 
183  public static boolean isNewId(Object id) {
184  if(id instanceof String) {
185  return Strings.isBlank((String) id);
186  } else if(id instanceof Long) {
187  return ((Long) id) == 0L;
188  }
189  return true;
190  }
191 
192 }
static IElephantEntity getController(String path)
Definition: Entities.java:78
void deleteObject(Object obj)
Definition: Dao.java:162
static Object getEntityId(Object entity)
Definition: DaoEntity.java:176
String dataEntity(Object entity)
Definition: DaoEntity.java:129
void logEntity(SystemLogType logType, Object entity, String action, String data)
Definition: DaoEntity.java:123
abstract boolean shouldLog()
void addMessage(String message)
Definition: DaoEntity.java:149
boolean equals(Object obj)
Definition: DaoEntity.java:154
IElephantEntity getIee()
Definition: DaoEntity.java:74
static boolean isNewId(Object id)
Definition: DaoEntity.java:183
DaoEntity(Class< T > persistentClass)
Definition: DaoEntity.java:54
List< String > getMessages()
Definition: DaoEntity.java:119
static ILogWrapper type(SystemLogType type)
ILogWrapper object(Serializable data)
ILogWrapper comment(String comment)
ILogWrapper entity(Object entity)