/*
 * Copyright 2017 Crown Copyright
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package stroom.meta.impl;

import stroom.event.logging.rs.api.AutoLogged;
import stroom.event.logging.rs.api.AutoLogged.OperationType;
import stroom.meta.api.MetaService;
import stroom.meta.shared.FindMetaCriteria;
import stroom.meta.shared.MetaResource;
import stroom.meta.shared.MetaRow;
import stroom.meta.shared.SelectionSummary;
import stroom.meta.shared.UpdateStatusRequest;
import stroom.util.shared.ResultPage;

import java.util.List;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.inject.Provider;

@AutoLogged
class MetaResourceImpl implements MetaResource {

    private final Provider<MetaService> metaServiceProvider;

    @Inject
    MetaResourceImpl(Provider<MetaService> metaServiceProvider) {
        this.metaServiceProvider = metaServiceProvider;
    }

    @Override
    public Integer updateStatus(final UpdateStatusRequest request) {
        return metaServiceProvider.get().updateStatus(
                request.getCriteria(),
                request.getCurrentStatus(),
                request.getNewStatus());
    }

    @Override
    public ResultPage<MetaRow> findMetaRow(final FindMetaCriteria criteria) {
        return metaServiceProvider.get().findDecoratedRows(criteria);
    }

    @AutoLogged(OperationType.SEARCH)
    @Override
    public SelectionSummary getSelectionSummary(final FindMetaCriteria criteria) {
        return metaServiceProvider.get().getSelectionSummary(criteria);
    }

    @AutoLogged(OperationType.SEARCH)
    @Override
    public SelectionSummary getReprocessSelectionSummary(final FindMetaCriteria criteria) {
        return metaServiceProvider.get().getReprocessSelectionSummary(criteria);
    }

    @Override
    public List<String> getTypes() {
        return metaServiceProvider
                .get()
                .getTypes()
                .stream()
                .sorted()
                .collect(Collectors.toList());
    }
}

@@ -620,16 +620,131 @@
 .impl;%0A%0A
+import stroom.event.logging.api.StroomEventLoggingService;%0Aimport stroom.event.logging.api.StroomEventLoggingUtil;%0A
 import s
@@ -1107,30 +1107,208 @@
 oom.
-util.shared.ResultPage
+query.api.v2.ExpressionOperator;%0Aimport stroom.util.shared.ResultPage;%0A%0Aimport event.logging.AdvancedQuery;%0Aimport event.logging.And;%0Aimport event.logging.Query;%0Aimport event.logging.SearchEventAction
 ;%0A%0Ai
@@ -1551,126 +1551,373 @@
 er;%0A
-%0A    @Inject%0A    MetaResourceImpl(Provider%3CMetaService%3E metaServiceProvider) %7B%0A        this.metaServiceProvider = meta
+    private final Provider%3CStroomEventLoggingService%3E eventLoggingServiceProvider;%0A%0A    @Inject%0A    MetaResourceImpl(Provider%3CMetaService%3E metaServiceProvider,%0A                     final Provider%3CStroomEventLoggingService%3E eventLoggingServiceProvider) %7B%0A        this.metaServiceProvider = metaServiceProvider;%0A        this.eventLoggingServiceProvider = eventLogging
 Serv
@@ -1928,32 +1928,33 @@
 rovider;%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -1943,32 +1943,79 @@
 %0A%0A    @Override%0A
+    @AutoLogged(OperationType.MANUALLY_LOGGED)%0A
     public Integ
@@ -2067,38 +2067,944 @@
 uest) %7B%0A        
-return
+final StroomEventLoggingService eventLoggingService = eventLoggingServiceProvider.get();%0A%0A        final ExpressionOperator expression = request.getCriteria().getExpression();%0A%0A        final String currentStatus = (request.getCurrentStatus() != null) ?%0A                request.getCurrentStatus().getDisplayValue() : %22selected%22;%0A        final String newStatus = (request.getNewStatus() != null) ?%0A                request.getNewStatus().getDisplayValue() : %22unspecified / unknown%22;%0A%0A        final SearchEventAction action = SearchEventAction.builder()%0A                .withQuery(StroomEventLoggingUtil.convertExpression(expression))%0A                .build();%0A%0A        return eventLoggingService.loggedResult(%0A                StroomEventLoggingUtil.buildTypeId(this, %22updateStatus%22),%0A                %22Modify the status of %22 + currentStatus + %22 streams to %22 + newStatus,%0A                action,%0A                () -%3E 
  metaServiceProv
@@ -3150,16 +3150,17 @@
 tatus())
+)
 ;%0A    %7D%0A
@@ -3152,32 +3152,70 @@
 tus()));%0A    %7D%0A%0A
+    @AutoLogged(OperationType.SEARCH)%0A
     @Override%0A  
@@ -3819,32 +3819,68 @@
 %0A%0A    @Override%0A
+    @AutoLogged(OperationType.VIEW)%0A
     public List%3C

Improve logging and prevent autologger warning for MetaResourcImpl