001/*
002 * (C) Copyright 2006-2007 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 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.elements;
023
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.Iterator;
027
028import org.nuxeo.theme.Manager;
029import org.nuxeo.theme.formats.Format;
030import org.nuxeo.theme.formats.FormatType;
031import org.nuxeo.theme.relations.DyadicRelation;
032import org.nuxeo.theme.relations.Relate;
033import org.nuxeo.theme.relations.Relation;
034import org.nuxeo.theme.relations.RelationStorage;
035import org.nuxeo.theme.types.TypeFamily;
036import org.nuxeo.theme.types.TypeRegistry;
037
038public final class ElementFormatter {
039
040    private ElementFormatter() {
041        // This class is not supposed to be instantiated.
042    }
043
044    public static void setFormat(final Object object, final Format format) {
045        // Remove the old format of the same type
046        final Format oldFormat = getFormatByType(object, format.getFormatType());
047        if (oldFormat != null) {
048            removeFormat(object, oldFormat);
049        }
050        Manager.getRelationStorage().add(new DyadicRelation(format.getPredicate(), (Element) object, format));
051    }
052
053    public static Format getFormatByType(final Object object, final FormatType type) {
054        final Collection<Relation> relations = Manager.getRelationStorage().search(type.getPredicate(),
055                (Element) object, null);
056        final Iterator<Relation> i = relations.iterator();
057        if (i.hasNext()) {
058            return (Format) ((DyadicRelation) i.next()).getRelate(2);
059        }
060        // TODO throw exception;
061        return null;
062    }
063
064    public static Collection<Format> getFormatsFor(final Element element) {
065        final Collection<Format> formats = new ArrayList<Format>();
066        // TODO use a type manager for registering and getting format types
067        final String[] formatTypeNames = { "widget", "style", "layout" };
068        for (String typeName : formatTypeNames) {
069            Format format = getFormatFor(element, typeName);
070            if (format != null) {
071                formats.add(format);
072            }
073        }
074        return formats;
075    }
076
077    public static Format getFormatFor(final Element element, final String typeName) {
078        final RelationStorage relationStorage = Manager.getRelationStorage();
079        final TypeRegistry typeRegistry = Manager.getTypeRegistry();
080        final FormatType type = (FormatType) typeRegistry.lookup(TypeFamily.FORMAT, typeName);
081        // FIXME: this loop doesn't loop!
082        for (Relation relation : relationStorage.search(type.getPredicate(), element, null)) {
083            return (Format) relation.getRelate(2);
084        }
085        return null;
086    }
087
088    public static Collection<Element> getElementsFor(final Format format) {
089        final Collection<Element> elements = new ArrayList<Element>();
090        final RelationStorage relationStorage = Manager.getRelationStorage();
091        final String[] formatTypeNames = { "widget", "style", "layout" };
092        final TypeRegistry typeRegistry = Manager.getTypeRegistry();
093        for (String typeName : formatTypeNames) {
094            final FormatType type = (FormatType) typeRegistry.lookup(TypeFamily.FORMAT, typeName);
095            for (Relation relation : relationStorage.search(type.getPredicate(), null, format)) {
096                elements.add((Element) relation.getRelate(1));
097            }
098        }
099        return elements;
100    }
101
102    public static void removeFormat(final Object object, final Format format) {
103        final RelationStorage relationStorage = Manager.getRelationStorage();
104        for (Relation relation : relationStorage.search(format.getPredicate(), (Relate) object, format)) {
105            relationStorage.remove(relation);
106        }
107    }
108
109}