Search in sources :

Example 1 with ProjectEvent

use of edu.stanford.bmir.protege.web.shared.event.ProjectEvent in project webprotege by protegeproject.

the class AbstractProjectChangeHandler method execute.

@Nonnull
@Override
public final R execute(@Nonnull A action, @Nonnull ExecutionContext executionContext) {
    EventTag tag = eventManager.getCurrentTag();
    ChangeListGenerator<T> changeListGenerator = getChangeListGenerator(action, executionContext);
    ChangeApplicationResult<T> result = applyChanges.applyChanges(executionContext.getUserId(), changeListGenerator);
    EventList<ProjectEvent<?>> eventList = eventManager.getEventsFromTag(tag);
    return createActionResult(result, action, executionContext, eventList);
}
Also used : EventTag(edu.stanford.bmir.protege.web.shared.event.EventTag) ProjectEvent(edu.stanford.bmir.protege.web.shared.event.ProjectEvent) Nonnull(javax.annotation.Nonnull)

Example 2 with ProjectEvent

use of edu.stanford.bmir.protege.web.shared.event.ProjectEvent in project webprotege by protegeproject.

the class AbstractUpdateFrameHandler method execute.

/**
 * Executes the specified action, against the specified project in the specified context.
 * @param action The action to be handled/executed
 * @param executionContext The {@link edu.stanford.bmir.protege.web.server.dispatch.ExecutionContext} that should be
 * used to provide details such as the
 * {@link edu.stanford.bmir.protege.web.shared.user.UserId} of the user who requested the action be executed.
 * @return The result of the execution to be returned to the client.
 */
@Nonnull
@Override
public Result execute(@Nonnull A action, @Nonnull ExecutionContext executionContext) {
    LabelledFrame<F> from = action.getFrom();
    LabelledFrame<F> to = action.getTo();
    final EventTag startTag = eventManager.getCurrentTag();
    if (from.equals(to)) {
        return createResponse(action.getTo(), eventManager.getEventsFromTag(startTag));
    }
    UserId userId = executionContext.getUserId();
    FrameTranslator<F, S> translator = createTranslator();
    final FrameChangeGenerator<F, S> changeGenerator = new FrameChangeGenerator<>(from.getFrame(), to.getFrame(), translator, rootOntology, changeDescriptionGeneratorFactory);
    ChangeDescriptionGenerator<S> generator = changeDescriptionGeneratorFactory.get("Edited " + from.getDisplayName());
    applyChanges.applyChanges(userId, changeGenerator);
    EventList<ProjectEvent<?>> events = eventManager.getEventsFromTag(startTag);
    return createResponse(action.getTo(), events);
}
Also used : UserId(edu.stanford.bmir.protege.web.shared.user.UserId) EventTag(edu.stanford.bmir.protege.web.shared.event.EventTag) ProjectEvent(edu.stanford.bmir.protege.web.shared.event.ProjectEvent) Nonnull(javax.annotation.Nonnull)

Example 3 with ProjectEvent

use of edu.stanford.bmir.protege.web.shared.event.ProjectEvent in project webprotege by protegeproject.

the class ChangeManager method applyChanges.

/**
 * Applies ontology changes to the ontologies contained within a project.
 *
 * @param userId                     The userId of the user applying the changes.  Not {@code null}.
 * @param changeListGenerator        A generator which creates a list of changes (based on the state of the project at
 *                                   the time of change application).  The idea behind passing in a change generator is that the list of changes to
 *                                   be applied can be created based on the state of the project immediately before they are applied.  This is
 *                                   necessary where the changes depend on the structure/state of the ontology.  This method guarantees that no third
 *                                   party
 *                                   ontology changes will take place between the {@link ChangeListGenerator#generateChanges(ChangeGenerationContext)}
 *                                   method being called and the changes being applied.
 * @return A {@link ChangeApplicationResult} that describes the changes which took place an any renaminings.
 * @throws NullPointerException      if any parameters are {@code null}.
 * @throws PermissionDeniedException if the user identified by {@code userId} does not have permssion to write to
 *                                   ontologies in this project.
 */
