Search in sources :

Example 1 with GroupSyncFinishedWithErrors

use of cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncFinishedWithErrors in project perun by CESNET.

the class GroupsManagerBlImpl method saveInformationAboutGroupSynchronization.

private void saveInformationAboutGroupSynchronization(PerunSession sess, Group group, long startTime, boolean failedDueToException, String exceptionMessage) throws AttributeNotExistsException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException, WrongAttributeValueException {
    // get current timestamp of this synchronization
    Date currentTimestamp = new Date();
    String originalExceptionMessage = exceptionMessage;
    // If session is null, throw an exception
    if (sess == null) {
        throw new InternalErrorException("Session is null when trying to save information about synchronization. Group: " + group + ", timestamp: " + currentTimestamp + ",message: " + exceptionMessage);
    }
    // If group is null, throw an exception
    if (group == null) {
        throw new InternalErrorException("Object group is null when trying to save information about synchronization. Timestamp: " + currentTimestamp + ", message: " + exceptionMessage);
    }
    // if exceptionMessage is empty, use "Empty message" instead
    if (exceptionMessage != null && exceptionMessage.isEmpty()) {
        exceptionMessage = "Empty message.";
    // else trim the message on 1000 characters if not null
    } else if (exceptionMessage != null && exceptionMessage.length() > 1000) {
        exceptionMessage = exceptionMessage.substring(0, 1000) + " ... message is too long, other info is in perun log file. If needed, please ask perun administrators.";
    }
    // Set correct format of currentTimestamp
    String correctTimestampString = BeansUtils.getDateFormatter().format(currentTimestamp);
    // Get both attribute definition lastSynchroTimestamp and lastSynchroState
    // Get definitions and values, set values
    Attribute lastSynchronizationTimestamp = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":lastSynchronizationTimestamp"));
    Attribute lastSynchronizationState = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":lastSynchronizationState"));
    lastSynchronizationTimestamp.setValue(correctTimestampString);
    // if exception is null, set null to value => remove attribute instead of setting in method setAttributes
    lastSynchronizationState.setValue(exceptionMessage);
    // attributes to set
    List<Attribute> attrsToSet = new ArrayList<>();
    // Set lastSuccessSynchronizationTimestamp if this one is success
    if (exceptionMessage == null) {
        String attrName = AttributesManager.NS_GROUP_ATTR_DEF + ":lastSuccessSynchronizationTimestamp";
        try {
            Attribute lastSuccessSynchronizationTimestamp = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, attrName));
            lastSuccessSynchronizationTimestamp.setValue(correctTimestampString);
            attrsToSet.add(lastSuccessSynchronizationTimestamp);
        } catch (AttributeNotExistsException ex) {
            log.error("Can't save lastSuccessSynchronizationTimestamp, because there is missing attribute with name {}", attrName);
        }
        // Make string from start of synchronization in correct format
        String startOfSyncString = BeansUtils.getDateFormatter().format(new Date(startTime));
        try {
            // Create attribute with start of last success synchronization timestamp
            Attribute startOfLastSuccessSynchronization = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, GroupsManager.GROUP_START_OF_LAST_SUCCESSFUL_SYNC_ATTRNAME));
            startOfLastSuccessSynchronization.setValue(startOfSyncString);
            attrsToSet.add(startOfLastSuccessSynchronization);
        } catch (AttributeNotExistsException ex) {
            log.error("Can't save startOfLastSuccessfulSynchronization, because there is missing attribute with name {}", GroupsManager.GROUP_START_OF_LAST_SUCCESSFUL_SYNC_ATTRNAME);
        }
    } else {
        // Log info about synchronization problems to audit log and to the perun system log
        if (failedDueToException) {
            getPerunBl().getAuditer().log(sess, new GroupSyncFailed(group));
            log.debug("{} synchronization failed because of {}", group, originalExceptionMessage);
        } else {
            getPerunBl().getAuditer().log(sess, new GroupSyncFinishedWithErrors(group));
            log.debug("{} synchronization finished with errors: {}", group, originalExceptionMessage);
        }
    }
    // set lastSynchronizationState and lastSynchronizationTimestamp
    attrsToSet.add(lastSynchronizationState);
    attrsToSet.add(lastSynchronizationTimestamp);
    ((PerunBl) sess.getPerun()).getAttributesManagerBl().setAttributes(sess, group, attrsToSet);
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) GroupSyncFinishedWithErrors(cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncFinishedWithErrors) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) GroupSyncFailed(cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncFailed) Date(java.util.Date) LocalDate(java.time.LocalDate)

Aggregations

GroupSyncFailed (cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncFailed)1 GroupSyncFinishedWithErrors (cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncFinishedWithErrors)1 Attribute (cz.metacentrum.perun.core.api.Attribute)1 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1