001/* 002 * (C) Copyright 2014 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 * Benoit Delbosc 018 * 019 */ 020 021package org.nuxeo.ecm.platform.query.core; 022 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.nuxeo.common.xmap.annotation.XNode; 029import org.nuxeo.common.xmap.annotation.XNodeList; 030import org.nuxeo.common.xmap.annotation.XObject; 031import org.nuxeo.ecm.platform.query.api.AggregateDefinition; 032import org.nuxeo.ecm.platform.query.api.AggregateRangeDateDefinition; 033import org.nuxeo.ecm.platform.query.api.AggregateRangeDefinition; 034import org.nuxeo.ecm.platform.query.api.PredicateFieldDefinition; 035 036/** 037 * @since 6.0 038 */ 039@XObject(value = "aggregate") 040public class AggregateDescriptor implements AggregateDefinition { 041 042 @XNodeList(value = "dateRanges/dateRange", type = ArrayList.class, componentType = AggregateRangeDateDescriptor.class) 043 protected List<AggregateRangeDateDescriptor> aggregateDateRanges; 044 045 @XNode(value = "properties") 046 protected PropertiesDescriptor aggregateProperties = new PropertiesDescriptor(); 047 048 protected Map<String, Integer> aggregateDateRangeDefinitionOrderMap; 049 050 private Map<String, Integer> aggregateRangeDefinitionOrderMap; 051 052 @XNodeList(value = "ranges/range", type = ArrayList.class, componentType = AggregateRangeDescriptor.class) 053 protected List<AggregateRangeDescriptor> aggregateRanges; 054 055 @XNode("field") 056 protected FieldDescriptor field; 057 058 @XNode("@id") 059 protected String id; 060 061 @XNode("@parameter") 062 protected String parameter; 063 064 @XNode("@type") 065 protected String type; 066 067 @Override 068 public AggregateDescriptor clone() { 069 AggregateDescriptor clone = new AggregateDescriptor(); 070 clone.id = id; 071 clone.parameter = parameter; 072 clone.type = type; 073 if (field != null) { 074 clone.field = field.clone(); 075 } 076 if (aggregateProperties != null) { 077 clone.aggregateProperties = new PropertiesDescriptor(); 078 clone.aggregateProperties.properties.putAll(aggregateProperties.properties); 079 } 080 if (aggregateRanges != null) { 081 clone.aggregateRanges = new ArrayList<AggregateRangeDescriptor>(aggregateRanges.size()); 082 clone.aggregateRanges.addAll(aggregateRanges); 083 } 084 if (aggregateDateRanges != null) { 085 clone.aggregateDateRanges = new ArrayList<AggregateRangeDateDescriptor>(aggregateDateRanges.size()); 086 clone.aggregateDateRanges.addAll(aggregateDateRanges); 087 } 088 return clone; 089 } 090 091 @Override 092 public Map<String, Integer> getAggregateDateRangeDefinitionOrderMap() { 093 if (aggregateDateRangeDefinitionOrderMap == null) { 094 aggregateDateRangeDefinitionOrderMap = new HashMap<String, Integer>(getDateRanges().size()); 095 for (int i = 0; i < getDateRanges().size(); i++) { 096 aggregateDateRangeDefinitionOrderMap.put(getDateRanges().get(i).getKey(), i); 097 } 098 } 099 return aggregateDateRangeDefinitionOrderMap; 100 } 101 102 @Override 103 public Map<String, Integer> getAggregateRangeDefinitionOrderMap() { 104 if (aggregateRangeDefinitionOrderMap == null) { 105 aggregateRangeDefinitionOrderMap = new HashMap<String, Integer>(getRanges().size()); 106 for (int i = 0; i < getRanges().size(); i++) { 107 aggregateRangeDefinitionOrderMap.put(getRanges().get(i).getKey(), i); 108 } 109 } 110 return aggregateRangeDefinitionOrderMap; 111 } 112 113 @Override 114 public List<AggregateRangeDateDefinition> getDateRanges() { 115 @SuppressWarnings("unchecked") 116 List<AggregateRangeDateDefinition> ret = (List<AggregateRangeDateDefinition>) (List<?>) aggregateDateRanges; 117 return ret; 118 } 119 120 @Override 121 public String getDocumentField() { 122 return parameter; 123 } 124 125 @Override 126 public String getId() { 127 return id; 128 } 129 130 @Override 131 public Map<String, String> getProperties() { 132 return aggregateProperties.getProperties(); 133 } 134 135 @Override 136 public List<AggregateRangeDefinition> getRanges() { 137 @SuppressWarnings("unchecked") 138 List<AggregateRangeDefinition> ret = (List<AggregateRangeDefinition>) (List<?>) aggregateRanges; 139 return ret; 140 } 141 142 @Override 143 public PredicateFieldDefinition getSearchField() { 144 return field; 145 } 146 147 @Override 148 public String getType() { 149 return type; 150 } 151 152 @Override 153 @SuppressWarnings("unchecked") 154 public void setDateRanges(List<AggregateRangeDateDefinition> ranges) { 155 aggregateDateRanges = (List<AggregateRangeDateDescriptor>) (List<?>) ranges; 156 aggregateDateRangeDefinitionOrderMap = null; 157 } 158 159 @Override 160 public void setDocumentField(String parameter) { 161 this.parameter = parameter; 162 } 163 164 @Override 165 public void setId(String id) { 166 this.id = id; 167 } 168 169 @Override 170 public void setProperty(String name, String value) { 171 aggregateProperties.properties.put(name, value); 172 } 173 174 @Override 175 @SuppressWarnings("unchecked") 176 public void setRanges(List<AggregateRangeDefinition> ranges) { 177 aggregateRanges = (List<AggregateRangeDescriptor>) (List<?>) ranges; 178 aggregateRangeDefinitionOrderMap = null; 179 } 180 181 @Override 182 public void setSearchField(PredicateFieldDefinition field) { 183 this.field = (FieldDescriptor) field; 184 } 185 186 @Override 187 public void setType(String type) { 188 this.type = type; 189 } 190 191}