package org.gcube.portlets.user.geoportaldataviewer.server; import static org.gcube.application.geoportal.client.GeoportalAbstractPlugin.statefulMongoConcessioni; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.gcube.application.geoportal.client.legacy.ConcessioniManagerI; import org.gcube.application.geoportal.common.model.legacy.Concessione; import org.gcube.application.geoportal.common.model.legacy.UploadedImage; import org.gcube.application.geoportalcommon.ConvertToDataViewModel; import org.gcube.application.geoportalcommon.shared.products.model.UploadedImageDV; import org.gcube.portlets.user.geoportaldataviewer.server.util.SessionUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * The Class ConcessioneImageUtil. * * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * * Sep 7, 2021 */ public class ConcessioneImageUtil { private static final Logger LOG = LoggerFactory.getLogger(ConcessioneImageUtil.class); /** * Gets the uploaded images for id. * * @param httpServletRequest the http servlet request * @param itemType the item type * @param itemId the item id * @param maxImages the max images * @return the uploaded images for id * @throws Exception the exception */ public List getUploadedImagesForId(HttpServletRequest httpServletRequest, String itemType, String itemId, Integer maxImages) throws Exception { LOG.info("getUploadedImagesForId [itemId: " + itemId + ", itemType: " + itemType + "] called"); if (itemType == null) throw new Exception("Invalid parameter. The itemType is null"); if (itemId == null) throw new Exception("Invalid parameter. The itemId is null"); List listUI = null; try { if (itemType.equalsIgnoreCase("concessione")) { LOG.info("Trying to get concessione for id " + itemId); SessionUtil.getCurrentContext(httpServletRequest, true); SessionUtil.getCurrentToken(httpServletRequest, true); ConcessioniManagerI concessioniManager = statefulMongoConcessioni().build(); Concessione concessione = concessioniManager.getById(itemId); if (concessione != null) { LOG.info("For id " + itemId + ", got concessione " + concessione.getNome() + " from service"); List images = concessione.getImmaginiRappresentative(); if (images != null) { listUI = new ArrayList(); int max = maxImages < images.size() ? maxImages : images.size(); for (int i = 0; i < max; i++) { UploadedImageDV ui = ConvertToDataViewModel.toUploadedImage(images.get(i)); listUI.add(ui); } LOG.info("For id " + itemId + ", got " + listUI.size() + " image/s"); } } else throw new Exception("Concessione with id '" + itemId + "' not available"); } return listUI; } catch (Exception e) { String erroMsg = UploadedImage.class.getSimpleName() + " not available for " + Concessione.class.getSimpleName() + " with id " + itemId; LOG.error(erroMsg, e); throw new Exception(erroMsg); } } }