Class LabelUtil


  • public final class LabelUtil
    extends Object
    Utility class designed to assist dealing with the client interface. In particular, the conversion from a string to a label and back, and the conversion from a list of strings to a list of labels and back.
    • Field Detail

      • LABEL_DELIM_CHARS

        public static final String LABEL_DELIM_CHARS
        The characters accepted as label delimiters
        See Also:
        Constant Field Values
      • MAX_ALLOWED_LABELS_PER_INPUT

        public static final int MAX_ALLOWED_LABELS_PER_INPUT
        The maximum number of labels we allow to be entered via a single sumbission from the user interface.
        See Also:
        Constant Field Values
      • MAX_ALLOWED_LABELS

        public static final int MAX_ALLOWED_LABELS
        The maximum number of labels allowed on a page or space.
        See Also:
        Constant Field Values
      • LABELS_STRING

        public static final String LABELS_STRING
        The name of the form field containing the space separated labels
        See Also:
        Constant Field Values
    • Constructor Detail

      • LabelUtil

        public LabelUtil()
    • Method Detail

      • convertToDelimitedString

        public static String convertToDelimitedString​(Labelable obj,
                                                      com.atlassian.user.User user)
        Helper method converting list of labels associated with the labelable object into a space separated string of the label names.
      • convertToDelimitedString

        public static String convertToDelimitedString​(List names)
      • isValidLabelNames

        public static boolean isValidLabelNames​(String delimitedLabelNames)
        Parameters:
        delimitedLabelNames - contains a list of label names delimited by LABEL_DELIM_CHARS
        Returns:
        true if the delimitedLabelNames represents a string of valid label names, false otherwise.
      • isValidLabelNames

        public static boolean isValidLabelNames​(Collection names)
        Parameters:
        names - a collection of label names as Strings.
        Returns:
        true if the delimitedLabelNames represents a string of valid label names, false otherwise.
      • isValidLabelName

        public static boolean isValidLabelName​(String labelName)
      • isValidLabelLengths

        public static boolean isValidLabelLengths​(String concatenatedLabelNames)
      • isValidLabelLengths

        public static boolean isValidLabelLengths​(Collection labelNames)
      • isValidLabelLength

        public static boolean isValidLabelLength​(String input)
      • getVisibleLabelNames

        public static List getVisibleLabelNames​(List labels,
                                                String username)
        Get a list of the visible label names for this content. System labels and personal labels that do not belong to this user will be stripped. Visible non-global label names will have their namespace prefixed( i.e. my:foo, team:bar)
        Parameters:
        labels - the list of Label objects to filter
        username - the user who the list is being filtered for
        Returns:
        a list of the names of the labels as Strings that the user can see
      • split

        public static List<String> split​(String input)
        Split the input string according to the label delimitation rules.
        Parameters:
        input - space separated string of labels
        Returns:
        a collection of strings, each representing a label name, in the order in which they appeared in the original input. Duplicates excluded.
      • countLabels

        public static int countLabels​(String input)
        Counts the labels present in an input string. Counts each appearance of duplicate labels.
        Parameters:
        input - a string of labels delimited by the characters in LABEL_DELIM_CHARS. May be null or empty.
        Returns:
        the number of labels present in the string. Returns 0 for empty or null strings.
      • countLabelsWithoutFavourites

        public static int countLabelsWithoutFavourites​(String labelsString)
        Counts the labels present in an input string. Counts each appearance of duplicate labels. Labels of type "Favourite" (LabelManager.FAVOURITE_LABEL and LabelManager.FAVOURITE_LABEL_YANKEE) are ignored.
        Parameters:
        labelsString - a string of labels delimited by the characters in LABEL_DELIM_CHARS. May be null or empty.
        Returns:
        the number of labels present in the string. Returns 0 for empty or null strings.
      • countParsedLabelNamesWithoutFavourites

        public static int countParsedLabelNamesWithoutFavourites​(Collection<ParsedLabelName> labels)
        Counts the labels present in an input collection. Counts each appearance of duplicate labels. Labels of type "Favourite" (LabelManager.FAVOURITE_LABEL and LabelManager.FAVOURITE_LABEL_YANKEE) are ignored.
        Parameters:
        labels - a collection of parsed labels
        Returns:
        the number of labels present in the collection.
      • countLabelsWithoutFavourites

        public static int countLabelsWithoutFavourites​(Collection<Label> labels)
        Counts the labels present in an input collection. Counts each appearance of duplicate labels. Labels of type "Favourite" (LabelManager.FAVOURITE_LABEL and LabelManager.FAVOURITE_LABEL_YANKEE) are ignored.
        Parameters:
        labels - a collection of labels
        Returns:
        the number of labels present in the collection.
      • syncState

        @Deprecated
        public static boolean syncState​(String source,
                                        LabelManager labelManager,
                                        com.atlassian.user.User user,
                                        Labelable dest,
                                        boolean ignored)
        Neat method that will synchronize a space separated list of label names with the labels contained within the labelable object. Labels that do not exist in the comma separated list will be removed, and labels that appear in the comma separated list will be created.

        Parameters:
        source - is a space separated list of label names, eg: the following list "LabelA LabelB LabelC" will be converted into label instances with the names 'LabelA', 'LabelB' and 'LabelC'
        ignored - is ignored
      • syncState

        public static boolean syncState​(String source,
                                        LabelManager labelManager,
                                        com.atlassian.user.User user,
                                        Labelable dest)
        Neat method that will synchronize a space separated list of label names with the labels contained within the labelable object. Labels that do not exist in the comma separated list will be removed, and labels that appear in the comma separated list will be created.
        Parameters:
        source - is a space separated list of label names, eg: the following list "LabelA LabelB LabelC" will be converted into label instances with the names 'LabelA', 'LabelB' and 'LabelC'
      • addLabel

        public static Label addLabel​(String labelReference,
                                     LabelManager labelManager,
                                     Labelable object)
        Adds the label to the specified object. The label is a string in the form of an unparsed label, and is validated by isValidLabelName(String) and isValidLabelLength(String).

        If the label can't be added, already exists, or is invalid, this method returns null.

        Returns:
        the label that was created and added or null if no label was added.
      • addLabel

        public static Label addLabel​(String labelReference,
                                     LabelManager labelManager,
                                     Labelable object,
                                     ConfluenceUser user)
        Adds the label to the specified object. The label is a string in the form of an unparsed label, and is validated by isValidLabelName(String) and isValidLabelLength(String).

        If the label can't be added, already exists, or is invalid, this method returns null.

        Parameters:
        user - The user to associate with the label, regardless of the label reference.
        Returns:
        the label that was created and added or null if no label was added.
      • recordLabelInteractionInHistory

        public static void recordLabelInteractionInHistory​(Label label)
      • rankResults

        public static List rankResults​(List results)
      • getRecentAndPopularLabels

        public static List getRecentAndPopularLabels​(String spaceKey,
                                                     LabelManager labelManager,
                                                     int maxResults,
                                                     String user)
      • fixLabels

        public static void fixLabels​(List suggestedLabels)
      • extractLabelsFromLabellings

        public static List<Label> extractLabelsFromLabellings​(List<Labelling> labellings)
        Simple utility method to extract the label instances from the given list of labelling instances.
        Returns:
        a new list containing the referenced Label instances.
      • getLabelPredicate

        @Deprecated
        public static com.google.common.base.Predicate<Label> getLabelPredicate​(com.atlassian.user.User user,
                                                                                boolean hideSpecialLabels)
        Deprecated.
      • labelPredicate

        public static Predicate<Label> labelPredicate​(com.atlassian.user.User user,
                                                      boolean hideSpecialLabels)
        Since:
        7.0.1
      • getLabelFilters

        public static com.atlassian.core.util.filter.ListFilter getLabelFilters​(com.atlassian.user.User user,
                                                                                boolean hideSpecialLabels)
        returns only labels the user can see and also strips out special labels if 'hideSpecialLabels' flag is set
      • getLabelsHash

        public static String getLabelsHash​(List<Label> labels)