protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); init(); ArrayList<String> data = new ArrayList<String>(); data.add("Info"); data.add("Assessment"); data.add("Billing"); data.add("Release"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data); GroupAdapter groupAdapter = new GroupAdapter(this,data); setListAdapter(groupAdapter); getListView().setDividerHeight(10); //setting the margin ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) getListView().getLayoutParams(); params.setMargins(10, 0, 10, 0); }
protected TypedQuery<GroupRoleMappingEntity> getGroupRoleMappingEntityTypedQuery(RoleModel role) { TypedQuery<GroupRoleMappingEntity> query = em.createNamedQuery("groupHasRole", GroupRoleMappingEntity.class); query.setParameter("group", getEntity()); query.setParameter("roleId", role.getId()); return query; }
@Override public void deleteRoleMapping(RoleModel role) { if (group == null || role == null) return; TypedQuery<GroupRoleMappingEntity> query = getGroupRoleMappingEntityTypedQuery(role); List<GroupRoleMappingEntity> results = query.getResultList(); if (results.size() == 0) return; for (GroupRoleMappingEntity entity : results) { em.remove(entity); } em.flush(); }
@Override public void setAttribute(String name, List<String> values) { // Remove all existing removeAttribute(name); // Put all new for (String value : values) { persistAttributeValue(name, value); } }
@Override public int hashCode() { return getId().hashCode(); }
@Override public Set<RoleModel> getRealmRoleMappings() { Set<RoleModel> roleMappings = getRoleMappings(); Set<RoleModel> realmRoles = new HashSet<RoleModel>(); for (RoleModel role : roleMappings) { RoleContainerModel container = role.getContainer(); if (container instanceof RealmModel) { realmRoles.add(role); } } return realmRoles; }
@Override public void addDefaultGroup(GroupModel group) { Collection<GroupEntity> entities = realm.getDefaultGroups(); for (GroupEntity entity : entities) { if (entity.getId().equals(group.getId())) return; } GroupEntity groupEntity = GroupAdapter.toEntity(group, em); realm.getDefaultGroups().add(groupEntity); em.flush(); }
@Override public void setSingleAttribute(String name, String value) { boolean found = false; List<GroupAttributeEntity> toRemove = new ArrayList<>(); for (GroupAttributeEntity attr : group.getAttributes()) { if (attr.getName().equals(name)) { if (!found) { attr.setValue(value); found = true; } else { toRemove.add(attr); } } } for (GroupAttributeEntity attr : toRemove) { em.remove(attr); group.getAttributes().remove(attr); } if (found) { return; } persistAttributeValue(name, value); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof GroupModel)) return false; GroupModel that = (GroupModel) o; return that.getId().equals(getId()); }
@Override public boolean hasRole(RoleModel role) { Set<RoleModel> roles = getRoleMappings(); return RoleUtils.hasRole(roles, role); }
@Override public GroupModel getGroupById(String id, RealmModel realm) { GroupEntity groupEntity = em.find(GroupEntity.class, id); if (groupEntity == null) return null; if (!groupEntity.getRealm().getId().equals(realm.getId())) return null; GroupAdapter adapter = new GroupAdapter(realm, em, groupEntity); return adapter; }
@Override public void addChild(GroupModel subGroup) { if (subGroup.getId().equals(getId())) { return; } subGroup.setParent(this); }
@Override public Set<RoleModel> getRoleMappings() { // we query ids only as the role might be cached and following the @ManyToOne will result in a load // even if we're getting just the id. TypedQuery<String> query = em.createNamedQuery("groupRoleMappingIds", String.class); query.setParameter("group", getEntity()); List<String> ids = query.getResultList(); Set<RoleModel> roles = new HashSet<RoleModel>(); for (String roleId : ids) { RoleModel roleById = realm.getRoleById(roleId); if (roleById == null) continue; roles.add(roleById); } return roles; }
@Override public Set<RoleModel> getClientRoleMappings(ClientModel app) { Set<RoleModel> roleMappings = getRoleMappings(); Set<RoleModel> roles = new HashSet<RoleModel>(); for (RoleModel role : roleMappings) { RoleContainerModel container = role.getContainer(); if (container instanceof ClientModel) { ClientModel appModel = (ClientModel)container; if (appModel.getId().equals(app.getId())) { roles.add(role); } } } return roles; }
groupAdapter = new GroupAdapter(this, list);
@Override public void removeChild(GroupModel subGroup) { if (subGroup.getId().equals(getId())) { return; } subGroup.setParent(null); }
public static GroupEntity toEntity(GroupModel model, EntityManager em) { if (model instanceof GroupAdapter) { return ((GroupAdapter)model).getEntity(); } return em.getReference(GroupEntity.class, model.getId()); }
@Override public GroupModel createGroup(RealmModel realm, String id, String name) { if (id == null) id = KeycloakModelUtils.generateId(); GroupEntity groupEntity = new GroupEntity(); groupEntity.setId(id); groupEntity.setName(name); RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); groupEntity.setRealm(realmEntity); em.persist(groupEntity); em.flush(); realmEntity.getGroups().add(groupEntity); GroupAdapter adapter = new GroupAdapter(realm, em, groupEntity); return adapter; }