001    package de.deepamehta.plugins.facets.model;
002    
003    import de.deepamehta.core.model.CompositeValueModel;
004    import de.deepamehta.core.model.TopicModel;
005    
006    import org.codehaus.jettison.json.JSONObject;
007    
008    import java.util.List;
009    
010    
011    
012    /**
013     * A facet value as used in update facet calls.
014     * Used for both, single-valued facets and multiple-valued facets.
015     */
016    public class FacetValue extends CompositeValueModel {
017    
018        private String childTypeUri;
019    
020        // ---------------------------------------------------------------------------------------------------- Constructors
021    
022        public FacetValue(String childTypeUri) {
023            this.childTypeUri = childTypeUri;
024        }
025    
026        public FacetValue(JSONObject obj) {
027            super(obj);
028            try {
029                if (size() != 1) {
030                    throw new RuntimeException("There are " + size() + " child type entries (expected is 1)");
031                }
032                //
033                this.childTypeUri = iterator().next();
034            } catch (Exception e) {
035                throw new RuntimeException("Parsing FacetValue failed (JSONObject=" + obj + ")", e);
036            }
037        }
038    
039        // -------------------------------------------------------------------------------------------------- Public Methods
040    
041        /**
042         * Accesses a single-valued facet.
043         */
044        public TopicModel getTopic() {
045            return getTopic(childTypeUri);
046        }
047    
048        /**
049         * Accesses a multiple-valued facet.
050         */
051        public List<TopicModel> getTopics() {
052            return getTopics(childTypeUri);
053        }
054    
055        // ---
056    
057        /**
058         * Convenience method to put a *simple* value in a single-valued facet.
059         */
060        public FacetValue put(Object value) {
061            return (FacetValue) put(childTypeUri, value);
062        }
063    
064        /**
065         * Convenience method to put a *composite* value in a single-valued facet.
066         */
067        public FacetValue put(CompositeValueModel value) {
068            return (FacetValue) put(childTypeUri, value);
069        }
070    
071        // ---
072    
073        /**
074         * Adds a by-ID topic reference to a multiple-valued facet.
075         */
076        public FacetValue addRef(long refTopicId) {
077            return (FacetValue) addRef(childTypeUri, refTopicId);
078        }
079    
080        /**
081         * Adds a by-URI topic reference to a multiple-valued facet.
082         */
083        public FacetValue addRef(String refTopicUri) {
084            return (FacetValue) addRef(childTypeUri, refTopicUri);
085        }
086    }