Search in sources :

Example 1 with DeletedObjectQuery

use of org.hisp.dhis.deletedobject.DeletedObjectQuery in project dhis2-core by dhis2.

the class HibernateGenericStore method save.

@Override
public void save(T object, User user, boolean clearSharing) {
    String username = user != null ? user.getUsername() : "system-process";
    if (IdentifiableObject.class.isAssignableFrom(object.getClass())) {
        BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
        identifiableObject.setAutoFields();
        identifiableObject.setLastUpdatedBy(user);
        if (clearSharing) {
            identifiableObject.setPublicAccess(AccessStringHelper.DEFAULT);
            if (identifiableObject.getUserGroupAccesses() != null) {
                identifiableObject.getUserGroupAccesses().clear();
            }
            if (identifiableObject.getUserAccesses() != null) {
                identifiableObject.getUserAccesses().clear();
            }
        }
        if (identifiableObject.getUser() == null) {
            identifiableObject.setUser(user);
        }
    }
    if (user != null && aclService.isShareable(clazz)) {
        BaseIdentifiableObject identifiableObject = (BaseIdentifiableObject) object;
        if (clearSharing) {
            if (aclService.canMakePublic(user, identifiableObject.getClass())) {
                if (aclService.defaultPublic(identifiableObject.getClass())) {
                    identifiableObject.setPublicAccess(AccessStringHelper.READ_WRITE);
                }
            } else if (aclService.canMakePrivate(user, identifiableObject.getClass())) {
                identifiableObject.setPublicAccess(AccessStringHelper.newInstance().build());
            }
        }
        if (!checkPublicAccess(user, identifiableObject)) {
            AuditLogUtil.infoWrapper(log, username, object, AuditLogUtil.ACTION_CREATE_DENIED);
            throw new CreateAccessDeniedException(object.toString());
        }
    }
    AuditLogUtil.infoWrapper(log, username, object, AuditLogUtil.ACTION_CREATE);
    getSession().save(object);
    if (MetadataObject.class.isInstance(object)) {
        deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery((IdentifiableObject) object));
    }
}
Also used : BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject)

Example 2 with DeletedObjectQuery

use of org.hisp.dhis.deletedobject.DeletedObjectQuery in project dhis2-core by dhis2.

the class HibernateDeletedObjectStore method getByKlass.

@Override
public List<DeletedObject> getByKlass(String klass) {
    DeletedObjectQuery query = new DeletedObjectQuery();
    query.getKlass().add(klass);
    return query(query);
}
Also used : DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery)

Example 3 with DeletedObjectQuery

use of org.hisp.dhis.deletedobject.DeletedObjectQuery 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 4 with DeletedObjectQuery

use of org.hisp.dhis.deletedobject.DeletedObjectQuery in project dhis2-core by dhis2.

the class DefaultObjectBundleService method handleCreates.

//-----------------------------------------------------------------------------------
// Utility Methods
//-----------------------------------------------------------------------------------
private TypeReport handleCreates(Session session, Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
    TypeReport typeReport = new TypeReport(klass);
    if (objects.isEmpty()) {
        return typeReport;
    }
    String message = "(" + bundle.getUsername() + ") Creating " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
    log.info(message);
    if (bundle.hasTaskId()) {
        notifier.notify(bundle.getTaskId(), message);
    }
    objects.forEach(object -> objectBundleHooks.forEach(hook -> hook.preCreate(object, bundle)));
    for (int idx = 0; idx < objects.size(); idx++) {
        IdentifiableObject object = objects.get(idx);
        if (Preheat.isDefault(object))
            continue;
        ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
        objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
        typeReport.addObjectReport(objectReport);
        preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
        session.save(object);
        if (MetadataObject.class.isInstance(object)) {
            deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery(object));
        }
        bundle.getPreheat().replace(bundle.getPreheatIdentifier(), object);
        objectBundleHooks.forEach(hook -> hook.postCreate(object, bundle));
        if (log.isDebugEnabled()) {
            String msg = "(" + bundle.getUsername() + ") Created object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(object) + "'";
            log.debug(msg);
        }
        if (FlushMode.OBJECT == bundle.getFlushMode())
            session.flush();
    }
    return typeReport;
}
Also used : HibernateCacheManager(org.hisp.dhis.cache.HibernateCacheManager) PreheatService(org.hisp.dhis.preheat.PreheatService) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) MergeService(org.hisp.dhis.schema.MergeService) Session(org.hibernate.Session) Preheat(org.hisp.dhis.preheat.Preheat) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) MergeParams(org.hisp.dhis.schema.MergeParams) FlushMode(org.hisp.dhis.dxf2.metadata.FlushMode) TypeReport(org.hisp.dhis.feedback.TypeReport) ArrayList(java.util.ArrayList) Notifier(org.hisp.dhis.system.notification.Notifier) ObjectBundleCommitReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleCommitReport) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) Service(org.springframework.stereotype.Service) Map(java.util.Map) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DeletedObjectService(org.hisp.dhis.deletedobject.DeletedObjectService) SessionFactory(org.hibernate.SessionFactory) SchemaService(org.hisp.dhis.schema.SchemaService) MergeMode(org.hisp.dhis.common.MergeMode) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) PreheatParams(org.hisp.dhis.preheat.PreheatParams) List(java.util.List) CurrentUserService(org.hisp.dhis.user.CurrentUserService) MetadataObject(org.hisp.dhis.common.MetadataObject) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Transactional(org.springframework.transaction.annotation.Transactional) TypeReport(org.hisp.dhis.feedback.TypeReport) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 5 with DeletedObjectQuery

