001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.metamodel.spec;
021
022import java.util.Collections;
023import java.util.List;
024import java.util.Set;
025
026import com.google.common.collect.Lists;
027
028import org.apache.isis.applib.annotation.MemberGroupLayout.ColumnSpans;
029import org.apache.isis.core.metamodel.facets.object.membergroups.MemberGroupLayoutFacet;
030
031
032public final class ObjectSpecifications {
033
034    private ObjectSpecifications() {
035    }
036
037    public enum MemberGroupLayoutHint {
038        LEFT,
039        MIDDLE,
040        RIGHT;
041
042        public int from(ColumnSpans columnSpans) {
043            if(this == LEFT) return columnSpans.getLeft();
044            if(this == MIDDLE) return columnSpans.getMiddle();
045            if(this == RIGHT) return columnSpans.getRight();
046            return 0;
047        }
048    }
049
050    public static List<String> orderByMemberGroups(ObjectSpecification objSpec, Set<String> groupNamesToOrder, MemberGroupLayoutHint memberGroupLayoutHint) {
051        final MemberGroupLayoutFacet facet = objSpec.getFacet(MemberGroupLayoutFacet.class);
052        final List<String> leftColumnGroupNames = Lists.newArrayList(groupNamesToOrder);
053        
054        // not expected to happen
055        if(facet == null) {
056            return leftColumnGroupNames;
057        }
058        
059        if(memberGroupLayoutHint == MemberGroupLayoutHint.MIDDLE) {
060            return facet.getColumnSpans().getMiddle()>0? facet.getMiddle(): Collections.<String>emptyList();
061        }
062        if(memberGroupLayoutHint == MemberGroupLayoutHint.RIGHT) {
063            return facet.getColumnSpans().getRight()>0? facet.getRight(): Collections.<String>emptyList();
064        }
065        
066        // else left; per the requested order, including any groups not mentioned in either list, 
067        // but excluding any groups in the middle or right columns
068        final List<String> groupNamedInRequiredOrder = facet.getLeft();
069        final List<String> order = order(leftColumnGroupNames, groupNamedInRequiredOrder);
070        
071        if(facet.getColumnSpans().getMiddle() > 0) {
072            order.removeAll(facet.getMiddle());
073        }
074        if(facet.getColumnSpans().getRight() > 0) {
075            order.removeAll(facet.getRight());
076        }
077        return order;
078    }
079
080    static List<String> order(final List<String> valuesToOrder, final List<String> valuesInRequiredOrder) {
081        int i=0;
082        for(String memberGroup: valuesInRequiredOrder) {
083            if(valuesToOrder.contains(memberGroup)) {
084                // move to next position
085                valuesToOrder.remove(memberGroup);
086                valuesToOrder.add(i++, memberGroup);
087            }
088        }
089        
090        return valuesToOrder;
091    }
092
093
094}