Search in sources :

Example 6 with AppEvent

use of org.eclipse.vorto.repository.core.events.AppEvent in project vorto by eclipse.

the class ModelRepository method save.

@Override
public ModelInfo save(final ModelResource modelInfo, IUserContext userContext) {
    return doInSession(jcrSession -> {
        org.modeshape.jcr.api.Session session = (org.modeshape.jcr.api.Session) jcrSession;
        LOGGER.info("Saving " + modelInfo.toString() + " as " + modelInfo.getFileName() + " in Workspace: " + session.getWorkspace().getName());
        try {
            Node folderNode = createNodeForModelId(session, modelInfo.getId());
            folderNode.addMixin(MIX_REFERENCEABLE);
            folderNode.addMixin(VORTO_META);
            folderNode.addMixin(MIX_LAST_MODIFIED);
            NodeIterator nodeIt = folderNode.getNodes(FILE_NODES);
            if (!nodeIt.hasNext()) {
                // new node
                Node fileNode = folderNode.addNode(modelInfo.getFileName(), NT_FILE);
                fileNode.addMixin(VORTO_META);
                fileNode.setProperty(VORTO_AUTHOR, userContext.getUsername());
                fileNode.setProperty(VORTO_VISIBILITY, VISIBILITY_PRIVATE);
                fileNode.addMixin(MODE_ACCESS_CONTROLLABLE);
                fileNode.addMixin(MIX_LAST_MODIFIED);
                Node contentNode = fileNode.addNode(JCR_CONTENT, NT_RESOURCE);
                Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(modelInfo.toDSL()));
                Property input = contentNode.setProperty(JCR_DATA, binary);
                boolean success = session.sequence("Vorto Sequencer", input, fileNode);
                if (!success) {
                    throw new FatalModelRepositoryException("Problem indexing new node for search" + modelInfo.getId(), null);
                }
            } else {
                // node already exists, so just update it.
                Node fileNode = nodeIt.nextNode();
                fileNode.addMixin(VORTO_META);
                fileNode.addMixin(MIX_LAST_MODIFIED);
                Node contentNode = fileNode.getNode(JCR_CONTENT);
                Binary binary = session.getValueFactory().createBinary(new ByteArrayInputStream(modelInfo.toDSL()));
                Property input = contentNode.setProperty(JCR_DATA, binary);
                boolean success = session.sequence("Vorto Sequencer", input, fileNode);
                if (!success) {
                    throw new FatalModelRepositoryException("Problem indexing new node for search" + modelInfo.getId(), null);
                }
            }
            session.save();
            LOGGER.info("Model was saved successfully");
            ModelInfo createdModel = getById(modelInfo.getId());
            eventPublisher.publishEvent(new AppEvent(this, createdModel, userContext, EventType.MODEL_CREATED));
            return createdModel;
        } catch (Exception e) {
            LOGGER.error("Error checking in model", e);
            throw new FatalModelRepositoryException("Problem saving model " + modelInfo.getId(), e);
        }
    });
}
Also used : NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ValidationException(org.eclipse.vorto.repository.core.impl.validation.ValidationException) NewNamespacesNotSupersetException(org.eclipse.vorto.repository.tenant.NewNamespacesNotSupersetException) AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) javax.jcr(javax.jcr)

Example 7 with AppEvent

use of org.eclipse.vorto.repository.core.events.AppEvent in project vorto by eclipse.

the class ModelRepository method updatePropertyInElevatedSession.

@Override
public ModelId updatePropertyInElevatedSession(ModelId modelId, Map<String, String> properties, IUserContext context) {
    return doInElevatedSession(session -> {
        try {
            Node folderNode = createNodeForModelId(session, modelId);
            Node fileNode = folderNode.getNodes(FILE_NODES).hasNext() ? folderNode.getNodes(FILE_NODES).nextNode() : null;
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                fileNode.setProperty(entry.getKey(), entry.getValue());
            }
            fileNode.addMixin(MIX_LAST_MODIFIED);
            session.save();
            eventPublisher.publishEvent(new AppEvent(this, getBasicInfoInElevatedSession(modelId, context), null, EventType.MODEL_UPDATED));
            return modelId;
        } catch (AccessDeniedException e) {
            throw new NotAuthorizedException(modelId, e);
        }
    }, context, privilegeService);
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) NotAuthorizedException(org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)

Example 8 with AppEvent

use of org.eclipse.vorto.repository.core.events.AppEvent in project vorto by eclipse.

the class UserService method delete.

@Transactional
public void delete(final String userId) {
    User userToDelete = cache.withUser(userId).getUser();
    if (userToDelete != null) {
        eventPublisher.publishEvent(new AppEvent(this, userId, EventType.USER_DELETED));
        userRepository.delete(userToDelete);
        if (userToDelete.hasEmailAddress()) {
            notificationService.sendNotification(new DeleteAccountMessage(userToDelete));
        }
    }
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) User(org.eclipse.vorto.repository.domain.User) DeleteAccountMessage(org.eclipse.vorto.repository.notification.message.DeleteAccountMessage) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with AppEvent

use of org.eclipse.vorto.repository.core.events.AppEvent in project vorto by eclipse.

the class UserService method createOrUpdateTechnicalUser.

/**
 * Validates and persists the given {@link User} as technical user.<br/>
 * This functionality is available to all users regardless of their privileges. <br/>
 * Failures in authorization may occur in a broader context, as technical users are created
 * and specifically associated to a {@link org.eclipse.vorto.repository.domain.Namespace}, so the
 * user performing the operation must have the right authorities to do so in context.
 *
 * @param technicalUser
 * @return
 * @throws InvalidUserException
 */
public User createOrUpdateTechnicalUser(User actor, User technicalUser) throws InvalidUserException {
    // boilerplate null validation
    ServiceValidationUtil.validateNulls(technicalUser);
    // validates technical user
    userUtil.validateNewUser(technicalUser);
    // sets createdby for tech user
    technicalUser.setCreatedBy(actor.getId());
    // save the technical user
    User result = userRepository.save(technicalUser);
    if (result != null) {
        eventPublisher.publishEvent(new AppEvent(this, technicalUser.getUsername(), EventType.USER_ADDED));
    }
    return result;
}
Also used : AppEvent(org.eclipse.vorto.repository.core.events.AppEvent) User(org.eclipse.vorto.repository.domain.User)

Aggregations

AppEvent (org.eclipse.vorto.repository.core.events.AppEvent)9 NotAuthorizedException (org.eclipse.vorto.repository.web.core.exceptions.NotAuthorizedException)5 Namespace (org.eclipse.vorto.repository.domain.Namespace)3 Transactional (org.springframework.transaction.annotation.Transactional)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 javax.jcr (javax.jcr)2 ModelIdHelper (org.eclipse.vorto.repository.core.impl.utils.ModelIdHelper)2 ValidationException (org.eclipse.vorto.repository.core.impl.validation.ValidationException)2 User (org.eclipse.vorto.repository.domain.User)2 DeleteAccountMessage (org.eclipse.vorto.repository.notification.message.DeleteAccountMessage)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 InputStream (java.io.InputStream)1 java.util (java.util)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 javax.jcr.query (javax.jcr.query)1 IOUtils (org.apache.commons.io.IOUtils)1