com.sprhibrad.framework.dao.ShrDao Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sprhibrad-framework Show documentation
Show all versions of sprhibrad-framework Show documentation
The framework of the SprHibRAD suite - Stefano Pizzocaro - 62
/*
Copyright (c) 2017, Stefano Pizzocaro. All rights reserved. Use is subject to license terms.
This file is part of SprHibRAD 1.0.
SprHibRAD 1.0 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 3 of the License, or
(at your option) any later version.
SprHibRAD 1.0 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 SprHibRAD 1.0. If not, see .
*/
package com.sprhibrad.framework.dao;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.sprhibrad.framework.common.DataSetClauses;
import com.sprhibrad.framework.common.ShrImage;
import com.sprhibrad.framework.common.Utils;
import com.sprhibrad.framework.common.DataSetClauses.OrderItem;
import com.sprhibrad.framework.configuration.UserManager;
import com.sprhibrad.framework.model.ShrEntity;
import com.sprhibrad.framework.model.VerboseLiteral;
/**
* The SprHibRAD core class for the Data Access Object layer.
* Here the framework relies on Hibernate Session to perform the ORM exchange activity and in particular on javax.persistence to build up the criteria for all the inquiries made by the Framework itself.
* Here, furthermore, the crypto service of Spring Security is used to store the password and the building of an image preview takes place, starting from an image uploaded by the user through
* the upload features of the framework, and to be stored in a dedicated field.
* Here it can be seen the dedicated method to binary data exchange as it occurs, in SprHibRAD, by means of an autonomous and atomic user action.
* The management of users by the framework is here directed, as well, towards the target class of the concrete application that implements the {@code UserManager} interface.
* {@link update} is the method that by having empty implementation can be implemented with exchange contribution for each desired involved attribute when updating is required.
*/
public class ShrDao implements IShrDao{
@Autowired
private SessionFactory sessionFactory;
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
UserManager userManager;
@Override
public List getObjects(Integer iteration, DataSetClauses clauses, Integer pageSize) {
CriteriaTools tools = getCriteriaQueryTools();
prepareList(tools, clauses);
return presentationList(tools, clauses, iteration, pageSize);
}
@Override
public T getObject(Serializable id) {
return (T) getCurrentSession().get(Utils.typeArgumentClass(getClass(), 0), id);
}
@Override
public void addObject(T entity) {
manageObject(entity, false);
}
@Override
public void updateObject(T entity) {
manageObject(entity, true);
}
protected void manageObject(T object, boolean update) {
boolean userOp = userOperation(object);
T objectInDb = null;
if (update)
objectInDb = getObject(object.getId());
String clearPwd = null;
if (userOp) {
if (update)
dropUser(objectInDb);
clearPwd = userManager.getPassword(object);
userManager.setPassword(object, passwordEncoder.encode(clearPwd));
}
if (update) {
update(object, objectInDb);
getCurrentSession().save(objectInDb);
} else
getCurrentSession().save(object);
if (userOp)
createUser(userManager.getUsername(object), clearPwd);
}
@Override
public void deleteObject(Serializable id) {
T entity = getObject(id);
if (entity != null) {
getCurrentSession().delete(entity);
if (userOperation(entity))
dropUser(entity);
}
}
@Override
public void uploadBinary(byte[] bytes, String op, String pp, Serializable id) {
T objectInDb = getObject(id);
Utils.setValue(op, objectInDb, bytes);
if (pp.length() > 0)
Utils.setValue(pp, objectInDb, getPreviewBytes(bytes, op));
getCurrentSession().save(objectInDb);
}
@Override
public void deleteBinary(String op, String pp, Serializable id) {
T objectInDb = getObject(id);
Utils.setValue(op, objectInDb, null);
if (pp.length() > 0)
Utils.setValue(pp, objectInDb, null);
getCurrentSession().save(objectInDb);
}
protected void update(T object, T objectInDb) {}
protected void prepareList(ShrDao.CriteriaTools tools, DataSetClauses clauses) {
String field;
Object comparingValue = null;
if (clauses != null) {
ArrayList predicateList = new ArrayList();
DataSetClauses.Criterion criterion;
Predicate predicate = null;
for (Iterator it = clauses.filter.entrySet().iterator(); it.hasNext();) {
Entry entry = (Entry) it.next();
field = entry.getKey();
criterion = (DataSetClauses.Criterion) entry.getValue();
comparingValue = criterion.value;
predicate = criterion.operator == null || criterion.operator.compareTo("")==0 ?
predEQ(tools, field, comparingValue) :
criterion.operator.compareTo(">")==0 ?
predGT(tools, field, comparingValue) :
criterion.operator.compareTo("<")==0 ?
predLT(tools, field, comparingValue) :
criterion.operator.compareTo("N")==0 ?
predNull(tools, field, comparingValue) :
predNotNull(tools, field, comparingValue);
if (predicate != null)
predicateList.add(predicate);
}
Predicate[] predicates = new Predicate[predicateList.size()];
int index = 0;
for (Predicate pred : predicateList) {
predicates[index] = pred;
index++;
}
tools.criteria.where(predicates);
}
}
protected Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
protected Predicate predEQ(CriteriaTools tools, String field, Object value) {
CriteriaBuilder builder = tools.builder;
Root from = tools.from;
if (value instanceof String) {
String text = (String) value;
return text.compareToIgnoreCase("%") == 0 ?
null :
builder.like(from.get(field), (String) value);
}
else if (value instanceof Date)
return builder.equal(from.get(field), (Date) value);
else if (value instanceof Number)
return builder.equal(from.get(field), (Number) value);
else
return builder.equal(from.