Search in sources :

Example 1 with NetworkState

use of org.obiba.mica.network.domain.NetworkState in project mica2 by obiba.

the class NetworkService method removeRoles.

private void removeRoles(@NotNull Network network, Iterable<String> roles) {
    saveInternal(network, String.format("Removed roles: %s", Joiner.on(", ").join(roles)), false);
    NetworkState state = findStateById(network.getId());
    if (state.isPublished()) {
        processNetworkForPublishedNumberOfStudies(network);
        publishState(network.getId());
        eventBus.post(new NetworkPublishedEvent(network, getCurrentUsername(), PublishCascadingScope.NONE));
    }
}
Also used : NetworkPublishedEvent(org.obiba.mica.network.event.NetworkPublishedEvent) NetworkState(org.obiba.mica.network.domain.NetworkState)

Example 2 with NetworkState

use of org.obiba.mica.network.domain.NetworkState in project mica2 by obiba.

the class NetworkService method saveInternal.

@SuppressWarnings("OverlyLongMethod")
private void saveInternal(@NotNull Network network, String comment, boolean cascade) {
    Network saved = network;
    if (network.isNew()) {
        generateId(saved);
    } else {
        saved = networkRepository.findOne(network.getId());
        if (saved != null) {
            BeanUtils.copyProperties(network, saved, "id", "version", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate");
        } else {
            saved = network;
        }
    }
    if (saved.getLogo() != null && saved.getLogo().isJustUploaded()) {
        fileStoreService.save(saved.getLogo().getId());
        saved.getLogo().setJustUploaded(false);
    }
    ImmutableSet<String> invalidRoles = ImmutableSet.copyOf(Sets.difference(saved.membershipRoles(), Sets.newHashSet(micaConfigService.getConfig().getRoles())));
    for (String r : invalidRoles) {
        saved.removeRole(r);
    }
    NetworkState networkState = findEntityState(network, () -> {
        NetworkState defaultState = new NetworkState();
        defaultState.setName(network.getName());
        return defaultState;
    });
    if (!network.isNew())
        ensureGitRepository(networkState);
    networkState.incrementRevisionsAhead();
    networkStateRepository.save(networkState);
    saved.setLastModifiedDate(DateTime.now());
    if (cascade)
        networkRepository.saveWithReferences(saved);
    else
        networkRepository.save(saved);
    eventBus.post(new NetworkUpdatedEvent(saved));
    gitService.save(saved, comment);
}
Also used : Network(org.obiba.mica.network.domain.Network) NetworkUpdatedEvent(org.obiba.mica.network.event.NetworkUpdatedEvent) LocalizedString(org.obiba.mica.core.domain.LocalizedString) NetworkState(org.obiba.mica.network.domain.NetworkState)

Example 3 with NetworkState

use of org.obiba.mica.network.domain.NetworkState in project mica2 by obiba.

the class NetworkService method index.

/**
 * Index a specific {@link Network} without updating it.
 *
 * @param id
 * @throws NoSuchNetworkException
 */
public void index(@NotNull String id) throws NoSuchNetworkException {
    NetworkState networkState = getEntityState(id);
    Network network = findById(id);
    eventBus.post(new NetworkUpdatedEvent(network));
    processNetworkForPublishedNumberOfStudies(network);
    if (networkState.isPublished())
        eventBus.post(new NetworkPublishedEvent(network, getCurrentUsername()));
    else
        eventBus.post(new NetworkUnpublishedEvent(network));
}
Also used : NetworkUnpublishedEvent(org.obiba.mica.network.event.NetworkUnpublishedEvent) NetworkPublishedEvent(org.obiba.mica.network.event.NetworkPublishedEvent) Network(org.obiba.mica.network.domain.Network) NetworkUpdatedEvent(org.obiba.mica.network.event.NetworkUpdatedEvent) NetworkState(org.obiba.mica.network.domain.NetworkState)

Example 4 with NetworkState

use of org.obiba.mica.network.domain.NetworkState in project mica2 by obiba.

the class NetworkDtos method asDtoBuilder.

@NotNull
Mica.NetworkDto.Builder asDtoBuilder(@NotNull Network network, boolean asDraft) {
    Mica.NetworkDto.Builder builder = Mica.NetworkDto.newBuilder();
    if (network.hasModel())
        builder.setContent(JSONUtils.toJSON(network.getModel()));
    // 
    builder.setId(network.getId()).addAllName(// 
    localizedStringDtos.asDto(network.getName())).addAllDescription(// 
    localizedStringDtos.asDto(network.getDescription())).addAllAcronym(localizedStringDtos.asDto(network.getAcronym()));
    Mica.PermissionsDto permissionsDto = permissionsDtos.asDto(network);
    NetworkState networkState = networkService.getEntityState(network.getId());
    builder.setPublished(networkState.isPublished());
    if (asDraft) {
        // 
        builder.setTimestamps(TimestampsDtos.asDto(network)).setPublished(// 
        networkState.isPublished()).setExtension(Mica.EntityStateDto.state, entityStateDtos.asDto(networkState).setPermissions(permissionsDto).build());
    }
    builder.setPermissions(permissionsDto);
    List<String> roles = micaConfigService.getConfig().getRoles();
    if (network.getMemberships() != null) {
        List<Mica.MembershipsDto> memberships = network.getMemberships().entrySet().stream().filter(e -> roles.contains(e.getKey())).map(e -> Mica.MembershipsDto.newBuilder().setRole(e.getKey()).addAllMembers(e.getValue().stream().map(m -> personDtos.asDto(m.getPerson(), asDraft)).collect(toList())).build()).collect(toList());
        builder.addAllMemberships(memberships);
    }
    List<BaseStudy> publishedStudies = publishedStudyService.findByIds(network.getStudyIds());
    Set<String> publishedStudyIds = publishedStudies.stream().map(AbstractGitPersistable::getId).collect(Collectors.toSet());
    Sets.SetView<String> unpublishedStudyIds = Sets.difference(ImmutableSet.copyOf(network.getStudyIds().stream().filter(sId -> asDraft && subjectAclService.isPermitted("/draft/individual-study", "VIEW", sId) || subjectAclService.isAccessible("/individual-study", sId)).collect(toList())), publishedStudyIds);
    if (!publishedStudies.isEmpty()) {
        Map<String, Long> datasetVariableCounts = asDraft ? null : datasetVariableService.getCountByStudyIds(Lists.newArrayList(publishedStudyIds));
        publishedStudies.forEach(study -> {
            builder.addStudyIds(study.getId());
            builder.addStudySummaries(studySummaryDtos.asDtoBuilder(study, true, datasetVariableCounts == null ? 0 : datasetVariableCounts.get(study.getId())));
        });
    }
    unpublishedStudyIds.forEach(studyId -> {
        try {
            builder.addStudySummaries(studySummaryDtos.asDto(studyId));
            builder.addStudyIds(studyId);
        } catch (NoSuchEntityException e) {
            log.warn("Study not found in network {}: {}", network.getId(), studyId);
        // ignore
        }
    });
    if (network.getLogo() != null) {
        builder.setLogo(attachmentDtos.asDto(network.getLogo()));
    }
    network.getNetworkIds().stream().filter(nId -> asDraft && subjectAclService.isPermitted("/draft/network", "VIEW", nId) || subjectAclService.isAccessible("/network", nId)).forEach(nId -> {
        try {
            builder.addNetworkSummaries(networkSummaryDtos.asDtoBuilder(nId, asDraft));
            builder.addNetworkIds(nId);
        } catch (NoSuchEntityException e) {
            log.warn("Network not found in network {}: {}", network.getId(), nId);
        // ignore
        }
    });
    return builder;
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) Membership(org.obiba.mica.core.domain.Membership) HashMap(java.util.HashMap) AbstractGitPersistable(org.obiba.mica.core.domain.AbstractGitPersistable) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) Map(java.util.Map) JSONUtils(org.obiba.mica.JSONUtils) Network(org.obiba.mica.network.domain.Network) Lists(jersey.repackaged.com.google.common.collect.Lists) PublishedDatasetVariableService(org.obiba.mica.study.service.PublishedDatasetVariableService) ImmutableSet(com.google.common.collect.ImmutableSet) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Logger(org.slf4j.Logger) HarmonizationStudy(org.obiba.mica.study.domain.HarmonizationStudy) BaseStudy(org.obiba.mica.study.domain.BaseStudy) Set(java.util.Set) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) PublishedStudyService(org.obiba.mica.study.service.PublishedStudyService) List(java.util.List) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) Study(org.obiba.mica.study.domain.Study) MicaConfigService(org.obiba.mica.micaConfig.service.MicaConfigService) NetworkState(org.obiba.mica.network.domain.NetworkState) NetworkService(org.obiba.mica.network.service.NetworkService) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) Sets(com.google.common.collect.Sets) NetworkState(org.obiba.mica.network.domain.NetworkState) BaseStudy(org.obiba.mica.study.domain.BaseStudy) NotNull(javax.validation.constraints.NotNull)

