001/*-
002 * #%L
003 * HAPI FHIR Server - SQL Migration
004 * %%
005 * Copyright (C) 2014 - 2024 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 jakarta.annotation.Nonnull;
024import org.flywaydb.core.api.MigrationVersion;
025
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 ->
060                                                !theAppliedMigrationVersions.contains(MigrationVersion.fromVersion(task.getMigrationVersion())))
061                                .collect(Collectors.toList());
062                return new MigrationTaskList(unappliedTasks);
063        }
064
065        public void append(Iterable<BaseTask> theMigrationTasks) {
066                for (BaseTask next : theMigrationTasks) {
067                        myTasks.add(next);
068                }
069        }
070
071        public void add(BaseTask theTask) {
072                myTasks.add(theTask);
073        }
074
075        public void clear() {
076                myTasks.clear();
077        }
078
079        @Nonnull
080        @Override
081        public Iterator<BaseTask> iterator() {
082                return myTasks.iterator();
083        }
084
085        public void forEach(Consumer<? super BaseTask> theAction) {
086                myTasks.forEach(theAction);
087        }
088
089        public String getLastVersion() {
090                return myTasks.stream()
091                                .map(BaseTask::getMigrationVersion)
092                                .map(MigrationVersion::fromVersion)
093                                .sorted()
094                                .map(MigrationVersion::toString)
095                                .reduce((first, second) -> second)
096                                .orElse(null);
097        }
098}