Skip to content

Commit

Permalink
SysConfigManager getAll signature change.
Browse files Browse the repository at this point in the history
ManagerSupport no longer needs the class. Refactor other managers to adapt to this.
  • Loading branch information
jrfinc committed Jun 8, 2017
1 parent 901131a commit 3ac9d32
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 225 deletions.
59 changes: 34 additions & 25 deletions modules/dbsupport/src/main/java/org/jpos/ee/ManagerSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.criteria.*;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -15,12 +16,14 @@
public class ManagerSupport<T> {

protected DB db;
private Class<T> clazz;

public ManagerSupport(DB db) {
this.db = db;
clazz = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

public int getItemCount(Class<T> clazz) throws Exception {
public int getItemCount() {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
Root<T> root = query.from(clazz);
Expand All @@ -31,7 +34,7 @@ public int getItemCount(Class<T> clazz) throws Exception {
return db.session().createQuery(query).getSingleResult().intValue();
}

public List<T> getAll(Class<T> clazz, int offset, int limit, Map<String,Boolean> orders) {
public List<T> getAll(int offset, int limit, Map<String,Boolean> orders) {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<T> query = criteriaBuilder.createQuery(clazz);
Root<T> root = query.from(clazz);
Expand All @@ -58,39 +61,45 @@ public List<T> getAll(Class<T> clazz, int offset, int limit, Map<String,Boolean>
return list;
}

public List<T> getAll(Class<T> clazz) {
return this.getAll(clazz,0,-1,null);

public List<T> getAll() {
return this.getAll(0,-1,null);
}

public T getItemByParam(Class<T> clazz, String param, Object value, boolean withFilter) {
public T getItemByParam(String param, Object value, boolean withFilter) {
try {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<T> query = criteriaBuilder.createQuery(clazz);
Root<T> root = query.from(clazz);
Predicate equals = criteriaBuilder.equal(root.get(param), value);
query.where(equals);
if (withFilter) {
Predicate[] predicates = buildPredicates(root);
if (predicates != null) {
//overrides previous
query.where(criteriaBuilder.and(criteriaBuilder.and(predicates), equals));
}
}
query.select(root);
CriteriaQuery<T> query = createQueryByParam(param, value, withFilter);
return db.session().createQuery(query).getSingleResult();
} catch (NoResultException nre) {
return null;
}
}

protected Predicate[] buildPredicates(Root<T> root) { return null; }





public List<T> getItemsByParam(String param, Object value, boolean withFilter) {
try {
CriteriaQuery<T> query = createQueryByParam(param, value, withFilter);
return db.session().createQuery(query).list();
} catch (NoResultException nre) {
return null;
}
}

private CriteriaQuery<T> createQueryByParam(String param, Object value, boolean withFilter) {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<T> query = criteriaBuilder.createQuery(clazz);
Root<T> root = query.from(clazz);
Predicate equals = criteriaBuilder.equal(root.get(param), value);
query.where(equals);
if (withFilter) {
Predicate[] predicates = buildPredicates(root);
if (predicates != null) {
//overrides previous predicates
query.where(criteriaBuilder.and(criteriaBuilder.and(predicates), equals));
}
}
query.select(root);
return query;
}

protected Predicate[] buildPredicates(Root<T> root) { return null; }

}
17 changes: 6 additions & 11 deletions modules/eeuser/src/main/java/org/jpos/ee/RevisionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import javax.persistence.criteria.Root;

@SuppressWarnings("unused")
public class RevisionManager {
DB db;
public class RevisionManager extends ManagerSupport<Revision> {

public RevisionManager (DB db) {
this.db = db;
super(db);
}
@SuppressWarnings("unchecked")
public List<Revision> getRevisionsByRef (String ref)
Expand All @@ -58,7 +58,9 @@ public List<Revision> getRevisionsByAuthor (User author)
return (List<Revision>) crit.list();
}

public List<Revision> getAll(int offset, int limit, Map<String,Boolean> orders) throws Exception {
//overridden to avoid LazyInitializationExc
@Override
public List<Revision> getAll(int offset, int limit, Map<String,Boolean> orders) {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<Revision> query = criteriaBuilder.createQuery(Revision.class);
Root<Revision> root = query.from(Revision.class);
Expand All @@ -78,13 +80,6 @@ public List<Revision> getAll(int offset, int limit, Map<String,Boolean> orders)
.getResultList();
}

public int getItemCount() throws Exception {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
query.select(criteriaBuilder.count(query.from(Revision.class)));
return db.session().createQuery(query).getSingleResult().intValue();
}



/**
Expand Down
45 changes: 4 additions & 41 deletions modules/eeuser/src/main/java/org/jpos/ee/RoleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,19 @@

package org.jpos.ee;

import org.hibernate.query.criteria.internal.OrderImpl;
public class RoleManager extends ManagerSupport<Role> {

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RoleManager {
private DB db;
public RoleManager (DB db) {
this.db = db;
super(db);
}

public Role getRoleByName (String name) {
return (Role) db.session().createQuery("from Role c WHERE c.name=:name").
setParameter("name", name).uniqueResult();
return super.getItemByParam("name",name,true);
}

public Role getRoleById (long id) {
return (Role) db.session().createQuery("from Role c WHERE c.id=:id").
setParameter("id", id).uniqueResult();
}

public List<Role> getAll() {
return db.session().createQuery("from Role", Role.class).list();
}

public List<Role> getAll(int offset, int limit, Map<String,Boolean> orders) throws Exception {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<Role> query = criteriaBuilder.createQuery(Role.class);
Root<Role> root = query.from(Role.class);
List<Order> orderList = new ArrayList<>();
//ORDERS
for (Map.Entry<String,Boolean> entry : orders.entrySet()) {
OrderImpl order = new OrderImpl(root.get(entry.getKey()),entry.getValue());
orderList.add(order);
}
query.select(root);
query.orderBy(orderList);
return db.session().createQuery(query)
.setMaxResults(limit)
.setFirstResult(offset)
.getResultList();
}
return super.getItemByParam("id",id,true);

public int getItemCount() throws Exception {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
query.select(criteriaBuilder.count(query.from(Role.class)));
return db.session().createQuery(query).getSingleResult().intValue();
}

}
20 changes: 3 additions & 17 deletions modules/eeuser/src/main/java/org/jpos/ee/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ public void setPassword (User u, String clearpass, User author, VERSION v) throw
* @throws HibernateException on low level hibernate related exception
*/
public List findAll () throws HibernateException {
return super.getAll(User.class);
}

public List getAll(int offset, int limit, Map<String,Boolean> orders) {
return super.getAll(User.class,offset,limit,orders);
return super.getAll();
}

@Override
Expand All @@ -102,16 +98,6 @@ protected Predicate[] buildPredicates(Root<User> root) {
};
}

public int getItemCount() {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
Root<User> root = query.from(User.class);
Predicate notDeleted = criteriaBuilder.isFalse(root.get("deleted"));
query.where(notDeleted);
query.select(criteriaBuilder.count(root));
return db.session().createQuery(query).getSingleResult().intValue();
}

public User getUserByNick (String nick)
throws HibernateException
{
Expand All @@ -120,7 +106,7 @@ public User getUserByNick (String nick)
public User getUserByNick (String nick, boolean includeDeleted)
throws HibernateException
{
return getItemByParam(User.class,"nick",nick,!includeDeleted);
return getItemByParam("nick",nick,!includeDeleted);
}

/**
Expand All @@ -140,7 +126,7 @@ public User getUserByNick (String nick, String pass)
}

public User getUserById(long id, boolean includeDeleted) throws HibernateException {
return getItemByParam(User.class,"id",id,!includeDeleted);
return getItemByParam("id",id,!includeDeleted);
}

public User getUserById(long id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,45 +96,4 @@ protected String getAuthorLink(String nickAndId, String currentRevision) {
String backRoute = "revision_history" + (!currentRevision.isEmpty() ? "." + currentRevision : "");
return "<a href=\"#!/users/" + data[1] + "?back="+ backRoute +"\">" + nickAndId + "</a>";
}


// protected Converter getRefConverter(String currentRevision) {
// return new Converter<String, String>() {
// @Override
// public String convertToModel(String value, Class<? extends String> targetType, Locale locale) throws ConversionException {
// if (value != null) {
// return value.split("<,>")[1];
// }
// return "";
// }
//
// @Override
// public String convertToPresentation(String value, Class<? extends String> targetType, Locale locale) throws ConversionException {
// return getLink(value,currentRevision);
// }
//
// @Override
// public Class<String> getModelType() {
// return String.class;
// }
//
// @Override
// public Class<String> getPresentationType() {
// return String.class;
// }
// };
// }

// protected Converter getAuthorConverter(String currentRevision) {
// return new UserConverter() {
// @Override
// public String convertToPresentation(User value, Class<? extends String> targetType, Locale locale) throws ConversionException {
// if (value == null)
// return null;
// else {
// return getAuthorLink(value.getNickAndId(),currentRevision);
// }
// }
// };
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ public boolean updateEntity (Binder binder) throws BLException {
}
}

public SysConfig[] getPermissions () {
public List<SysConfig> getPermissions () {
try {
return (SysConfig[]) DB.exec( (db) -> {
return (List<SysConfig>) DB.exec( (db) -> {
SysConfigManager mgr = new SysConfigManager(db, "perm.");

return mgr.getAll();
});
} catch (Exception e) {
Expand All @@ -96,20 +95,4 @@ public Role getRoleByName(String name) {
return null;
}
}

// public Validator getNameTakenValidator(final Role selectedR) {
//
//
//// Validator nameTaken = (Validator) (value, context) -> {
//// String oldName = selectedR.getName();
//// Role role = getRoleByName((String)value);
//// if (role == null || (oldName != null && oldName.trim().equals(((String) value).trim()))) {
//// return ValidationResult.ok();
//// }
//// return ValidationResult.error(getApp().getMessage("errorMessage.fieldTaken",value));
//// };
//// return nameTaken;
//
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ protected Component buildAndBindCustomComponent(String propertyId) {
List<Validator> validators = getValidators(propertyId);
if ("permissions".equals(propertyId)) {
CheckBoxGroup<Permission> f = new CheckBoxGroup("Permissions");
SysConfig[] sysconfigs = ((RolesHelper)getHelper()).getPermissions();
List<SysConfig> sysconfigs = ((RolesHelper)getHelper()).getPermissions();
List<Permission> allPermissions = new ArrayList<>();
//convert SysConfigs to Permissions
for (SysConfig sys : sysconfigs) {
Permission p = Permission.valueOf(sys.getId().substring(5));
allPermissions.add(p);
}
f.setItems(allPermissions);
f.setItemCaptionGenerator((ItemCaptionGenerator<Permission>) item -> sysconfigs[allPermissions.indexOf(item)].getValue());
f.setItemCaptionGenerator((ItemCaptionGenerator<Permission>) item -> sysconfigs.get(allPermissions.indexOf(item)).getValue());
Binder.BindingBuilder builder = getBinder().forField(f);
validators.forEach(builder::withValidator);
builder.bind(propertyId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SysConfig getSysConfig (String param) {
try {
return (SysConfig) DB.exec((db) -> {
SysConfigManager mgr = new SysConfigManager(db, prefix);
return mgr.getItemByParam(SysConfig.class, "id", param, false);
return mgr.getItemByParam("id", param, false);
}
);
} catch (NoResultException nr) {
Expand Down
Loading

0 comments on commit 3ac9d32

Please sign in to comment.