Skip to content

Latest commit

 

History

History
127 lines (93 loc) · 4.19 KB

README.md

File metadata and controls

127 lines (93 loc) · 4.19 KB

Travis CI Build Status Coverage Status Maven Central

Stories in Ready

generic-data

Generic Spring service with Hibernate DAO methods

You can use from Maven central

<dependency>
    <groupId>ro.fortsoft</groupId>
    <artifactId>generic-data-service</artifactId>
    <version>1.9.0</version>
</dependency>

Sample code

The entity class

@Entity
@Table(name = "USERS")
public class User extends DefaultPersistent{
    
    @Id
    @Column(name = "ID")
    @GeneratedValue
    private Integer id;
    
    @Column(name = "USERNAME")
    private String username;
    
    @Column(name = "PASSWORD")
    private String password;
    
    @Column(name = "ENABLED")
    private Boolean enabled;
    
    @Column(name = "SUPERVISOR_ID")
    private User supervisor;
    
    @Column(name = "BADGE_NUMBER")
    private String badgeNumber;
    
    //getters and setters goes here
}

The controller class

import static ro.fortsoft.genericdata.utils.query.QueryParameter.*;

public class SecurityController {
    
    @Autowired
    protected GenericService genericService;
    
    public User getUserById(Integer id){
        return genericService.get(User.class, id);
    }
    
    public User getUserbyUsername(String username){
        User exampleUser = new User();
        exampleUser.setUsername(username);
        return genericService.getUnique(User.class, exampleUser);
    }
    
    public User getEnabledUsersByBadgeNumberOrdered(String badge){
        return genericService.getList(User.class, QueryParameter.build().where("enabled",EQ, Boolean.TRUE).and("badgeNumber",EQ, badge).orderBy("username", true));
    }
    
    public List getUserCreatedBefore(Date before){
        return genericService.getList(User.class, QueryParameter.build().where("creationDate", LE, before));
    }
    
    public List<User> getUsersWithStatus(String status){
    	return genericService.getList(User.class, QueryParameter.build().where("status", EQ, status));
    } 
    
    public List<User> getUsersWithoutSupervisor(){
        return genericService.getList(User.class, QueryParameter.build().where("supervisor", IS_NULL));
    }
    
    public List<User> getUsersWithBadgeContainingTextAndIgnoreCase(String text){
        return genericService.getList(User.class, QueryParameter.build().where("badgeNumber", ILIKE_ANYWHERE, text));
    }
    
    public List<User> getUsersFromSql(String sql){
        return genericService.getList(User.class, QueryParameter.build().where(sql));
    }
    
    public List<User> getUsersBySomeRestrictions(){
        List<ValueRestriction> restrictions = new ArrayList<>();
        restrictions.add(new ValueRestriction("badgeNumber", EQ, "123456"));
        restrictions.add(new ValueRestriction("enabled", EQ, Boolean.FALSE));
        return genericService.getList(User.class, OR_RESTRICTIONS, restrictions);
    }
    
    public List<User> getUsersBySomeRestrictionsNegate(){
        List<ValueRestriction> restrictions = new ArrayList<>();
        ValueRestriction vrBadgeNumber = new ValueRestriction("badgeNumber", EQ, "123456");
        vrBadgeNumber.setIsNotRestriction(true);
        restrictions.add(vrBadgeNumber);
        ValueRestriction vrEnabled = new ValueRestriction("enabled", EQ, Boolean.FALSE);
        vrEnabled.setIsNotRestriction(true);
        restrictions.add(vrEnabled);
        return genericService.getList(User.class, OR_RESTRICTIONS, restrictions);
    }
}