In this post, I will talk about the API which lets you publish a role or an app instance to a particular organization.
public void addPublication(String roleKey, String orgKeyIntValue) {
EntityPublicationService eps = Platform
.getService(EntityPublicationService.class);
EntityPublication ep = new EntityPublication(roleKey,
PolicyConstants.Resources.ROLE, Long.parseLong(orgKeyIntValue),
true);
List<EntityPublication> epl = new ArrayList<EntityPublication>();
epl.add(ep);
eps.addEntityPublications(epl);
}
What the above will do is take a role and publish it to specific organization specified here by organization key.
Removing(Unpublishing) is far more complicated as you will have to get the publication ID
public void removePublication(String roleKey, String orgKey,
String roleName, String newOrgKey) {
HashMap hm = new HashMap();
hm.put("STARTROW", 0);
hm.put("ENDROW", 1);
EntityPublicationSearchCriteria epsc = new EntityPublicationSearchCriteria();
epsc.setRoleName(roleName);
epsc.setEntityId(roleKey.toString());
epsc.setEntityType(PolicyConstants.Resources.ROLE);
epsc.setScopeId(orgKey);
EntityPublicationService eps = Platform
.getService(EntityPublicationService.class);
List<EntityPublication> epl = eps.search(epsc, null);
for (EntityPublication ep : epl) {
Long pubId = ep.getEntityPublicationId();
EntityPublication ep2 = new EntityPublication(roleKey,
PolicyConstants.Resources.ROLE, newOrgKey, true);
ep2.setEntityPublicationId(pubId);
epl.add(ep);
eps.removeEntityPublications(epl, true);
}
}