001/*
002 * (C) Copyright 2016 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 *     Funsho David
018 */
019
020package org.nuxeo.ecm.platform.query.core;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.nuxeo.common.xmap.annotation.XNode;
026import org.nuxeo.common.xmap.annotation.XNodeList;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.ecm.core.api.SortInfo;
029import org.nuxeo.ecm.platform.query.api.QuickFilter;
030
031/**
032 * Descriptor for the quick filter used by page providers
033 *
034 * @author Funsho David
035 * @since 8.4
036 */
037@XObject(value = "quickFilters")
038public class QuickFilterDescriptor implements QuickFilter {
039
040    @Override
041    public int hashCode() {
042        final int prime = 31;
043        int result = 1;
044        result = prime * result + ((clause == null) ? 0 : clause.hashCode());
045        result = prime * result + ((name == null) ? 0 : name.hashCode());
046        result = prime * result + ((sortInfos == null) ? 0 : sortInfos.hashCode());
047        return result;
048    }
049
050    @Override
051    public boolean equals(Object obj) {
052        if (this == obj)
053            return true;
054        if (obj == null)
055            return false;
056        if (getClass() != obj.getClass())
057            return false;
058        QuickFilterDescriptor other = (QuickFilterDescriptor) obj;
059        if (clause == null) {
060            if (other.clause != null)
061                return false;
062        } else if (!clause.equals(other.clause))
063            return false;
064        if (name == null) {
065            if (other.name != null)
066                return false;
067        } else if (!name.equals(other.name))
068            return false;
069        if (sortInfos == null) {
070            if (other.sortInfos != null)
071                return false;
072        } else if (!sortInfos.equals(other.sortInfos))
073            return false;
074        return true;
075    }
076
077    @XNode("@name")
078    protected String name;
079
080    @XNode("clause")
081    protected String clause;
082
083    @XNodeList(value = "sort", type = ArrayList.class, componentType = SortInfoDescriptor.class)
084    protected List<SortInfoDescriptor> sortInfos;
085
086    @Override
087    public String getName() {
088        return name;
089    }
090
091    @Override
092    public String getClause() {
093        return clause;
094    }
095
096    @Override
097    public List<SortInfo> getSortInfos() {
098        List<SortInfo> infos = new ArrayList<>();
099        for (SortInfoDescriptor sortInfoDesc : sortInfos) {
100            infos.add(sortInfoDesc.getSortInfo());
101        }
102        return infos;
103    }
104
105    @Override
106    public void setName(String name) {
107        this.name = name;
108    }
109
110    @Override
111    public void setClause(String clause) {
112        this.clause = clause;
113    }
114
115    @Override
116    public QuickFilterDescriptor clone() {
117        QuickFilterDescriptor clone = new QuickFilterDescriptor();
118        clone.name = getName();
119        clone.clause = getClause();
120        if (sortInfos != null) {
121            clone.sortInfos = new ArrayList<>();
122            for (SortInfoDescriptor sortInfo : sortInfos) {
123                clone.sortInfos.add(sortInfo);
124            }
125        }
126        return clone;
127    }
128}