service.GroupService.java Source code

Java tutorial

Introduction

Here is the source code for service.GroupService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import entity.Group;
import entity.GroupRole;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import persistence.GroupDao;
import persistence.RolesGroupDao;
import service.parent.PrimService;
import datastructure.roles.GroupRoleInfo;
import datastructure.roles.RoleInfo;
import support.ServiceResult;

/**
 *
 * @author Rice Pavel
 */
@Service
@Transactional
public class GroupService extends PrimService {

    private GroupDao groupDao;

    private RolesGroupDao rolesGroupDao;

    @Autowired
    public void setGroupDao(GroupDao groupDao) {
        this.groupDao = groupDao;
    }

    @Autowired
    public void setRolesGroupDao(RolesGroupDao rolesGroupDao) {
        this.rolesGroupDao = rolesGroupDao;
    }

    public void save(Group group) {
        if (group.getGroupId() == null) {
            groupDao.save(group);
        }
    }

    public List<Group> getAllGroups() {
        return groupDao.getAll();
    }

    public void update(Group group) {
        groupDao.update(group);
    }

    public ServiceResult deleteGroup(Group group) {
        return _delete(group, groupDao);
    }

    public Group getOneGroup(String groupId) {
        //     
        return groupDao.getOne(groupId);
    }

    public GroupRoleInfo getOneGroupInfo(String groupId) throws ClassNotFoundException {
        //     
        Group group = groupDao.getOne(groupId);
        //  ?? ? 
        List<String> roles = getRolesList();
        // ?   ,   ?  ? ? 
        return getInfo(group, roles);
    }

    public void changeGroup(String groupId, String[] roles) {
        //  ? ?  group_role,   ? 
        rolesGroupDao.deleteGroupRole(groupId);
        // ? ? ? 
        if (roles != null) {
            for (String role : roles) {
                GroupRole gr = new GroupRole();
                Group group = new Group();
                group.setGroupId(Long.valueOf(groupId));
                gr.setGroup(group);
                gr.setRole(role);
                rolesGroupDao.addGroupRole(gr);
            }
        }
    }

    private GroupRoleInfo getInfo(Group group, List<String> roles) {
        List<String> activeRoles = new ArrayList();
        for (GroupRole gr : group.getGroupRoles()) {
            activeRoles.add(gr.getRole());
        }
        GroupRoleInfo info = new GroupRoleInfo();
        info.group = group;
        List<RoleInfo> roleInfoList = new ArrayList();
        info.roleInfoList = roleInfoList;
        for (String role : roles) {
            boolean active = activeRoles.contains(role);
            roleInfoList.add(new RoleInfo(role, active));
        }
        return info;
    }

    private List<String> getRolesList() throws ClassNotFoundException {
        Class cl = Class.forName("roles.Roles");
        Field[] fields = cl.getFields();
        List<String> roles = new ArrayList();
        for (Field field : fields) {
            roles.add(field.getName());
        }
        return roles;
    }

}