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;
022
023/**
024 * Base class for building data transfer objects for results of requests to the SuggestionService.
025 *
026 * @author ogrisel
027 */
028public abstract class Suggestion implements Serializable {
029
030    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
031
032    private static final long serialVersionUID = 1L;
033
034    protected final String id;
035
036    protected final String type;
037
038    protected final String label;
039
040    protected final String iconURL;
041
042    protected String thumbnailURL = "";
043
044    protected String description = "";
045
046    protected boolean disabled = false;
047
048    public Suggestion(String id, String type, String label, String iconURL) {
049        this.id = id;
050        this.type = type;
051        this.label = label;
052        this.iconURL = iconURL;
053    }
054
055    /**
056     * @since 8.4
057     */
058    public Suggestion(String id, String type, String label, String iconURL, String thumbnailURL) {
059        this(id, type, label, iconURL);
060        this.thumbnailURL = thumbnailURL;
061    }
062
063
064    /**
065     * The id of the object associated to the suggestion.
066     *
067     * @since 6.0
068     */
069    public String getId() {
070        return id;
071    }
072
073    /**
074     * A string marker to give the type (i.e. category) of the suggested user action / intent. The type is used to
075     * broadcast the selected suggestion to the correct handler.
076     */
077    public String getType() {
078        return type;
079    }
080
081    /**
082     * The i18n label to display to the user for this suggestion.
083     */
084    public String getLabel() {
085        return label;
086    }
087
088    /**
089     * Relative URL path to download an icon (can represent the type of suggestion or the specific instance such as the
090     * mimetype icon of a document suggestion or the avatar icon of a user profile suggestion).
091     */
092    public String getIconURL() {
093        return iconURL;
094    }
095
096    public String getDescription() {
097        return description;
098    }
099
100    public Suggestion withDescription(String description) {
101        this.description = description;
102        return this;
103    }
104
105    /**
106     * Disabled suggestions can be useful to display suggestions that might have been relevant if the context was
107     * slightly different (e.g. if the user was logged in instead of anonymous): the UI should not make them selectable
108     * but the description should give information to the user on how to make that suggestion enabled (e.g. by logging
109     * in). The SuggestionService will throw an exception if the user selects a disabled suggestion.
110     */
111    public boolean getIsDisabled() {
112        return disabled;
113    }
114
115    /**
116     * @since 8.4
117     */
118    public String getThumbnailURL() {
119        return thumbnailURL;
120    }
121
122    /**
123     * @since 8.4
124     */
125    public Suggestion withThumbnailURL(String thumbnailURL) {
126        this.thumbnailURL = thumbnailURL;
127        return this;
128    }
129
130    public Suggestion disable() {
131        this.disabled = true;
132        return this;
133    }
134
135    /**
136     * @return the url to access to the object. It used by the navigation in the select2.
137     * @since 6.0
138     */
139    public abstract String getObjectUrl();
140
141    @Override
142    public String toString() {
143        return String.format("Suggestion(\"%s\", \"%s\", \"%s\")", type, label, iconURL);
144    }
145}