001/*-
002 * #%L
003 * HAPI FHIR Server - SQL Migration
004 * %%
005 * Copyright (C) 2014 - 2023 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.migrate;
021
022import ca.uhn.fhir.jpa.migrate.taskdef.BaseTask;
023import org.flywaydb.core.api.MigrationVersion;
024
025import javax.annotation.Nonnull;
026import java.util.ArrayList;
027import java.util.Collection;
028import java.util.Iterator;
029import java.util.List;
030import java.util.Set;
031import java.util.function.Consumer;
032import java.util.stream.Collectors;
033
034public class MigrationTaskList implements Iterable<BaseTask> {
035        private final List<BaseTask> myTasks;
036
037        public MigrationTaskList() {
038                myTasks = new ArrayList<>();
039        }
040
041        public MigrationTaskList(List<BaseTask> theTasks) {
042                myTasks = theTasks;
043        }
044
045        public void addAll(Collection<BaseTask> theTasks) {
046                myTasks.addAll(theTasks);
047        }
048
049        public void setDoNothingOnSkippedTasks(String theSkipVersions) {
050                MigrationTaskSkipper.setDoNothingOnSkippedTasks(myTasks, theSkipVersions);
051        }
052
053        public int size() {
054                return myTasks.size();
055        }
056
057        public MigrationTaskList diff(Set<MigrationVersion> theAppliedMigrationVersions) {
058                List<BaseTask> unappliedTasks = myTasks.stream()
059                        .filter(task -> !theAppliedMigrationVersions.contains(MigrationVersion.fromVersion(task.getMigrationVersion())))
060                        .collect(Collectors.toList());
061                return new MigrationTaskList(unappliedTasks);
062        }
063
064        public void append(Iterable<BaseTask> theMigrationTasks) {
065                for (BaseTask next : theMigrationTasks) {
066                        myTasks.add(next);
067                }
068        }
069
070        public void add(BaseTask theTask) {
071                myTasks.add(theTask);
072        }
073
074        public void clear() {
075                myTasks.clear();
076        }
077
078        @Nonnull
079        @Override
080        public Iterator<BaseTask> iterator() {
081                return myTasks.iterator();
082        }
083
084        public void forEach(Consumer<? super BaseTask> theAction) {
085                myTasks.forEach(theAction);
086        }
087
088        public String getLastVersion() {
089                return myTasks.stream()
090                        .map(BaseTask::getMigrationVersion)
091                        .map(MigrationVersion::fromVersion)
092                        .sorted()
093                        .map(MigrationVersion::toString)
094                        .reduce((first, second) -> second)
095                        .orElse(null);
096        }
097}