001/*
002 * (C) Copyright 2017-2018 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.runtime.migration;
020
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import org.nuxeo.common.xmap.annotation.XNode;
025import org.nuxeo.common.xmap.annotation.XNodeMap;
026import org.nuxeo.common.xmap.annotation.XObject;
027import org.nuxeo.runtime.model.Descriptor;
028
029/**
030 * Descriptor of a Migration, consisting of States and Steps.
031 *
032 * @since 9.3
033 */
034@XObject("migration")
035public class MigrationDescriptor implements Descriptor {
036
037    @XObject("state")
038    public static class MigrationStateDescriptor implements Descriptor {
039
040        @XNode("@id")
041        public String id;
042
043        @XNode("description@label")
044        public String descriptionLabel;
045
046        @XNode("description")
047        public String description;
048
049        @Override
050        public String getId() {
051            return id;
052        }
053
054        public String getDescriptionLabel() {
055            return descriptionLabel;
056        }
057
058        public String getDescription() {
059            return description;
060        }
061    }
062
063    @XObject("step")
064    public static class MigrationStepDescriptor implements Descriptor {
065
066        @XNode("@id")
067        public String id;
068
069        @XNode("@fromState")
070        public String fromState;
071
072        @XNode("@toState")
073        public String toState;
074
075        @XNode("description@label")
076        public String descriptionLabel;
077
078        @XNode("description")
079        public String description;
080
081        @Override
082        public String getId() {
083            return id;
084        }
085
086        public String getFromState() {
087            return fromState;
088        }
089
090        public String getToState() {
091            return toState;
092        }
093
094        public String getDescriptionLabel() {
095            return descriptionLabel;
096        }
097
098        public String getDescription() {
099            return description;
100        }
101    }
102
103    @XNode("@id")
104    public String id;
105
106    @XNode("description@label")
107    public String descriptionLabel;
108
109    @XNode("description")
110    public String description;
111
112    @XNode("class")
113    public Class<?> klass;
114
115    @XNode("defaultState")
116    public String defaultState;
117
118    @XNodeMap(value = "state", key = "@id", type = LinkedHashMap.class, componentType = MigrationStateDescriptor.class)
119    public Map<String, MigrationStateDescriptor> states = new LinkedHashMap<>();
120
121    @XNodeMap(value = "step", key = "@id", type = LinkedHashMap.class, componentType = MigrationStepDescriptor.class)
122    public Map<String, MigrationStepDescriptor> steps = new LinkedHashMap<>();
123
124    @Override
125    public String getId() {
126        return id;
127    }
128
129    public String getDescriptionLabel() {
130        return descriptionLabel;
131    }
132
133    public String getDescription() {
134        return description;
135    }
136
137    public Class<?> getKlass() {
138        return klass;
139    }
140
141    public String getDefaultState() {
142        return defaultState;
143    }
144
145    public Map<String, MigrationStateDescriptor> getStates() {
146        return states;
147    }
148
149    public Map<String, MigrationStepDescriptor> getSteps() {
150        return steps;
151    }
152
153    @Override
154    public Descriptor merge(Descriptor o) {
155        MigrationDescriptor other = (MigrationDescriptor) o;
156        MigrationDescriptor merged = new MigrationDescriptor();
157        merged.id = id;
158        merged.klass = other.klass != null ? other.klass : klass;
159        merged.description = other.description != null ? other.description : description;
160        merged.defaultState = other.defaultState != null ? other.defaultState : defaultState;
161        merged.descriptionLabel = other.descriptionLabel != null ? other.descriptionLabel : descriptionLabel;
162        merged.steps.putAll(steps);
163        merged.steps.putAll(other.steps);
164        merged.states.putAll(states);
165        merged.states.putAll(other.states);
166        return merged;
167    }
168
169}