/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013, 2014, 2015 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */
package com.wegas.core.security.persistence;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.wegas.core.persistence.AbstractEntity;
import com.wegas.core.persistence.ListUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.persistence.*;

/**
 *
 * @author Francois-Xavier Aeberhard <fx@red-agent.com>
 */
@Entity
@Table(name = "roles", uniqueConstraints = {
    @UniqueConstraint(columnNames = "name")
})
@Cacheable(true)
@NamedQueries({@NamedQuery(name = "Role.findByName", query = "SELECT a FROM Role a WHERE a.name = :name")})
public class Role extends AbstractEntity {

    private static final long serialVersionUID = 1L;
    /**
     *
     */
    @Id
    @GeneratedValue
    private Long id;
    /**
     *
     */
    @Basic(optional = false)
    @Column(length = 100)
    private String name;
    /**
     *
     */
    @Basic(optional = false)
    @Column(length = 255)
    private String description;
    /**
     *
     */
    //@ElementCollection(fetch = FetchType.EAGER)
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "role")
    private List<Permission> permissions = new ArrayList<>();

    /**
     *
     */
    @JsonIgnore
    @ManyToMany(mappedBy = "roles")
    private Set<AbstractAccount> abstractAccounts = new HashSet<>();
    /**
     *
     */
    protected Role() {
    }

    /**
     *
     * @param name
     */
    public Role(String name) {
        this.name = name;
    }

    @Override
    public void merge(AbstractEntity other) {
        Role r = (Role) other;
        this.setName(r.getName());
        this.setDescription(r.getDescription());
        ListUtils.mergeLists(this.permissions, r.getPermissions());
    }

    /**
     *
     * @return
     */
    @Override
    public Long getId() {
        return id;
    }

    /**
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     *
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     *
     * @return
     */
    public String getDescription() {
        return description;
    }

    /**
     *
     * @param description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     *
     * @return
     */
    public List<Permission> getPermissions() {
        return permissions;
    }

    /**
     *
     * @param permissions
     */
    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
        for (Permission p : this.permissions) {
            p.setRole(this);
        }
    }

    /**
     *
     * @param permission
     * @return
     */
    public boolean addPermission(String permission) {
        return this.addPermission(new Permission(permission));
    }

    /**
     *
     * @param permission
     * @return
     */
    public boolean addPermission(Permission permission) {
        if (!this.permissions.contains(permission)) {
            permission.setRole(this);
            return this.permissions.add(permission);
        } else {
            return false;
        }
    }

    /**
     *
     * @param permission
     * @return
     */
    public boolean removePermission(String permission) {
        Permission perm = new Permission(permission);
        Permission currPerm;
        boolean returnVal = false;
        Iterator<Permission> it = this.permissions.iterator();
        while (it.hasNext()) {
            currPerm = it.next();
            if (currPerm.equals(perm)) {
                it.remove();
                returnVal = true;
            }
        }
        return returnVal;
    }

    public int getNumberOfMember(){
        return abstractAccounts.size();
    }

    /**
     * get role members 
     * @return all accounts which are member of this role
     */
    public Set<AbstractAccount> getAbstractAccounts() {
        return abstractAccounts;
    }

    /**
     * set the role members
     * @param abstractAccounts list of member
     */
    public void setAbstractAccounts(Set<AbstractAccount> abstractAccounts) {
        this.abstractAccounts = abstractAccounts;
    }



    @Override
    public String toString() {
        return "Role(" + this.id + ", " + this.name + ")";
    }
}

@@ -252,16 +252,78 @@
 Ignore;%0A
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;%0A
 import c
@@ -763,16 +763,21 @@
 ueries(%7B
+%0A    
 @NamedQu
@@ -1602,24 +1602,25 @@
 ashSet%3C%3E();%0A
+%0A
     /**%0A    
@@ -4131,16 +4131,17 @@
 Member()
+ 
 %7B%0A      
@@ -4173,32 +4173,95 @@
 .size();%0A    %7D%0A%0A
+    public void setNumberOfMember(int numberOfMember) %7B%0A    %7D%0A%0A
     /**%0A     * g
@@ -4275,17 +4275,23 @@
  members
- 
+%0A     *
 %0A     * 
@@ -4477,24 +4477,31 @@
 ole members%0A
+     *%0A
      * @para
@@ -4676,18 +4676,16 @@
 %0A    %7D%0A%0A
-%0A%0A
     @Ove

Fix #1019