Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.bonitasoft.engine.profile.ProfilesExporter Maven / Gradle / Ivy
/**
* Copyright (C) 2019 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.bonitasoft.engine.profile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.bonitasoft.engine.commons.exceptions.SBonitaException;
import org.bonitasoft.engine.exception.ExecutionException;
import org.bonitasoft.engine.identity.IdentityService;
import org.bonitasoft.engine.identity.SGroupNotFoundException;
import org.bonitasoft.engine.identity.SRoleNotFoundException;
import org.bonitasoft.engine.identity.SUserNotFoundException;
import org.bonitasoft.engine.persistence.FilterOption;
import org.bonitasoft.engine.persistence.OrderByOption;
import org.bonitasoft.engine.persistence.OrderByType;
import org.bonitasoft.engine.persistence.QueryOptions;
import org.bonitasoft.engine.persistence.SBonitaReadException;
import org.bonitasoft.engine.profile.model.SProfile;
import org.bonitasoft.engine.profile.model.SProfileEntry;
import org.bonitasoft.engine.profile.model.SProfileMember;
import org.bonitasoft.engine.profile.xml.MembershipNode;
import org.bonitasoft.engine.profile.xml.ParentProfileEntryNode;
import org.bonitasoft.engine.profile.xml.ProfileEntryNode;
import org.bonitasoft.engine.profile.xml.ProfileMappingNode;
import org.bonitasoft.engine.profile.xml.ProfileNode;
import org.bonitasoft.engine.profile.xml.ProfilesNode;
/**
* @author Baptiste Mesta
*/
public class ProfilesExporter {
private final static String USER_SUFFIX = "ForUser";
private final static String ROLE_SUFFIX = "ForRole";
private final static String GROUP_SUFFIX = "ForGroup";
private final static String ROLE_AND_GROUP_SUFFIX = "ForRoleAndGroup";
private static final int NUMBER_OF_RESULTS = 100;
private IdentityService identityService;
private ProfileService profileService;
private ProfilesParser profilesParser;
public ProfilesExporter(IdentityService identityService, ProfileService profileService,
ProfilesParser profilesParser) {
this.identityService = identityService;
this.profileService = profileService;
this.profilesParser = profilesParser;
}
public String exportAllProfiles() throws ExecutionException {
try {
ArrayList sProfiles = getAllProfiles();
ProfilesNode profiles = toProfiles(sProfiles);
return profilesParser.convert(profiles);
} catch (SBonitaException | JAXBException e) {
throw new ExecutionException(e);
}
}
private ArrayList getAllProfiles() throws SBonitaReadException {
ArrayList profiles = new ArrayList<>();
int from = 0;
List sProfiles;
do {
final QueryOptions queryOptions = new QueryOptions(from, 100, Collections.singletonList(new OrderByOption(
SProfile.class, SProfile.NAME, OrderByType.ASC)), Collections. emptyList(), null);
sProfiles = profileService.searchProfiles(queryOptions);
from += 100;
profiles.addAll(sProfiles);
} while (sProfiles.size() == 100);
return profiles;
}
public String exportProfiles(List longs) throws ExecutionException {
try {
List sProfile = profileService.getProfiles(longs);
ProfilesNode profiles = toProfiles(sProfile);
return profilesParser.convert(profiles);
} catch (SBonitaException | JAXBException e) {
throw new ExecutionException(e);
}
}
ProfilesNode toProfiles(List sProfile)
throws SBonitaReadException, SUserNotFoundException, SGroupNotFoundException, SRoleNotFoundException {
ArrayList profiles = new ArrayList<>();
for (SProfile profile : sProfile) {
profiles.add(toProfile(profile));
}
return new ProfilesNode(profiles);
}
private ProfileNode toProfile(SProfile sProfile)
throws SBonitaReadException, SUserNotFoundException, SGroupNotFoundException, SRoleNotFoundException {
ProfileNode profile = new ProfileNode(sProfile.getName(), sProfile.isDefault());
profile.setDescription(sProfile.getDescription());
profile.setParentProfileEntries(getProfilesEntries(sProfile));
profile.setProfileMapping(getProfileMapping(sProfile));
return profile;
}
private List getProfilesEntries(SProfile profile) throws SBonitaReadException {
ArrayList parentProfileEntries = new ArrayList<>();
List sProfileEntries = searchProfileEntries(profile.getId(), 0);
for (SProfileEntry sProfileEntry : sProfileEntries) {
parentProfileEntries.add(toParentProfileEntry(sProfileEntry));
}
return parentProfileEntries;
}
private ParentProfileEntryNode toParentProfileEntry(SProfileEntry sProfileEntry) throws SBonitaReadException {
ParentProfileEntryNode parentProfileEntry = new ParentProfileEntryNode(sProfileEntry.getName());
parentProfileEntry.setCustom(sProfileEntry.isCustom());
parentProfileEntry.setIndex(sProfileEntry.getIndex());
parentProfileEntry.setDescription(sProfileEntry.getDescription());
parentProfileEntry.setType(sProfileEntry.getType());
parentProfileEntry.setPage(sProfileEntry.getPage());
parentProfileEntry.setChildProfileEntries(getChildrenProfilesEntries(sProfileEntry));
return parentProfileEntry;
}
private List getChildrenProfilesEntries(SProfileEntry sProfileEntry) throws SBonitaReadException {
ArrayList profileEntries = new ArrayList<>();
List sProfileEntries = searchProfileEntries(sProfileEntry.getProfileId(), sProfileEntry.getId());
for (SProfileEntry profileEntry : sProfileEntries) {
profileEntries.add(toProfileEntry(profileEntry, sProfileEntry.getName()));
}
return profileEntries;
}
private ProfileEntryNode toProfileEntry(SProfileEntry sProfileEntry, String parentName) {
ProfileEntryNode profileEntry = new ProfileEntryNode(sProfileEntry.getName());
profileEntry.setCustom(sProfileEntry.isCustom());
profileEntry.setIndex(sProfileEntry.getIndex());
profileEntry.setDescription(sProfileEntry.getDescription());
profileEntry.setType(sProfileEntry.getType());
profileEntry.setPage(sProfileEntry.getPage());
return profileEntry;
}
protected List searchProfileEntries(final long profileId, final long parentId)
throws SBonitaReadException {
final List orderByOptions = Collections
.singletonList(new OrderByOption(SProfileEntry.class, SProfileEntry.INDEX, OrderByType.ASC));
final List filters = new ArrayList<>();
filters.add(new FilterOption(SProfileEntry.class, SProfileEntry.PROFILE_ID, profileId));
filters.add(new FilterOption(SProfileEntry.class, SProfileEntry.PARENT_ID, parentId));
final QueryOptions queryOptions = new QueryOptions(0, Integer.MAX_VALUE, orderByOptions, filters, null);//profiles entry of a profile will not have a lot of elements
return profileService.searchProfileEntries(queryOptions);
}
private ProfileMappingNode getProfileMapping(SProfile sProfile)
throws SUserNotFoundException, SBonitaReadException, SGroupNotFoundException, SRoleNotFoundException {
ProfileMappingNode profileMapping = new ProfileMappingNode();
profileMapping.setUsers(getUsers(sProfile));
profileMapping.setGroups(getGroups(sProfile));
profileMapping.setRoles(getRoles(sProfile));
profileMapping.setMemberships(getMemberships(sProfile));
return profileMapping;
}
private List getUsers(SProfile profile) throws SBonitaReadException, SUserNotFoundException {
ArrayList users = new ArrayList<>();
int pageIndex = 0;
List sProfileMembers;
do {
sProfileMembers = searchProfileMembers(pageIndex, profile.getId(), USER_SUFFIX);
for (final SProfileMember sProfileMember : sProfileMembers) {
users.add(identityService.getUser(sProfileMember.getUserId()).getUserName());
}
pageIndex++;
} while (sProfileMembers.size() > 0);
return users;
}
private List getGroups(SProfile profile) throws SBonitaReadException, SGroupNotFoundException {
ArrayList groups = new ArrayList<>();
int pageIndex = 0;
List sProfileMembers;
do {
sProfileMembers = searchProfileMembers(pageIndex, profile.getId(), GROUP_SUFFIX);
for (final SProfileMember sProfileMember : sProfileMembers) {
groups.add(identityService.getGroup(sProfileMember.getGroupId()).getPath());
}
pageIndex++;
} while (sProfileMembers.size() > 0);
return groups;
}
private List getRoles(SProfile profile) throws SBonitaReadException, SRoleNotFoundException {
ArrayList roles = new ArrayList<>();
int pageIndex = 0;
List sProfileMembers;
do {
sProfileMembers = searchProfileMembers(pageIndex, profile.getId(), ROLE_SUFFIX);
for (final SProfileMember sProfileMember : sProfileMembers) {
roles.add(identityService.getRole(sProfileMember.getRoleId()).getName());
}
pageIndex++;
} while (sProfileMembers.size() > 0);
return roles;
}
private List getMemberships(SProfile profile)
throws SBonitaReadException, SRoleNotFoundException, SGroupNotFoundException {
ArrayList memberships = new ArrayList<>();
int pageIndex = 0;
List sProfileMembers;
do {
sProfileMembers = searchProfileMembers(pageIndex, profile.getId(), ROLE_AND_GROUP_SUFFIX);
for (final SProfileMember sProfileMember : sProfileMembers) {
memberships.add(new MembershipNode(identityService.getGroup(sProfileMember.getGroupId()).getPath(),
identityService.getRole(sProfileMember.getRoleId()).getName()));
}
pageIndex++;
} while (sProfileMembers.size() > 0);
return memberships;
}
private List searchProfileMembers(final int fromIndex, final long profileId,
final String querySuffix) throws SBonitaReadException {
final QueryOptions queryOptions = new QueryOptions(fromIndex * NUMBER_OF_RESULTS, NUMBER_OF_RESULTS,
Collections.singletonList(new OrderByOption(
SProfileMember.class, SProfileMember.ID, OrderByType.ASC)),
Collections.singletonList(new FilterOption(SProfileMember.class,
SProfileEntry.PROFILE_ID, profileId)),
null);
return profileService.searchProfileMembers(querySuffix, queryOptions);
}
}