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