use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class OrganisationUnitObjectBundleHook method postCommit.
@Override
public void postCommit(ObjectBundle bundle) {
if (!bundle.getObjectMap().containsKey(OrganisationUnit.class))
return;
List<IdentifiableObject> objects = bundle.getObjectMap().get(OrganisationUnit.class);
Map<String, Map<String, Object>> objectReferences = bundle.getObjectReferences(OrganisationUnit.class);
Session session = sessionFactory.getCurrentSession();
for (IdentifiableObject identifiableObject : objects) {
identifiableObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), identifiableObject);
Map<String, Object> objectReferenceMap = objectReferences.get(identifiableObject.getUid());
if (objectReferenceMap == null || objectReferenceMap.isEmpty() || !objectReferenceMap.containsKey("parent")) {
continue;
}
OrganisationUnit organisationUnit = (OrganisationUnit) identifiableObject;
OrganisationUnit parentRef = (OrganisationUnit) objectReferenceMap.get("parent");
OrganisationUnit parent = bundle.getPreheat().get(bundle.getPreheatIdentifier(), parentRef);
organisationUnit.setParent(parent);
session.update(organisationUnit);
}
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class UserObjectBundleHook method postCommit.
@Override
@SuppressWarnings("unchecked")
public void postCommit(ObjectBundle bundle) {
if (!bundle.getObjectMap().containsKey(User.class))
return;
List<IdentifiableObject> objects = bundle.getObjectMap().get(User.class);
Map<String, Map<String, Object>> userReferences = bundle.getObjectReferences(User.class);
Map<String, Map<String, Object>> userCredentialsReferences = bundle.getObjectReferences(UserCredentials.class);
if (userReferences == null || userReferences.isEmpty() || userCredentialsReferences == null || userCredentialsReferences.isEmpty()) {
return;
}
for (IdentifiableObject identifiableObject : objects) {
identifiableObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), identifiableObject);
Map<String, Object> userReferenceMap = userReferences.get(identifiableObject.getUid());
if (userReferenceMap == null || userReferenceMap.isEmpty()) {
continue;
}
User user = (User) identifiableObject;
UserCredentials userCredentials = user.getUserCredentials();
if (userCredentials == null) {
continue;
}
Map<String, Object> userCredentialsReferenceMap = userCredentialsReferences.get(userCredentials.getUid());
if (userCredentialsReferenceMap == null || userCredentialsReferenceMap.isEmpty()) {
continue;
}
user.setOrganisationUnits((Set<OrganisationUnit>) userReferenceMap.get("organisationUnits"));
user.setDataViewOrganisationUnits((Set<OrganisationUnit>) userReferenceMap.get("dataViewOrganisationUnits"));
userCredentials.setUser((User) userCredentialsReferenceMap.get("user"));
userCredentials.setUserInfo(user);
preheatService.connectReferences(user, bundle.getPreheat(), bundle.getPreheatIdentifier());
preheatService.connectReferences(userCredentials, bundle.getPreheat(), bundle.getPreheatIdentifier());
user.setUserCredentials(userCredentials);
sessionFactory.getCurrentSession().update(user);
}
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class VersionedObjectObjectBundleHook method postTypeImport.
@Override
public <T extends IdentifiableObject> void postTypeImport(Class<? extends IdentifiableObject> klass, List<T> objects, ObjectBundle bundle) {
if (Section.class.isAssignableFrom(klass)) {
Set<DataSet> dataSets = new HashSet<>();
objects.forEach(o -> {
DataSet dataSet = ((Section) o).getDataSet();
if (dataSet.getId() > 0) {
dataSets.add(dataSet);
}
});
dataSets.forEach(ds -> {
ds.increaseVersion();
sessionFactory.getCurrentSession().save(ds);
});
} else if (Option.class.isAssignableFrom(klass)) {
Set<OptionSet> optionSets = new HashSet<>();
objects.forEach(o -> {
Option option = (Option) o;
if (option.getOptionSet() != null && option.getId() > 0) {
optionSets.add(option.getOptionSet());
}
});
optionSets.forEach(os -> {
os.increaseVersion();
sessionFactory.getCurrentSession().save(os);
});
}
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class HibernateGenericStore method update.
@Override
public void update(T object, User user) {
String username = user != null ? user.getUsername() : "system-process";
if (IdentifiableObject.class.isInstance(object)) {
BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
identifiableObject.setAutoFields();
identifiableObject.setLastUpdatedBy(user);
if (identifiableObject.getUser() == null) {
identifiableObject.setUser(user);
}
}
if (!isUpdateAllowed(object, user)) {
AuditLogUtil.infoWrapper(log, username, object, AuditLogUtil.ACTION_UPDATE_DENIED);
throw new UpdateAccessDeniedException(object.toString());
}
AuditLogUtil.infoWrapper(log, username, object, AuditLogUtil.ACTION_UPDATE);
if (object != null) {
getSession().update(object);
}
if (MetadataObject.class.isInstance(object)) {
deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery((IdentifiableObject) object));
}
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class SharingController method getSharing.
// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public void getSharing(@RequestParam String type, @RequestParam String id, HttpServletResponse response) throws IOException, WebMessageException {
if (!aclService.isShareable(type)) {
throw new WebMessageException(WebMessageUtils.conflict("Type " + type + " is not supported."));
}
Class<? extends IdentifiableObject> klass = aclService.classForType(type);
IdentifiableObject object = manager.get(klass, id);
if (object == null) {
throw new WebMessageException(WebMessageUtils.notFound("Object of type " + type + " with ID " + id + " was not found."));
}
User user = currentUserService.getCurrentUser();
if (!aclService.canRead(user, object)) {
throw new AccessDeniedException("You do not have manage access to this object.");
}
Sharing sharing = new Sharing();
sharing.getMeta().setAllowPublicAccess(aclService.canMakePublic(user, object.getClass()));
sharing.getMeta().setAllowExternalAccess(aclService.canMakeExternal(user, object.getClass()));
sharing.getObject().setId(object.getUid());
sharing.getObject().setName(object.getDisplayName());
sharing.getObject().setDisplayName(object.getDisplayName());
sharing.getObject().setExternalAccess(object.getExternalAccess());
if (object.getPublicAccess() == null) {
String access;
if (aclService.canMakePublic(user, klass)) {
access = AccessStringHelper.newInstance().enable(AccessStringHelper.Permission.READ).enable(AccessStringHelper.Permission.WRITE).build();
} else {
access = AccessStringHelper.newInstance().build();
}
sharing.getObject().setPublicAccess(access);
} else {
sharing.getObject().setPublicAccess(object.getPublicAccess());
}
if (object.getUser() != null) {
sharing.getObject().getUser().setId(object.getUser().getUid());
sharing.getObject().getUser().setName(object.getUser().getDisplayName());
}
for (UserGroupAccess userGroupAccess : object.getUserGroupAccesses()) {
SharingUserGroupAccess sharingUserGroupAccess = new SharingUserGroupAccess();
sharingUserGroupAccess.setId(userGroupAccess.getUserGroup().getUid());
sharingUserGroupAccess.setName(userGroupAccess.getUserGroup().getDisplayName());
sharingUserGroupAccess.setDisplayName(userGroupAccess.getUserGroup().getDisplayName());
sharingUserGroupAccess.setAccess(userGroupAccess.getAccess());
sharing.getObject().getUserGroupAccesses().add(sharingUserGroupAccess);
}
for (UserAccess userAccess : object.getUserAccesses()) {
SharingUserAccess sharingUserAccess = new SharingUserAccess();
sharingUserAccess.setId(userAccess.getUser().getUid());
sharingUserAccess.setName(userAccess.getUser().getDisplayName());
sharingUserAccess.setDisplayName(userAccess.getUser().getDisplayName());
sharingUserAccess.setAccess(userAccess.getAccess());
sharing.getObject().getUserAccesses().add(sharingUserAccess);
}
sharing.getObject().getUserGroupAccesses().sort(SharingUserGroupAccessNameComparator.INSTANCE);
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
renderService.toJson(response.getOutputStream(), sharing);
}
Aggregations