001/*
002 * (C) Copyright 2010-2013 Nuxeo SA (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 *     Olivier Grisel
018 */
019package org.nuxeo.ecm.platform.suggestbox.service;
020
021import java.io.Serializable;
022import java.util.List;
023import java.util.Map;
024
025/**
026 * Base class for building data transfer objects for results of requests to the SuggestionService.
027 *
028 * @author ogrisel
029 */
030public abstract class Suggestion implements Serializable {
031
032    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
033
034    private static final long serialVersionUID = 1L;
035
036    protected final String id;
037
038    protected final String type;
039
040    protected final String label;
041
042    protected final String iconURL;
043
044    protected String thumbnailURL = "";
045
046    protected String description = "";
047
048    protected Map<String, List<String>> highlights;
049
050    protected boolean disabled = false;
051
052    public Suggestion(String id, String type, String label, String iconURL) {
053        this.id = id;
054        this.type = type;
055        this.label = label;
056        this.iconURL = iconURL;
057    }
058
059    /**
060     * @since 8.4
061     */
062    public Suggestion(String id, String type, String label, String iconURL, String thumbnailURL) {
063        this(id, type, label, iconURL);
064        this.thumbnailURL = thumbnailURL;
065    }
066
067    /**
068     * @since 9.2
069     */
070    public Suggestion(String id, String type, String label, String iconURL, String thumbnailURL,
071            Map<String, List<String>> highlights) {
072        this(id, type, label, iconURL);
073        this.thumbnailURL = thumbnailURL;
074        this.highlights = highlights;
075    }
076
077    /**
078     * The id of the object associated to the suggestion.
079     *
080     * @since 6.0
081     */
082    public String getId() {
083        return id;
084    }
085
086    /**
087     * A string marker to give the type (i.e. category) of the suggested user action / intent. The type is used to
088     * broadcast the selected suggestion to the correct handler.
089     */
090    public String getType() {
091        return type;
092    }
093
094    /**
095     * The i18n label to display to the user for this suggestion.
096     */
097    public String getLabel() {
098        return label;
099    }
100
101    /**
102     * Relative URL path to download an icon (can represent the type of suggestion or the specific instance such as the
103     * mimetype icon of a document suggestion or the avatar icon of a user profile suggestion).
104     */
105    public String getIconURL() {
106        return iconURL;
107    }
108
109    public String getDescription() {
110        return description;
111    }
112
113    public Suggestion withDescription(String description) {
114        this.description = description;
115        return this;
116    }
117
118    /**
119     * Disabled suggestions can be useful to display suggestions that might have been relevant if the context was
120     * slightly different (e.g. if the user was logged in instead of anonymous): the UI should not make them selectable
121     * but the description should give information to the user on how to make that suggestion enabled (e.g. by logging
122     * in). The SuggestionService will throw an exception if the user selects a disabled suggestion.
123     */
124    public boolean getIsDisabled() {
125        return disabled;
126    }
127
128    /**
129     * @since 8.4
130     */
131    public String getThumbnailURL() {
132        return thumbnailURL;
133    }
134
135    /**
136     * @since 9.2
137     */
138    public Suggestion withHighlights(Map<String, List<String>> highlights) {
139        this.highlights = highlights;
140        return this;
141    }
142
143    /**
144     * @since 8.4
145     */
146    public Suggestion withThumbnailURL(String thumbnailURL) {
147        this.thumbnailURL = thumbnailURL;
148        return this;
149    }
150
151
152    public Suggestion disable() {
153        this.disabled = true;
154        return this;
155    }
156
157    /**
158     * @return the url to access to the object. It used by the navigation in the select2.
159     * @since 6.0
160     */
161    public abstract String getObjectUrl();
162
163    @Override
164    public String toString() {
165        return String.format("Suggestion(\"%s\", \"%s\", \"%s\")", type, label, iconURL);
166    }
167
168    /**
169     * Get the map of highlights associated to the suggested result. The key of a map entry item represents the
170     * highlighted field, the value is the list of segment.
171     *
172     * @since 9.2
173     */
174    public Map<String, List<String>> getHighlights() {
175        return highlights;
176    }
177}