@Override
public <R> ChangeApplicationResult<R> applyChanges(final UserId userId, final ChangeListGenerator<R> changeListGenerator) throws PermissionDeniedException {
    // noinspection ResultOfMethodCallIgnored
    checkNotNull(userId);
    // noinspection ResultOfMethodCallIgnored
    checkNotNull(changeListGenerator);
    // Final check of whether the user can actually edit the project
    Subject subject = forUser(userId);
    ProjectResource projectResource = new ProjectResource(projectId);
    if (!accessManager.hasPermission(subject, projectResource, EDIT_ONTOLOGY.getActionId())) {
        throw new PermissionDeniedException("You do not have permission to edit this project", userInSessionFactory.getUserInSession(userId));
    }
    final List<OWLOntologyChange> appliedChanges;
    final ChangeApplicationResult<R> finalResult;
    // with true entity IRIs
    try {
        // Compute the changes that need to take place.  We don't allow any other writes here because the
        // generation of the changes may depend upon the state of the project
        changeProcesssingLock.lock();
        final ChangeGenerationContext context = new ChangeGenerationContext(userId);
        OntologyChangeList<R> gen = changeListGenerator.generateChanges(context);
        // We have our changes
        List<OWLOntologyChange> changes = gen.getChanges();
        // We coin fresh entities for places where tmp: is the scheme - the name for the entity comes from
        // the fragment
        final Map<IRI, IRI> iriRenameMap = new HashMap<>();
        final ChangeSetEntityCrudSession session = getEntityCrudKitHandler().createChangeSetSession();
        Set<OWLOntologyChange> changesToRename = new HashSet<>();
        List<OWLOntologyChange> freshEntityChanges = new ArrayList<>();
        for (OWLOntologyChange change : changes) {
            for (OWLEntity entity : change.getSignature()) {
                if (DataFactory.isFreshEntity(entity)) {
                    if (entity.isOWLClass()) {
                        if (!accessManager.hasPermission(subject, projectResource, CREATE_CLASS.getActionId())) {
                            throw new PermissionDeniedException("You do not have permission to create new classes", userInSessionFactory.getUserInSession(userId));
                        }
                    } else if (entity.isOWLObjectProperty() || entity.isOWLDataProperty() || entity.isOWLAnnotationProperty()) {
                        if (!accessManager.hasPermission(subject, projectResource, CREATE_PROPERTY.getActionId())) {
                            throw new PermissionDeniedException("You do not have permission to create new properties", userInSessionFactory.getUserInSession(userId));
                        }
                    } else if (entity.isOWLNamedIndividual()) {
                        if (!accessManager.hasPermission(subject, projectResource, CREATE_INDIVIDUAL.getActionId())) {
                            throw new PermissionDeniedException("You do not have permission to create new individuals", userInSessionFactory.getUserInSession(userId));
                        }
                    } else if (entity.isOWLDatatype()) {
                        if (!accessManager.hasPermission(subject, projectResource, CREATE_DATATYPE.getActionId())) {
                            throw new PermissionDeniedException("You do not have permission to create new datatypes", userInSessionFactory.getUserInSession(userId));
                        }
                    }
                    changesToRename.add(change);
                    IRI currentIRI = entity.getIRI();
                    if (!iriRenameMap.containsKey(currentIRI)) {
                        String shortName = DataFactory.getFreshEntityShortName(entity);
                        OWLEntityCreator<? extends OWLEntity> creator = getEntityCreator(session, userId, shortName, (EntityType<? extends OWLEntity>) entity.getEntityType());
                        freshEntityChanges.addAll(creator.getChanges());
                        IRI replacementIRI = creator.getEntity().getIRI();
                        iriRenameMap.put(currentIRI, replacementIRI);
                    }
                }
            }
        }
        List<OWLOntologyChange> allChangesIncludingRenames = new ArrayList<>();
        final OWLObjectDuplicator duplicator = new OWLObjectDuplicator(dataFactory, iriRenameMap);
        for (OWLOntologyChange change : changes) {
            if (changesToRename.contains(change)) {
                OWLOntologyChange replacementChange = getRenamedChange(change, duplicator);
                allChangesIncludingRenames.add(replacementChange);
            } else {
                allChangesIncludingRenames.add(change);
            }
        }
        allChangesIncludingRenames.addAll(freshEntityChanges);
        List<OWLOntologyChange> minimisedChanges = getMinimisedChanges(allChangesIncludingRenames);
        final EventTranslatorManager eventTranslatorManager = eventTranslatorManagerProvider.get();
        eventTranslatorManager.prepareForOntologyChanges(minimisedChanges);
        // Now we do the actual changing, so we lock the project here.  No writes or reads can take place whilst
        // we apply the changes
        final Optional<Revision> revision;
        try {
            projectChangeWriteLock.lock();
            ProjectOWLOntologyManager manager = ((ProjectOWLOntologyManager) rootOntology.getOWLOntologyManager());
            List<OWLOntologyChange> effectiveChanges = getEffectiveChanges(minimisedChanges);
            manager.getDelegate().applyChanges(effectiveChanges);
            appliedChanges = effectiveChanges;
            final RenameMap renameMap = new RenameMap(iriRenameMap);
            R renamedResult = getRenamedResult(changeListGenerator, gen.getResult(), renameMap);
            finalResult = new ChangeApplicationResult<>(renamedResult, appliedChanges, renameMap);
            if (!appliedChanges.isEmpty()) {
                Revision rev = logAndBroadcastAppliedChanges(userId, changeListGenerator, finalResult);
                revision = Optional.of(rev);
                projectDetailsRepository.setModified(projectId, rev.getTimestamp(), userId);
            } else {
                revision = Optional.empty();
            }
        } finally {
            // Release for reads
            projectChangeWriteLock.unlock();
        }
        if (revision.isPresent() && !(changeListGenerator instanceof SilentChangeListGenerator)) {
            List<ProjectEvent<?>> highLevelEvents = new ArrayList<>();
            Revision rev = revision.get();
            eventTranslatorManager.translateOntologyChanges(rev, finalResult, highLevelEvents);
            if (changeListGenerator instanceof HasHighLevelEvents) {
                highLevelEvents.addAll(((HasHighLevelEvents) changeListGenerator).getHighLevelEvents());
            }
            projectEventManager.postEvents(highLevelEvents);
            projectChangedWebhookInvoker.invoke(userId, rev.getRevisionNumber(), rev.getTimestamp());
        }
    } finally {
        changeProcesssingLock.unlock();
    }
    return finalResult;
}
Also used : OWLObjectDuplicator(org.semanticweb.owlapi.util.OWLObjectDuplicator) EventTranslatorManager(edu.stanford.bmir.protege.web.server.events.EventTranslatorManager) Subject(edu.stanford.bmir.protege.web.server.access.Subject) Revision(edu.stanford.bmir.protege.web.server.revision.Revision) ProjectResource(edu.stanford.bmir.protege.web.server.access.ProjectResource) PermissionDeniedException(edu.stanford.bmir.protege.web.shared.permissions.PermissionDeniedException) RenameMap(edu.stanford.bmir.protege.web.server.owlapi.RenameMap) ProjectEvent(edu.stanford.bmir.protege.web.shared.event.ProjectEvent)

