001/*
002 * (C) Copyright 2014-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 *     Nicolas Chapurlat
018 */
019package org.nuxeo.ecm.core.schema.types.resolver;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Locale;
025import java.util.Map;
026
027public class TestingColorResolver extends AbstractObjectResolver implements ObjectResolver {
028
029    private static final long serialVersionUID = 1L;
030
031    public enum MODE {
032        PRIMARY, SECONDARY
033    }
034
035    public interface Color {
036        String name();
037    }
038
039    public enum PrimaryColor implements Color {
040        RED, BLUE, YELLOW
041    }
042
043    public enum SecondaryColor implements Color {
044        VIOLET, ORANGE, GREEN
045    }
046
047    public static final String COLOR_MODE = "mode";
048
049    public static final String NAME = "colorReference";
050
051    private MODE mode;
052
053    private List<Class<?>> managedClasses = null;
054
055    @Override
056    public List<Class<?>> getManagedClasses() {
057        if (managedClasses == null) {
058            managedClasses = new ArrayList<>();
059            managedClasses.add(Color.class);
060        }
061        return managedClasses;
062    }
063
064    @Override
065    public void configure(Map<String, String> parameters) throws IllegalStateException, IllegalArgumentException {
066        super.configure(parameters);
067        String modeParam = parameters.get(COLOR_MODE);
068        if (modeParam == null || modeParam.trim().isEmpty()) {
069            throw new IllegalArgumentException("missing mode param");
070        }
071        try {
072            mode = MODE.valueOf(modeParam);
073        } catch (IllegalArgumentException e) {
074            throw new IllegalArgumentException("missing mode param", e);
075        }
076        this.parameters.put(COLOR_MODE, mode.name());
077    }
078
079    @Override
080    public String getName() throws IllegalStateException {
081        checkConfig();
082        return NAME;
083    }
084
085    @Override
086    public Color fetch(Object value) throws IllegalStateException {
087        checkConfig();
088        if (value instanceof String) {
089            String ref = (String) value;
090            switch (mode) {
091            case PRIMARY:
092                for (PrimaryColor color : PrimaryColor.values()) {
093                    if (color.name().equals(ref)) {
094                        return color;
095                    }
096                }
097                break;
098            case SECONDARY:
099                for (SecondaryColor color : SecondaryColor.values()) {
100                    if (color.name().equals(ref)) {
101                        return color;
102                    }
103                }
104                break;
105            }
106        }
107        return null;
108    }
109
110    @Override
111    @SuppressWarnings("unchecked")
112    public <T> T fetch(Class<T> type, Object value) throws IllegalStateException {
113        checkConfig();
114        if (Color.class.equals(type)) {
115            return (T) fetch(value);
116        } else if (mode == MODE.PRIMARY && PrimaryColor.class.equals(type)) {
117            return (T) fetch(value);
118        } else if (mode == MODE.SECONDARY && SecondaryColor.class.equals(type)) {
119            return (T) fetch(value);
120        }
121        return null;
122    }
123
124    @Override
125    public Serializable getReference(Object entity) throws IllegalStateException {
126        checkConfig();
127        if (entity instanceof Color) {
128            Color color = (Color) entity;
129            switch (mode) {
130            case PRIMARY:
131                if (color instanceof PrimaryColor) {
132                    return color.name();
133                }
134                break;
135            case SECONDARY:
136                if (color instanceof SecondaryColor) {
137                    return color.name();
138                }
139                break;
140            }
141        }
142        return null;
143    }
144
145    @Override
146    public String getConstraintErrorMessage(Object invalidValue, Locale locale) {
147        checkConfig();
148        return String.format("\"%s\" is not a correct %s color", invalidValue, mode.name().toLowerCase());
149    }
150
151}