/* * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.server.rest.model; import com.intellij.util.containers.SortedList; import java.util.*; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import jetbrains.buildServer.ServiceLocator; import jetbrains.buildServer.server.rest.data.FilterUtil; import jetbrains.buildServer.server.rest.util.DefaultValueAware; import jetbrains.buildServer.server.rest.util.ValueWithDefault; import jetbrains.buildServer.serverSide.Parameter; import jetbrains.buildServer.serverSide.SimpleParameter; import jetbrains.buildServer.util.CaseInsensitiveStringComparator; import jetbrains.buildServer.util.CollectionsUtil; import jetbrains.buildServer.util.Converter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * @author Yegor.Yarko * Date: 13.07.2009 */ @XmlRootElement(name = "properties") public class Properties implements DefaultValueAware { @XmlAttribute public Integer count; @XmlAttribute(required = false) @Nullable public String href; @XmlElement(name = "property") public List properties = new SortedList(new Comparator() { private final CaseInsensitiveStringComparator comp = new CaseInsensitiveStringComparator(); public int compare(final Property o1, final Property o2) { return comp.compare(o1.name, o2.name); } }); public Properties() { } //todo: use another constructor public Properties(@NotNull final Map propertiesP) { for (Map.Entry prop : propertiesP.entrySet()) { properties.add(new Property(prop.getKey(), prop.getValue())); } } public Properties(@Nullable final Map properties, @Nullable String href, @NotNull final Fields fields) { if (properties == null) { this.count = null; this.properties = null; } else { this.count = ValueWithDefault.decideIncludeByDefault(fields.isIncluded("count"), properties.size()); if (fields.isIncluded("property", false, true)){ for (java.util.Map.Entry prop : properties.entrySet()) { this.properties.add(new Property(prop.getKey(), prop.getValue())); } } } this.href = ValueWithDefault.decideDefault(fields.isIncluded("href"), href); } public Properties(@Nullable final Collection parameters, @Nullable final Collection ownParameters, @Nullable String href, @NotNull final Fields fields, @NotNull final ServiceLocator serviceLocator) { if (parameters == null) { this.count = null; this.properties = null; } else { this.count = ValueWithDefault.decideIncludeByDefault(fields.isIncluded("count"), parameters.size()); if (fields.isIncluded("property", false, true)) { for (Parameter parameter : parameters) { this.properties.add( new Property(parameter, ownParameters != null && ownParameters.contains(parameter), fields.getNestedField("property", Fields.NONE, Fields.LONG), serviceLocator)); } } } this.href = ValueWithDefault.decideDefault(fields.isIncluded("href"), href); } public static List convertToSimpleParameters(final Map parametersMap) { return CollectionsUtil.convertCollection(parametersMap.entrySet(), new Converter>() { public Parameter createFrom(@NotNull final Map.Entry source) { return new SimpleParameter(source.getKey(), source.getValue()); } }); } @NotNull public Map getMap() { return getMap(null); } @NotNull public Map getMap(final Boolean ownOnly) { if (properties == null) { return new HashMap(); } final HashMap result = new HashMap(properties.size()); for (Property property : properties) { boolean actualOwn = property.own != null && property.own; if (FilterUtil.isIncludedByBooleanFilter(ownOnly, actualOwn)){ result.put(property.name, property.value); } } return result; } public boolean isDefault() { return ValueWithDefault.isAllDefault(count, href, properties); } @NotNull public List getFromPosted(@NotNull final ServiceLocator serviceLocator) { if (properties == null) { return Collections.emptyList(); } final ArrayList result = new ArrayList(properties.size()); for (Property parameter : properties) { result.add(parameter.getFromPosted(serviceLocator)); } return result; } }