001/*
002 *  Copyright 2018 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 */
016package org.ametys.cms.data.type;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022import java.util.Optional;
023
024import javax.xml.transform.TransformerException;
025
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.cocoon.xml.AttributesImpl;
029import org.apache.cocoon.xml.XMLUtils;
030import org.apache.commons.lang3.StringUtils;
031import org.w3c.dom.Element;
032import org.xml.sax.ContentHandler;
033import org.xml.sax.SAXException;
034
035import org.ametys.core.model.type.AbstractElementType;
036import org.ametys.core.user.User;
037import org.ametys.core.user.UserIdentity;
038import org.ametys.core.user.UserManager;
039import org.ametys.plugins.core.user.UserHelper;
040import org.ametys.runtime.model.ViewItem;
041import org.ametys.runtime.model.type.DataContext;
042
043/**
044 * Abstract class for user type of elements
045 */
046public abstract class AbstractUserElementType extends AbstractElementType<UserIdentity>
047{
048    /** The user helper */
049    protected UserHelper _helper;
050    /** The user manager */
051    protected UserManager _userManager;
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        super.service(manager);
057        _helper = (UserHelper) manager.lookup(UserHelper.ROLE);
058        _userManager = (UserManager) manager.lookup(UserManager.ROLE);
059    }
060
061    @Override
062    public UserIdentity convertValue(Object value)
063    {
064        if (value instanceof String)
065        {
066            if (StringUtils.isEmpty((String) value))
067            {
068                return null;
069            }
070            
071            UserIdentity user = UserIdentity.stringToUserIdentity((String) value);
072            if (user != null)
073            {
074                return user;
075            }
076        }
077        
078        return super.convertValue(value);
079    }
080    
081    @Override
082    public String toString(UserIdentity value)
083    {
084        return UserIdentity.userIdentityToString(value);
085    }
086    
087    @SuppressWarnings("unchecked")
088    public Object fromJSONForClient(Object json)
089    {
090        if (json == null)
091        {
092            return json;
093        }
094        else if (json instanceof Map)
095        {
096            return _helper.json2userIdentity((Map<String, Object>) json);
097        }
098        else if (json instanceof List)
099        {
100            List<UserIdentity> users = new ArrayList<>();
101            for (Map<String, Object> singleJson : (List<Map<String, Object>>) json)
102            {
103                users.add(_helper.json2userIdentity(singleJson));
104            }
105            return users.toArray(new UserIdentity[users.size()]);
106        }
107        else
108        {
109            throw new IllegalArgumentException("Try to convert the non user JSON object '" + json + "' into a user");
110        }
111    }
112    
113    @Override
114    protected Object _singleValueToJSON(UserIdentity value, DataContext context)
115    {
116        return _helper.user2json(value);
117    }
118    
119    @Override
120    protected UserIdentity _singleValueFromXML(Element element, Optional<Object> additionalData) throws TransformerException, IOException
121    {
122        String login = element.getAttribute("login");
123        String populationId = element.getAttribute("populationId");
124
125        if (StringUtils.isNoneEmpty(login, populationId))
126        {
127            return new UserIdentity(login, populationId);
128        }
129        
130        return null;
131    }
132    
133    @Override
134    protected void _singleValueToSAX(ContentHandler contentHandler, String tagName, UserIdentity value, Optional<ViewItem> viewItem, DataContext context, AttributesImpl attributes) throws SAXException
135    {
136        User user = _userManager.getUser(value);
137        if (user != null)
138        {
139            AttributesImpl localAttributes = new AttributesImpl(attributes);
140            localAttributes.addCDATAAttribute("login", value.getLogin());
141            localAttributes.addCDATAAttribute("populationId", value.getPopulationId());
142            localAttributes.addCDATAAttribute("email", user.getEmail());
143            XMLUtils.createElement(contentHandler, tagName, localAttributes, user.getFullName());
144        }
145    }
146    
147    public boolean isSimple()
148    {
149        return true;
150    }
151    
152    @Override
153    protected boolean _useJSONForEdition()
154    {
155        return true;
156    }
157}