001/*
002 * (C) Copyright 2020 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 *     Nour AL KOTOB
018 */
019package org.nuxeo.runtime.migration;
020
021import java.util.List;
022import java.util.stream.Collectors;
023
024import org.apache.commons.lang3.builder.EqualsBuilder;
025import org.apache.commons.lang3.builder.HashCodeBuilder;
026import org.nuxeo.runtime.migration.MigrationService.MigrationStatus;
027
028/**
029 * @since 11.2
030 */
031public class Migration {
032
033    protected final String id;
034
035    protected final String description;
036
037    protected final String descriptionLabel;
038
039    protected final MigrationStatus status;
040
041    protected final List<MigrationStep> steps;
042
043    protected Migration(String id, String description, String label, MigrationStatus status,
044            List<MigrationStep> steps) {
045        this.id = id;
046        this.description = description;
047        this.descriptionLabel = label;
048        this.steps = steps;
049        this.status = status;
050    }
051
052    public static Migration from(MigrationDescriptor descriptor, MigrationStatus status) {
053        return new Migration(descriptor.getId(), descriptor.getDescription(), descriptor.getDescriptionLabel(), status,
054                getAvailableSteps(descriptor, status));
055    }
056
057    protected static List<MigrationStep> getAvailableSteps(MigrationDescriptor descriptor, MigrationStatus status) {
058        return descriptor.getSteps()
059                         .values()
060                         .stream()
061                         .filter(s -> s.getFromState().equals(status.getState()))
062                         .map(MigrationStep::from)
063                         .collect(Collectors.toUnmodifiableList());
064    }
065
066    public String getId() {
067        return id;
068    }
069
070    public String getDescription() {
071        return description;
072    }
073
074    public String getDescriptionLabel() {
075        return descriptionLabel;
076    }
077
078    public MigrationStatus getStatus() {
079        return status;
080    }
081
082    public List<MigrationStep> getSteps() {
083        return steps;
084    }
085
086    @Override
087    public int hashCode() {
088        return HashCodeBuilder.reflectionHashCode(this);
089    }
090
091    @Override
092    public boolean equals(Object obj) {
093        return EqualsBuilder.reflectionEquals(this, obj);
094    }
095
096    @Override
097    public String toString() {
098        return "Migration [id=" + id + ", description=" + description + ", descriptionLabel=" + descriptionLabel
099                + ", status=" + status + ", steps=" + steps + "]";
100    }
101
102}