Example 4 with ProjectEvent

use of edu.stanford.bmir.protege.web.shared.event.ProjectEvent in project webprotege by protegeproject.

the class TagsManager method setProjectTags.

/**
 * Sets the tags for the project that this manager is associated with.
 */
public void setProjectTags(@Nonnull Collection<TagData> newProjectTags) {
    checkNotNull(newProjectTags);
    Collection<Tag> currentProjectTags = getProjectTags();
    Set<OWLEntity> modifiedEntityTags = new HashSet<>();
    try {
        writeLock.lock();
        projectTags = null;
        Set<TagId> tagIds = newProjectTags.stream().map(TagData::getTagId).filter(Optional::isPresent).map(Optional::get).collect(toSet());
        // Remove current tags
        currentProjectTags.stream().map(Tag::getTagId).peek(tagId -> {
            if (!tagIds.contains(tagId)) {
                // Record modified entity tags
                entityTagsRepository.findByTagId(tagId).stream().map(EntityTags::getEntity).forEach(modifiedEntityTags::add);
                // Remove tag from entity
                entityTagsRepository.removeTag(projectId, tagId);
            }
        }).forEach(tagRepository::deleteTag);
        // Set fresh tags
        List<Tag> tags = newProjectTags.stream().map(tagData -> new Tag(tagData.getTagId().orElse(createTagId()), projectId, tagData.getLabel(), tagData.getDescription(), tagData.getColor(), tagData.getBackgroundColor())).peek(tag -> {
            // Find the modified entities for this tag
            entityTagsRepository.findByTagId(tag.getTagId()).stream().map(EntityTags::getEntity).forEach(modifiedEntityTags::add);
        }).collect(toList());
        tagRepository.saveTags(tags);
    } finally {
        writeLock.unlock();
    }
    Set<Tag> oldProjectTags = new HashSet<>(currentProjectTags);
    Set<Tag> projectTags = new HashSet<>(getProjectTags());
    if (!oldProjectTags.equals(projectTags)) {
        List<ProjectEvent<?>> events = new ArrayList<>();
        for (OWLEntity entity : modifiedEntityTags) {
            EntityTagsChangedEvent event = new EntityTagsChangedEvent(projectId, entity, getTags(entity));
            events.add(event);
        }
        events.add(new ProjectTagsChangedEvent(projectId, projectTags));
        eventBus.postEvents(events);
    }
}
Also used : java.util(java.util) ProjectEvent(edu.stanford.bmir.protege.web.shared.event.ProjectEvent) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TagId.createTagId(edu.stanford.bmir.protege.web.shared.tag.TagId.createTagId) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Multimap(com.google.common.collect.Multimap) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) HasPostEvents(edu.stanford.bmir.protege.web.server.events.HasPostEvents) edu.stanford.bmir.protege.web.shared.tag(edu.stanford.bmir.protege.web.shared.tag) Lock(java.util.concurrent.locks.Lock) HashMultimap(com.google.common.collect.HashMultimap) OWLEntity(org.semanticweb.owlapi.model.OWLEntity) ProjectId(edu.stanford.bmir.protege.web.shared.project.ProjectId) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ProjectSingleton(edu.stanford.bmir.protege.web.shared.inject.ProjectSingleton) OWLEntity(org.semanticweb.owlapi.model.OWLEntity) TagId.createTagId(edu.stanford.bmir.protege.web.shared.tag.TagId.createTagId) ProjectEvent(edu.stanford.bmir.protege.web.shared.event.ProjectEvent)

