001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.fragments;
016
017import org.apache.commons.logging.Log;
018import org.apache.commons.logging.LogFactory;
019import org.nuxeo.theme.Manager;
020import org.nuxeo.theme.elements.ElementType;
021import org.nuxeo.theme.types.TypeFamily;
022import org.nuxeo.theme.types.TypeRegistry;
023import org.nuxeo.theme.uids.UidManager;
024
025public final class FragmentFactory {
026
027    private static final Log log = LogFactory.getLog(FragmentFactory.class);
028
029    public static Fragment create(String typeName) {
030        TypeRegistry typeRegistry = Manager.getTypeRegistry();
031        ElementType elementType = (ElementType) typeRegistry.lookup(TypeFamily.ELEMENT, "fragment");
032        FragmentType fragmentType = (FragmentType) typeRegistry.lookup(TypeFamily.FRAGMENT, typeName);
033
034        if (fragmentType == null) {
035            log.error("Fragment type not found: " + typeName);
036            return null;
037        }
038
039        String className = fragmentType.getClassName();
040        UidManager uidManager = Manager.getUidManager();
041
042        Fragment fragment = null;
043        try {
044            fragment = (Fragment) Class.forName(className).newInstance();
045            fragment.setElementType(elementType);
046            fragment.setFragmentType(fragmentType);
047
048            uidManager.register(fragment);
049        } catch (ReflectiveOperationException e) {
050            log.error(e, e);
051        }
052        return fragment;
053    }
054
055}