Example 5 with NetworkState

use of org.obiba.mica.network.domain.NetworkState in project mica2 by obiba.

the class NetworkSummaryDtos method asDto.

@NotNull
public Mica.NetworkSummaryDto asDto(@NotNull Network network, boolean asDraft) {
    Mica.NetworkSummaryDto.Builder builder = Mica.NetworkSummaryDto.newBuilder();
    NetworkState networkState = networkService.getEntityState(network.getId());
    builder.setId(network.getId()).addAllAcronym(localizedStringDtos.asDto(network.getAcronym())).addAllName(localizedStringDtos.asDto(network.getName())).setPublished(networkState.isPublished());
    Mica.PermissionsDto permissionsDto = permissionsDtos.asDto(network);
    if (asDraft) {
        // 
        builder.setTimestamps(TimestampsDtos.asDto(network)).setPublished(// 
        networkState.isPublished()).setExtension(Mica.EntityStateDto.networkSummaryState, entityStateDtos.asDto(networkState).setPermissions(permissionsDto).build());
    }
    builder.setPermissions(permissionsDto);
    network.getStudyIds().stream().filter(sId -> asDraft && subjectAclService.isPermitted("/draft/individual-study", "VIEW", sId) || subjectAclService.isAccessible("/individual-study", sId)).forEach(sId -> {
        try {
            builder.addStudyIds(sId);
        } catch (NoSuchEntityException e) {
            log.warn("Study not found in network {}: {}", network.getId(), sId);
        // ignore
        }
    });
    network.getNetworkIds().stream().filter(nId -> asDraft && subjectAclService.isPermitted("/draft/network", "VIEW", nId) || subjectAclService.isAccessible("/network", nId)).forEach(nId -> {
        try {
            builder.addNetworkIds(nId);
        } catch (NoSuchEntityException e) {
            log.warn("Network not found in network {}: {}", network.getId(), nId);
        // ignore
        }
    });
    return builder.build();
}
Also used : Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Logger(org.slf4j.Logger) NetworkState(org.obiba.mica.network.domain.NetworkState) PublishedNetworkService(org.obiba.mica.network.service.PublishedNetworkService) LoggerFactory(org.slf4j.LoggerFactory) Network(org.obiba.mica.network.domain.Network) NetworkService(org.obiba.mica.network.service.NetworkService) NotNull(javax.validation.constraints.NotNull) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) NetworkState(org.obiba.mica.network.domain.NetworkState) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) NotNull(javax.validation.constraints.NotNull)

Aggregations

NetworkState (org.obiba.mica.network.domain.NetworkState)5 Network (org.obiba.mica.network.domain.Network)4 Inject (javax.inject.Inject)2 NotNull (javax.validation.constraints.NotNull)2 NoSuchEntityException (org.obiba.mica.NoSuchEntityException)2 NetworkPublishedEvent (org.obiba.mica.network.event.NetworkPublishedEvent)2 NetworkUpdatedEvent (org.obiba.mica.network.event.NetworkUpdatedEvent)2 NetworkService (org.obiba.mica.network.service.NetworkService)2 SubjectAclService (org.obiba.mica.security.service.SubjectAclService)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Component (org.springframework.stereotype.Component)2 Strings (com.google.common.base.Strings)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1