org.obiba.mica.network.search.EsDraftNetworkService.java Source code

Java tutorial

Introduction

Here is the source code for org.obiba.mica.network.search.EsDraftNetworkService.java

Source

/*
 * Copyright (c) 2018 OBiBa. All rights reserved.
 *
 * This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.obiba.mica.network.search;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.obiba.mica.network.domain.Network;
import org.obiba.mica.network.service.DraftNetworkService;
import org.obiba.mica.network.service.NetworkService;
import org.obiba.mica.search.AbstractIdentifiedDocumentService;
import org.obiba.mica.spi.search.Indexer;
import org.obiba.mica.spi.search.Searcher;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.IOException;
import java.util.Collection;
import java.util.stream.Collectors;

@Service
public class EsDraftNetworkService extends AbstractIdentifiedDocumentService<Network>
        implements DraftNetworkService {

    @Inject
    private ObjectMapper objectMapper;

    @Inject
    private NetworkService networkService;

    @Override
    public NetworkService getNetworkService() {
        return networkService;
    }

    @Override
    protected Network processHit(Searcher.DocumentResult res) throws IOException {
        return objectMapper.readValue(res.getSourceInputStream(), Network.class);
    }

    @Override
    protected String getIndexName() {
        return Indexer.DRAFT_NETWORK_INDEX;
    }

    @Override
    protected String getType() {
        return Indexer.NETWORK_TYPE;
    }

    @Nullable
    @Override
    protected Searcher.IdFilter getAccessibleIdFilter() {
        return new Searcher.IdFilter() {
            @Override
            public Collection<String> getValues() {
                return networkService.findAllIds().stream()
                        .filter(s -> subjectAclService.isPermitted("/draft/network", "VIEW", s))
                        .collect(Collectors.toList());
            }
        };
    }
}