/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.smartitengineering.user.service.impl; import com.smartitengineering.dao.common.queryparam.FetchMode; import com.smartitengineering.dao.common.queryparam.Order; import com.smartitengineering.dao.common.queryparam.QueryParameter; import com.smartitengineering.dao.common.queryparam.QueryParameterFactory; import com.smartitengineering.dao.impl.hibernate.AbstractCommonDaoImpl; import com.smartitengineering.user.domain.Privilege; import com.smartitengineering.user.domain.UniqueConstrainedField; import com.smartitengineering.user.domain.User; import com.smartitengineering.user.service.ExceptionMessage; import com.smartitengineering.user.service.PrivilegeService; import com.smartitengineering.user.service.UserService; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.hibernate.StaleStateException; import org.hibernate.exception.ConstraintViolationException; /** * * @author russel */ public class PrivilegeServiceImpl extends AbstractCommonDaoImpl<Privilege> implements PrivilegeService { public PrivilegeServiceImpl() { setEntityClass(Privilege.class); } private UserService userService; public UserService getUserService() { return userService; } public void setUserService(UserService userService) { this.userService = userService; } @Override public void create(Privilege privilege) { validatePrivilege(privilege); final Date date = new Date(); privilege.setCreationDate(date); privilege.setLastModifiedDate(date); try { super.save(privilege); } catch (ConstraintViolationException e) { String message = ExceptionMessage.CONSTRAINT_VIOLATION_EXCEPTION.name() + "-" + UniqueConstrainedField.OTHER; throw new RuntimeException(message, e); } catch (StaleStateException e) { String message = ExceptionMessage.STALE_OBJECT_STATE_EXCEPTION.name() + "-" + UniqueConstrainedField.OTHER; throw new RuntimeException(message, e); } } @Override public void delete(Privilege privilege) { try { super.delete(privilege); } catch (Exception e) { } } @Override public void update(Privilege privilege) { final Date date = new Date(); privilege.setLastModifiedDate(date); validatePrivilege(privilege); try { super.update(privilege); } catch (ConstraintViolationException e) { String message = ExceptionMessage.CONSTRAINT_VIOLATION_EXCEPTION.name() + "-" + UniqueConstrainedField.OTHER; throw new RuntimeException(message, e); } catch (StaleStateException e) { String message = ExceptionMessage.STALE_OBJECT_STATE_EXCEPTION.name() + "-" + UniqueConstrainedField.OTHER; throw new RuntimeException(message, e); } } // public Privilege getPrivilegesByObjectID(String objectID){ // // } @Override public Privilege getPrivilegeByOrganizationAndPrivilegeName(String organizationName, String privilegename) { return super.getSingle(QueryParameterFactory.getEqualPropertyParam("name", privilegename), QueryParameterFactory.getNestedParametersParam("parentOrganization", FetchMode.DEFAULT, QueryParameterFactory.getEqualPropertyParam( "uniqueShortName", organizationName))); } @Override public Collection<Privilege> getPrivilegesByOrganizationAndUser(String organizationName, String userName) { User user = userService.getUserByOrganizationAndUserName(organizationName, userName); return user.getPrivileges(); } @Override public Collection<Privilege> getPrivilegesByOrganization(String organization) { Collection<Privilege> users = new HashSet<Privilege>(); QueryParameter qp = QueryParameterFactory.getNestedParametersParam("parentOrganization", FetchMode.DEFAULT, QueryParameterFactory. getEqualPropertyParam("uniqueShortName", organization)); return super.getList(qp); } public Collection<Privilege> getPrivilegess(String nameLike, String name, boolean isSmallerThan, int count) { List<QueryParameter> params = new ArrayList<QueryParameter>(); if (StringUtils.isNotBlank(nameLike)) { final QueryParameter orgNameLikeParam = QueryParameterFactory.getNestedParametersParam("name", FetchMode.EAGER, QueryParameterFactory. getStringLikePropertyParam("name", nameLike)); params.add(orgNameLikeParam); } else { params.add(QueryParameterFactory.getNestedParametersParam("username", FetchMode.EAGER)); } if (StringUtils.isNotBlank(name)) { if (isSmallerThan) { params.add(QueryParameterFactory.getLesserThanPropertyParam("username", name)); } else { params.add(QueryParameterFactory.getGreaterThanPropertyParam("username", name)); } } params.add(QueryParameterFactory.getMaxResultsParam(count)); params.add(QueryParameterFactory.getOrderByParam("id", Order.DESC)); params.add(QueryParameterFactory.getDistinctPropProjectionParam("id")); List<Integer> userIDs = getOtherList(params); if (userIDs != null && !userIDs.isEmpty()) { List<Privilege> privileges = new ArrayList<Privilege>(super.getByIds(userIDs)); Collections.sort(privileges, new Comparator<Privilege>() { @Override public int compare(Privilege o1, Privilege o2) { return o1.getId().compareTo(o2.getId()) * -1; } }); return privileges; } else { return Collections.emptySet(); } } public void validatePrivilege(Privilege privilege) { if (StringUtils.isEmpty(privilege.getName())) { throw new RuntimeException(ExceptionMessage.CONSTRAINT_VIOLATION_EXCEPTION.name() + "-" + UniqueConstrainedField.PRIVILEGE_NAME. name()); } if (privilege.getId() == null) { Integer count = (Integer) super.getOther( QueryParameterFactory.getElementCountParam("name"), QueryParameterFactory.getConjunctionParam( QueryParameterFactory.getEqualPropertyParam("parentOrganization.id", privilege.getParentOrganization().getId()), QueryParameterFactory. getStringLikePropertyParam( "name", privilege.getName()))); if (count.intValue() > 0) { throw new RuntimeException(ExceptionMessage.CONSTRAINT_VIOLATION_EXCEPTION.name() + "-" + UniqueConstrainedField.PRIVILEGE_NAME. name()); } } else { Integer count = (Integer) super.getOther( QueryParameterFactory.getElementCountParam("name"), QueryParameterFactory.getConjunctionParam( QueryParameterFactory.getNotEqualPropertyParam("id", privilege.getId()), QueryParameterFactory.getEqualPropertyParam( "parentOrganization.id", privilege.getParentOrganization().getId()), QueryParameterFactory.getStringLikePropertyParam( "name", privilege.getName()))); if (count.intValue() > 0) { throw new RuntimeException(ExceptionMessage.CONSTRAINT_VIOLATION_EXCEPTION.name() + "-" + UniqueConstrainedField.PRIVILEGE_NAME. name()); } } } @Override public Collection<Privilege> getPrivilegesByOrganizationNameAndObjectID(String organizationName, String objectID) { return super.getList(QueryParameterFactory.getNestedParametersParam("securedObject", FetchMode.DEFAULT, QueryParameterFactory. getEqualPropertyParam("objectID", objectID)), QueryParameterFactory.getNestedParametersParam( "parentOrganization", FetchMode.DEFAULT, QueryParameterFactory.getEqualPropertyParam("uniqueShortName", organizationName))); } @Override public Set<Privilege> getPrivilegesByIds(Long... ids) { return getPrivilegesByIds(Arrays.<Long>asList(ids)); } @Override public Set<Privilege> getPrivilegesByIds(List<Long> ids) { List<Integer> ints = new ArrayList<Integer>(ids.size()); for (Long id : ids) { ints.add(id.intValue()); } return getByIds(ints); } }