Search in sources :

Example 61 with IdentifiableObject

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);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Map(java.util.Map) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Session(org.hibernate.Session)

Example 62 with IdentifiableObject

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);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) UserCredentials(org.hisp.dhis.user.UserCredentials) Map(java.util.Map) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 63 with IdentifiableObject

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);
        });
    }
}
Also used : HashSet(java.util.HashSet) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Option(org.hisp.dhis.option.Option) List(java.util.List) ObjectBundle(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle) OptionSet(org.hisp.dhis.option.OptionSet) VersionedObject(org.hisp.dhis.common.VersionedObject) DataSet(org.hisp.dhis.dataset.DataSet) Set(java.util.Set) Section(org.hisp.dhis.dataset.Section) HashSet(java.util.HashSet) OptionSet(org.hisp.dhis.option.OptionSet) DataSet(org.hisp.dhis.dataset.DataSet) Set(java.util.Set) DataSet(org.hisp.dhis.dataset.DataSet) Option(org.hisp.dhis.option.Option) Section(org.hisp.dhis.dataset.Section) HashSet(java.util.HashSet)

Example 64 with IdentifiableObject

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));
    }
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject)

Example 65 with IdentifiableObject

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);
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Sharing(org.hisp.dhis.webapi.webdomain.sharing.Sharing) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) UserAccess(org.hisp.dhis.user.UserAccess) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) SharingUserAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserAccess) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) SharingUserGroupAccess(org.hisp.dhis.webapi.webdomain.sharing.SharingUserGroupAccess) UserGroupAccess(org.hisp.dhis.user.UserGroupAccess) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)124 List (java.util.List)76 Test (org.junit.Test)67 DhisSpringTest (org.hisp.dhis.DhisSpringTest)64 ClassPathResource (org.springframework.core.io.ClassPathResource)54 DataElement (org.hisp.dhis.dataelement.DataElement)44 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)39 User (org.hisp.dhis.user.User)37 DataSet (org.hisp.dhis.dataset.DataSet)24 ArrayList (java.util.ArrayList)22 ObjectReport (org.hisp.dhis.feedback.ObjectReport)22 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)22 Schema (org.hisp.dhis.schema.Schema)19 HashMap (java.util.HashMap)18 TypeReport (org.hisp.dhis.feedback.TypeReport)18 Set (java.util.Set)15 ErrorReport (org.hisp.dhis.feedback.ErrorReport)15 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)15 Map (java.util.Map)14 Property (org.hisp.dhis.schema.Property)13