001/*
002 *  Copyright 2011 Anyware Services
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
017package org.ametys.plugins.repository.dom;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.w3c.dom.Element;
023import org.w3c.dom.Node;
024
025import org.ametys.core.user.UserIdentity;
026import org.ametys.core.util.dom.AmetysAttribute;
027import org.ametys.plugins.repository.metadata.CompositeMetadata;
028import org.ametys.plugins.repository.metadata.CompositeMetadata.MetadataType;
029
030/**
031 * DOM layer for a USER metadata
032 */
033public class UserMetadataElement extends CompositeMetadataElement
034{
035    /**
036     * Constructor.
037     * @param metadata the underlying {@link CompositeMetadata}.
038     * @param name the name of underlying {@link CompositeMetadata}
039     */
040    public UserMetadataElement(CompositeMetadata metadata, String name)
041    {
042        super(metadata, name, null);
043    }
044
045    /**
046     * Constructor.
047     * @param metadata the underlying {@link CompositeMetadata}.
048     * @param name the name of underlying {@link CompositeMetadata}
049     * @param parent the parent {@link Element}.
050     */
051    public UserMetadataElement(CompositeMetadata metadata, String name, CompositeMetadataElement parent)
052    {
053        super(metadata, name, parent);
054    }
055    
056    @Override
057    protected Map<String, AmetysAttribute> _lookupAttributes()
058    {
059        Map<String, AmetysAttribute> result = new HashMap<>();
060        result.put("name", new AmetysAttribute("name", "name", null, _name, this));
061        result.put("type", new AmetysAttribute("type", "type", null, MetadataType.USER.toString(), this));
062        return result;
063    }
064    
065    @Override
066    public Node getFirstChild()
067    {
068        UserIdentity user = _object.getUser(_name, null);
069        if (user != null)
070        {
071            return new MetadataElement("login", _object.getCompositeMetadata(_name), this);
072        }
073        
074        return null;
075    }
076}