Example 5 with ProjectEvent

use of edu.stanford.bmir.protege.web.shared.event.ProjectEvent in project webprotege by protegeproject.

the class UpdateEntityTagsActionHandler method execute.

@Nonnull
@Override
public UpdateEntityTagsResult execute(@Nonnull UpdateEntityTagsAction action, @Nonnull ExecutionContext executionContext) {
    EventTag startTag = eventEventManager.getCurrentTag();
    tagsManager.updateTags(action.getEntity(), action.getFromTagIds(), action.getToTagIds());
    EventList<ProjectEvent<?>> events = eventEventManager.getEventsFromTag(startTag);
    return new UpdateEntityTagsResult(events);
}
Also used : UpdateEntityTagsResult(edu.stanford.bmir.protege.web.shared.tag.UpdateEntityTagsResult) EventTag(edu.stanford.bmir.protege.web.shared.event.EventTag) ProjectEvent(edu.stanford.bmir.protege.web.shared.event.ProjectEvent) Nonnull(javax.annotation.Nonnull)

Aggregations

ProjectEvent (edu.stanford.bmir.protege.web.shared.event.ProjectEvent)9 Nonnull (javax.annotation.Nonnull)8 EventTag (edu.stanford.bmir.protege.web.shared.event.EventTag)7 ProjectId (edu.stanford.bmir.protege.web.shared.project.ProjectId)3 UserId (edu.stanford.bmir.protege.web.shared.user.UserId)3 Nullable (javax.annotation.Nullable)3 Inject (javax.inject.Inject)3 AccessManager (edu.stanford.bmir.protege.web.server.access.AccessManager)2 AbstractProjectActionHandler (edu.stanford.bmir.protege.web.server.dispatch.AbstractProjectActionHandler)2 ExecutionContext (edu.stanford.bmir.protege.web.server.dispatch.ExecutionContext)2 EventManager (edu.stanford.bmir.protege.web.server.events.EventManager)2 BuiltInAction (edu.stanford.bmir.protege.web.shared.access.BuiltInAction)2 EventList (edu.stanford.bmir.protege.web.shared.event.EventList)2 edu.stanford.bmir.protege.web.shared.issues (edu.stanford.bmir.protege.web.shared.issues)2 Optional (java.util.Optional)2 OWLEntity (org.semanticweb.owlapi.model.OWLEntity)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 ProjectResource (edu.stanford.bmir.protege.web.server.access.ProjectResource)1