001/*
002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.security;
020
021import java.util.Calendar;
022
023import org.nuxeo.ecm.core.api.security.SecurityConstants;
024import org.nuxeo.ecm.core.bulk.BulkService;
025import org.nuxeo.ecm.core.bulk.message.BulkCommand;
026import org.nuxeo.ecm.core.event.Event;
027import org.nuxeo.ecm.core.event.EventListener;
028import org.nuxeo.ecm.core.repository.RepositoryService;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Listener triggering the check of expired retentions.
033 *
034 * @since 11.1
035 */
036public class RetentionExpiredFinderListener implements EventListener {
037
038    public static final String QUERY = "SELECT * FROM Document, Relation"
039            + " WHERE ecm:isProxy = 0 AND ecm:retainUntil < TIMESTAMP '%s'";
040
041
042    @Override
043    public void handleEvent(Event event) {
044        BulkService bulkService = Framework.getService(BulkService.class);
045        RepositoryService repositoryService = Framework.getService(RepositoryService.class);
046
047        Calendar now = Calendar.getInstance();
048        String nxql = String.format(QUERY, now.toInstant());
049
050        for (String repositoryName : repositoryService.getRepositoryNames()) {
051            BulkCommand command = new BulkCommand.Builder(RetentionExpiredAction.ACTION_NAME, nxql).user(
052                    SecurityConstants.SYSTEM_USERNAME).repository(repositoryName).build();
053            bulkService.submit(command);
054        }
055    }
056
057}