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