001/*
002 * (C) Copyright 2010-2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Olivier Grisel
016 */
017package org.nuxeo.ecm.platform.suggestbox.service;
018
019import java.io.Serializable;
020
021/**
022 * Base class for building data transfer objects for results of requests to the SuggestionService.
023 *
024 * @author ogrisel
025 */
026public abstract class Suggestion implements Serializable {
027
028    public static final String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
029
030    private static final long serialVersionUID = 1L;
031
032    protected final String id;
033
034    protected final String type;
035
036    protected final String label;
037
038    protected final String iconURL;
039
040    protected String description = "";
041
042    protected boolean disabled = false;
043
044    public Suggestion(String id, String type, String label, String iconURL) {
045        this.id = id;
046        this.type = type;
047        this.label = label;
048        this.iconURL = iconURL;
049    }
050
051    /**
052     * The id of the object associated to the suggestion.
053     *
054     * @since 6.0
055     */
056    public String getId() {
057        return id;
058    }
059
060    /**
061     * A string marker to give the type (i.e. category) of the suggested user action / intent. The type is used to
062     * broadcast the selected suggestion to the correct handler.
063     */
064    public String getType() {
065        return type;
066    }
067
068    /**
069     * The i18n label to display to the user for this suggestion.
070     */
071    public String getLabel() {
072        return label;
073    }
074
075    /**
076     * Relative URL path to download an icon (can represent the type of suggestion or the specific instance such as the
077     * mimetype icon of a document suggestion or the avatar icon of a user profile suggestion).
078     */
079    public String getIconURL() {
080        return iconURL;
081    }
082
083    public String getDescription() {
084        return description;
085    }
086
087    public Suggestion withDescription(String description) {
088        this.description = description;
089        return this;
090    }
091
092    /**
093     * Disabled suggestions can be useful to display suggestions that might have been relevant if the context was
094     * slightly different (e.g. if the user was logged in instead of anonymous): the UI should not make them selectable
095     * but the description should give information to the user on how to make that suggestion enabled (e.g. by logging
096     * in). The SuggestionService will throw an exception if the user selects a disabled suggestion.
097     */
098    public boolean getIsDisabled() {
099        return disabled;
100    }
101
102    public Suggestion disable() {
103        this.disabled = true;
104        return this;
105    }
106
107    /**
108     * @return the url to access to the object. It used by the navigation in the select2.
109     * @since 6.0
110     */
111    public abstract String getObjectUrl();
112
113    @Override
114    public String toString() {
115        return String.format("Suggestion(\"%s\", \"%s\", \"%s\")", type, label, iconURL);
116    }
117}