319 lines
10 KiB
Java
319 lines
10 KiB
Java
package org.gcube.portlets.user.workspace.server.util;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
import org.apache.log4j.Logger;
|
|
import org.gcube.datacatalogue.ckanutillibrary.CKanUtils;
|
|
import org.gcube.datacatalogue.ckanutillibrary.models.RolesIntoOrganization;
|
|
import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader;
|
|
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataType;
|
|
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField;
|
|
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
|
|
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataValidator;
|
|
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataVocabulary;
|
|
import org.gcube.portlets.user.workspace.client.model.InfoContactModel;
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.LicensesBean;
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataProfileBean;
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetaDataTypeWrapper;
|
|
import org.gcube.portlets.widgets.ckandatapublisherwidget.shared.MetadataFieldWrapper;
|
|
import org.gcube.vomanagement.usermanagement.UserManager;
|
|
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
|
|
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
|
|
import org.gcube.vomanagement.usermanagement.impl.LiferayUserManager;
|
|
import org.gcube.vomanagement.usermanagement.model.GCubeUser;
|
|
|
|
import eu.trentorise.opendata.jackan.model.CkanLicense;
|
|
|
|
|
|
/**
|
|
* The Class UserUtil.
|
|
*
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* Nov 17, 2015
|
|
*/
|
|
public class UserUtil {
|
|
|
|
static UserManager um = new LiferayUserManager();
|
|
protected static Logger logger = Logger.getLogger(UserUtil.class);
|
|
|
|
|
|
/**
|
|
* Gets the user full name.
|
|
*
|
|
* @param portalLogin the portal login
|
|
* @return the user full name if is available, the input parameter portalLogin otherwise
|
|
*/
|
|
public static String getUserFullName(String portalLogin){
|
|
// return user.getPortalLogin(); //for testing in eclipse
|
|
|
|
// logger.trace("Finding full name for: "+portalLogin);
|
|
|
|
if(portalLogin==null)
|
|
return "";
|
|
|
|
if (WsUtil.isWithinPortal()) { //INTO PORTAL
|
|
|
|
GCubeUser curr = null;
|
|
|
|
try {
|
|
try {
|
|
curr = um.getUserByScreenName(portalLogin);
|
|
|
|
} catch (UserManagementSystemException e) {
|
|
|
|
logger.error("An error occurred in getUserFullName "+e,e);
|
|
} catch (UserRetrievalFault e) {
|
|
|
|
logger.error("An error occurred in getUserFullName "+e,e);
|
|
}
|
|
}catch (Exception e) {
|
|
logger.error("An error occurred in getUserFullName "+e,e);
|
|
logger.warn("Return portal login "+portalLogin);
|
|
return portalLogin;
|
|
}
|
|
|
|
if (curr != null){
|
|
// logger.trace("Return "+curr.getFullname() +" full name for: "+portalLogin);
|
|
return curr.getFullname();
|
|
}
|
|
}else{
|
|
logger.trace("DEVELOPEMENT MODE ON");
|
|
logger.trace("Returning input login: "+portalLogin);
|
|
return portalLogin;
|
|
}
|
|
|
|
logger.trace("Return portal login as full name for: "+portalLogin);
|
|
return portalLogin;
|
|
}
|
|
|
|
/**
|
|
* Gets the list login by info contact model.
|
|
*
|
|
* @param listContacts the list contacts
|
|
* @return the list login by info contact model
|
|
*/
|
|
public static List<String> getListLoginByInfoContactModel(List<InfoContactModel> listContacts){
|
|
|
|
List<String> listUsers = new ArrayList<String>();
|
|
|
|
for (InfoContactModel infoContactModel : listContacts) {
|
|
listUsers.add(infoContactModel.getLogin());
|
|
}
|
|
|
|
return listUsers;
|
|
}
|
|
|
|
/**
|
|
* Separate users names to comma.
|
|
*
|
|
* @param listContacts the list contacts
|
|
* @return the string
|
|
*/
|
|
public static String separateUsersNamesToComma(List<InfoContactModel> listContacts){
|
|
|
|
String users = "";
|
|
|
|
for (int i = 0; i < listContacts.size()-1; i++) {
|
|
users+= listContacts.get(i).getName() + ", ";
|
|
}
|
|
|
|
if(listContacts.size()>1)
|
|
users += listContacts.get(listContacts.size()-1).getName();
|
|
|
|
return users;
|
|
}
|
|
|
|
|
|
/**
|
|
* Separate full name to comma for portal login.
|
|
*
|
|
* @param listLogin the list login
|
|
* @return the string
|
|
*/
|
|
public static String separateFullNameToCommaForPortalLogin(List<String> listLogin){
|
|
|
|
String users = "";
|
|
|
|
logger.trace("SeparateFullNameToCommaForPortalLogin converting: "+listLogin);
|
|
|
|
//N-1 MEMBERS
|
|
for (int i = 0; i < listLogin.size()-1; i++) {
|
|
// logger.trace("Converting: "+i+") "+listLogin.get(i));
|
|
users+= getUserFullName(listLogin.get(i)) + ", ";
|
|
}
|
|
|
|
//LAST MEMBER
|
|
if(listLogin.size()>=1){
|
|
// logger.trace("Converting: "+(listLogin.size()-1)+") " +listLogin.get(listLogin.size()-1));
|
|
users += getUserFullName(listLogin.get(listLogin.size()-1));
|
|
}
|
|
|
|
logger.trace("SeparateFullNameToCommaForPortalLogin returning: "+users);
|
|
|
|
return users;
|
|
}
|
|
|
|
/**
|
|
* The main method.
|
|
*
|
|
* @param args the arguments
|
|
*/
|
|
public static void main(String[] args) {
|
|
List<String> login = new ArrayList<String>();
|
|
// login.add("ale");
|
|
// login.add("pepe");
|
|
System.out.println(separateFullNameToCommaForPortalLogin(login));
|
|
|
|
}
|
|
|
|
/**
|
|
* Load the licenses list and put them into the asl session (the publisher widget will use it)
|
|
* @param session
|
|
* @param ckanLicensesKey
|
|
* @param ckanUtils
|
|
*/
|
|
public static void getLicenses(HttpSession session, String username, String ckanLicensesKey, CKanUtils ckanUtils) {
|
|
|
|
try{
|
|
logger.debug("User in session is " + username);
|
|
List<CkanLicense> titlesLicenses = ckanUtils.getLicenses();
|
|
List<String> titles = new ArrayList<String>();
|
|
List<String> urls = new ArrayList<String>();
|
|
for (CkanLicense license : titlesLicenses) {
|
|
titles.add(license.getTitle());
|
|
String url = (license.getUrl() != null && !license.getUrl().isEmpty()) ? license.getUrl() : "";
|
|
urls.add(url);
|
|
}
|
|
LicensesBean licensesBean = new LicensesBean(titles, urls);
|
|
|
|
session.setAttribute(ckanLicensesKey, licensesBean);
|
|
logger.info("List of licenses has been saved into session" + licensesBean);
|
|
}
|
|
catch(Exception e){
|
|
logger.error("Failed to preload licenses list", e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Load the list of organizations in which he can publish and put them into the asl session (the publisher widget will use it)
|
|
* @param session
|
|
* @param ckanOrganizationsPublishKey
|
|
* @param ckanUtils
|
|
*/
|
|
public static void getUserOrganizationsList(HttpSession session, String username, boolean isSysAdmin,
|
|
String ckanOrganizationsPublishKey, CKanUtils ckanUtils, String token) {
|
|
|
|
try{
|
|
logger.debug("Request for user " + username + " organizations list");
|
|
List<String> orgsName = new ArrayList<String>();
|
|
|
|
if(isSysAdmin){
|
|
|
|
logger.info("The user " + username + " is a sysadmin. He can publish everywhere");
|
|
orgsName = ckanUtils.getOrganizationsNames(); // get all organizations' names
|
|
|
|
}else{
|
|
|
|
// We need to retrieve orgs in which the user has the roles ADMIN or EDITOR
|
|
List<RolesIntoOrganization> rolesToMatch = new ArrayList<RolesIntoOrganization>();
|
|
rolesToMatch.add(RolesIntoOrganization.EDITOR);
|
|
rolesToMatch.add(RolesIntoOrganization.ADMIN);
|
|
|
|
Map<String, List<RolesIntoOrganization>> orgsAndRoles = ckanUtils.getGroupsAndRolesByUser(username, rolesToMatch);
|
|
logger.debug("Result is " + orgsAndRoles);
|
|
Iterator<Entry<String, List<RolesIntoOrganization>>> iterator = orgsAndRoles.entrySet().iterator();
|
|
|
|
// get the names
|
|
while (iterator.hasNext()) {
|
|
Map.Entry<String, List<RolesIntoOrganization>> entry = (Map.Entry<String, List<RolesIntoOrganization>>) iterator
|
|
.next();
|
|
orgsName.add(entry.getKey());
|
|
logger.debug("The user has a role ADMIN/EDITOR into org " + entry.getKey());
|
|
}
|
|
}
|
|
session.setAttribute(ckanOrganizationsPublishKey, orgsName);
|
|
logger.info("Organizations name for user " + username + " has been saved into session");
|
|
}catch(Exception e){
|
|
logger.error("Failed to preload list of organizations in which the user can publish", e);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Load the list of product profiles and put them into the asl session (the publisher widget will use it)
|
|
* @param session
|
|
* @param ckanOrganizationsPublishKey
|
|
* @param ckanUtils
|
|
*/
|
|
public static void getMetadataProfilesList(HttpSession session, String username,
|
|
String ckanProfilesKey, CKanUtils ckanUtils) {
|
|
|
|
try{
|
|
|
|
logger.debug("User in session is " + username);
|
|
|
|
List<MetaDataProfileBean> beans = new ArrayList<MetaDataProfileBean>();
|
|
|
|
try {
|
|
|
|
DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader();
|
|
|
|
for (MetadataType mt : reader.getListOfMetadataTypes()) {
|
|
MetadataFormat metadata = reader.getMetadataFormatForMetadataType(mt);
|
|
|
|
// we need to wrap the list of metadata
|
|
List<MetadataFieldWrapper> wrapperList = new ArrayList<MetadataFieldWrapper>();
|
|
List<MetadataField> toWrap = metadata.getMetadataFields();
|
|
for(MetadataField metadataField: toWrap){
|
|
|
|
MetadataFieldWrapper wrapperObj = new MetadataFieldWrapper();
|
|
wrapperObj.setDefaulValue(metadataField.getDefaulValue());
|
|
wrapperObj.setFieldName(metadataField.getFieldName());
|
|
wrapperObj.setIsBoolean(metadataField.getIsBoolean());
|
|
wrapperObj.setMandatory(metadataField.getMandatory());
|
|
wrapperObj.setNote(metadataField.getNote());
|
|
|
|
MetadataValidator validator = metadataField.getValidator();
|
|
if(validator != null)
|
|
wrapperObj.setValidator(validator.getRegularExpression());
|
|
|
|
MetadataVocabulary vocabulary = metadataField.getVocabulary();
|
|
if(vocabulary != null)
|
|
wrapperObj.setVocabulary(vocabulary.getVocabularyFields());
|
|
|
|
// add to the list
|
|
wrapperList.add(wrapperObj);
|
|
|
|
}
|
|
|
|
// wrap the mt as well
|
|
MetaDataTypeWrapper typeWrapper = new MetaDataTypeWrapper();
|
|
typeWrapper.setDescription(mt.getDescription());
|
|
typeWrapper.setId(mt.getId());
|
|
typeWrapper.setName(mt.getName());
|
|
MetaDataProfileBean bean = new MetaDataProfileBean(typeWrapper, wrapperList);
|
|
beans.add(bean);
|
|
}
|
|
|
|
logger.info("List of beans is " + beans);
|
|
session.setAttribute(ckanProfilesKey, beans);
|
|
logger.debug("List of profiles has been saved into session");
|
|
|
|
} catch (Exception e) {
|
|
logger.error("Error while retrieving metadata beans ", e);
|
|
}
|
|
}
|
|
catch(Exception e){
|
|
logger.error("Failed to retrieve the list of product profiles", e);
|
|
}
|
|
|
|
}
|
|
|
|
}
|