use of org.hisp.dhis.deletedobject.DeletedObjectQuery in project dhis2-core by dhis2.

the class DefaultObjectBundleService method handleUpdates.

private TypeReport handleUpdates(Session session, Class<? extends IdentifiableObject> klass, List<IdentifiableObject> objects, ObjectBundle bundle) {
    TypeReport typeReport = new TypeReport(klass);
    if (objects.isEmpty()) {
        return typeReport;
    }
    String message = "(" + bundle.getUsername() + ") Updating " + objects.size() + " object(s) of type " + objects.get(0).getClass().getSimpleName();
    log.info(message);
    if (bundle.hasTaskId()) {
        notifier.notify(bundle.getTaskId(), message);
    }
    objects.forEach(object -> {
        IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
        objectBundleHooks.forEach(hook -> hook.preUpdate(object, persistedObject, bundle));
    });
    for (int idx = 0; idx < objects.size(); idx++) {
        IdentifiableObject object = objects.get(idx);
        IdentifiableObject persistedObject = bundle.getPreheat().get(bundle.getPreheatIdentifier(), object);
        if (Preheat.isDefault(object))
            continue;
        ObjectReport objectReport = new ObjectReport(klass, idx, object.getUid());
        objectReport.setDisplayName(IdentifiableObjectUtils.getDisplayName(object));
        typeReport.addObjectReport(objectReport);
        preheatService.connectReferences(object, bundle.getPreheat(), bundle.getPreheatIdentifier());
        if (bundle.getMergeMode() != MergeMode.NONE) {
            mergeService.merge(new MergeParams<>(object, persistedObject).setMergeMode(bundle.getMergeMode()).setSkipSharing(bundle.isSkipSharing()));
        }
        session.update(persistedObject);
        if (MetadataObject.class.isInstance(object)) {
            deletedObjectService.deleteDeletedObjects(new DeletedObjectQuery(object));
        }
        objectBundleHooks.forEach(hook -> hook.postUpdate(persistedObject, bundle));
        bundle.getPreheat().replace(bundle.getPreheatIdentifier(), persistedObject);
        if (log.isDebugEnabled()) {
            String msg = "(" + bundle.getUsername() + ") Updated object '" + bundle.getPreheatIdentifier().getIdentifiersWithName(persistedObject) + "'";
            log.debug(msg);
        }
        if (FlushMode.OBJECT == bundle.getFlushMode())
            session.flush();
    }
    return typeReport;
}
Also used : TypeReport(org.hisp.dhis.feedback.TypeReport) DeletedObjectQuery(org.hisp.dhis.deletedobject.DeletedObjectQuery) ObjectReport(org.hisp.dhis.feedback.ObjectReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

DeletedObjectQuery (org.hisp.dhis.deletedobject.DeletedObjectQuery)5 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)2 ObjectReport (org.hisp.dhis.feedback.ObjectReport)2 TypeReport (org.hisp.dhis.feedback.TypeReport)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 HibernateCacheManager (org.hisp.dhis.cache.HibernateCacheManager)1 IdentifiableObjectManager (org.hisp.dhis.common.IdentifiableObjectManager)1 IdentifiableObjectUtils (org.hisp.dhis.common.IdentifiableObjectUtils)1 MergeMode (org.hisp.dhis.common.MergeMode)1 MetadataObject (org.hisp.dhis.common.MetadataObject)1 DbmsManager (org.hisp.dhis.dbms.DbmsManager)1 DeletedObjectService (org.hisp.dhis.deletedobject.DeletedObjectService)1