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 getListLoginByInfoContactModel(List listContacts){ List listUsers = new ArrayList(); 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 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 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 login = new ArrayList(); // 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 titlesLicenses = ckanUtils.getLicenses(); List titles = new ArrayList(); List urls = new ArrayList(); 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 orgsName = new ArrayList(); 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 List rolesToMatch = new ArrayList(); rolesToMatch.add(RolesIntoOrganization.ADMIN); Map> orgsAndRoles = ckanUtils.getGroupsAndRolesByUser(username, rolesToMatch); logger.debug("Result is " + orgsAndRoles); Iterator>> iterator = orgsAndRoles.entrySet().iterator(); // get the names while (iterator.hasNext()) { Map.Entry> entry = (Map.Entry>) iterator .next(); orgsName.add(entry.getKey()); logger.debug("The user has a role ADMIN 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 beans = new ArrayList(); try { DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader(); for (MetadataType mt : reader.getListOfMetadataTypes()) { MetadataFormat metadata = reader.getMetadataFormatForMetadataType(mt); // we need to wrap the list of metadata List wrapperList = new ArrayList(); List 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); } } }