/*
 * 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.storedquery.impl;

import stroom.task.api.TaskContext;
import stroom.util.logging.LambdaLogger;
import stroom.util.logging.LambdaLoggerFactory;

import javax.inject.Inject;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.function.Supplier;

/**
 * Task to clean out old query history items.
 */
public class StoredQueryHistoryCleanExecutor {
    private static final LambdaLogger LOGGER = LambdaLoggerFactory.getLogger(StoredQueryHistoryCleanExecutor.class);

    private final TaskContext taskContext;
    private final StoredQueryDao storedQueryDao;
    private final StoredQueryConfig storedQueryConfig;

    @Inject
    public StoredQueryHistoryCleanExecutor(final TaskContext taskContext, final StoredQueryDao storedQueryDao, final StoredQueryConfig storedQueryConfig) {
        this.taskContext = taskContext;
        this.storedQueryDao = storedQueryDao;
        this.storedQueryConfig = storedQueryConfig;
    }

    public void exec() {
        clean(false);
    }

    public void clean(final boolean favourite) {
        info(() -> "Starting history clean task");

        final int historyItemsRetention = storedQueryConfig.getItemsRetention();
        final int historyDaysRetention = storedQueryConfig.getDaysRetention();

        final long oldestCrtMs = ZonedDateTime.now().minusDays(historyDaysRetention).toInstant().toEpochMilli();

        final List<String> users = storedQueryDao.getUsers(favourite);
        users.forEach(user -> {
            info(() -> "Cleaning query history for '" + user + "'");

            final Integer oldestId = storedQueryDao.getOldestId(user, favourite, historyItemsRetention);
            storedQueryDao.clean(user, favourite, oldestId, oldestCrtMs);
        });

        info(() -> "Finished history clean task");
    }

    private void info(final Supplier<String> message) {
        LOGGER.debug(message);
        taskContext.info(message);
    }
}

@@ -801,29 +801,61 @@
 va.time.
-ZonedDateTime
+Instant;%0Aimport java.time.temporal.ChronoUnit
 ;%0Aimport
@@ -1364,45 +1364,131 @@
 ext,
- final StoredQueryDao storedQueryDao,
+%0A                                           final StoredQueryDao storedQueryDao,%0A                                          
  fin
@@ -2027,21 +2027,15 @@
 s = 
-ZonedDateTime
+Instant
 .now
@@ -2042,20 +2042,16 @@
 ().minus
-Days
 (history
@@ -2067,20 +2067,25 @@
 tion
-).toInstant(
+, ChronoUnit.DAYS
 ).to

Fix incorrect use of default timezone