storagehub-client-wrapper/src/main/java/org/gcube/common/storagehubwrapper/server/converter/HLMapper.java

294 lines
11 KiB
Java

/**
*
*/
package org.gcube.common.storagehubwrapper.server.converter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.gcube.common.storagehub.model.Metadata;
import org.gcube.common.storagehub.model.items.AbstractFileItem;
import org.gcube.common.storagehub.model.items.ExternalURL;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.GCubeItem;
import org.gcube.common.storagehub.model.items.GenericFileItem;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.common.storagehub.model.items.PDFFileItem;
import org.gcube.common.storagehub.model.items.SharedFolder;
import org.gcube.common.storagehub.model.items.TrashItem;
import org.gcube.common.storagehub.model.items.VreFolder;
import org.gcube.common.storagehub.model.items.nodes.Accounting;
import org.gcube.common.storagehub.model.items.nodes.Content;
import org.gcube.common.storagehub.model.items.nodes.ImageContent;
import org.gcube.common.storagehub.model.items.nodes.PDFContent;
import org.gcube.common.storagehub.model.items.nodes.accounting.AccountEntry;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItemType;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.AccountingEntry;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.FileItem;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.GcubeItem;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.ImageFile;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.PDFFile;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.PropertyMap;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.URLFile;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.WorkspaceFolder;
import org.gcube.common.storagehubwrapper.shared.tohl.impl.WorkspaceSharedFolder;
import org.gcube.common.storagehubwrapper.shared.tohl.items.FileItemType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class HLMapper.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Jun 20, 2018
*/
public class HLMapper {
private static Logger logger = LoggerFactory.getLogger(HLMapper.class);
public static Function<org.gcube.common.storagehub.model.items.nodes.accounting.AccountEntry, AccountingEntry> toAccountingEntry = new Function<org.gcube.common.storagehub.model.items.nodes.accounting.AccountEntry, AccountingEntry>() {
public AccountingEntry apply(org.gcube.common.storagehub.model.items.nodes.accounting.AccountEntry accountingEntry) {
return new AccountingEntry(
accountingEntry.getUser(),
accountingEntry.getDate(),
accountingEntry.getType().name(),
accountingEntry.getVersion(),
accountingEntry.getPrimaryType());
}
};
/**
* To workspace item.
*
* @param <T> the generic type
* @param item the item
* @return the t
*/
public static<T extends WorkspaceItem> T toWorkspaceItem(Item item){
return toWorkspaceItem(item, false, false, false);
}
/**
* To accounting entries.
*
* @param item the item
* @return the list
*/
public static List<org.gcube.common.storagehubwrapper.shared.tohl.AccountingEntry> toAccountingEntries(Item item){
List<org.gcube.common.storagehubwrapper.shared.tohl.AccountingEntry> accountingEntries = null;
Accounting accounting = item.getAccounting();
if(accounting!=null && accounting.getEntries().size()>0){
accountingEntries = new ArrayList<org.gcube.common.storagehubwrapper.shared.tohl.AccountingEntry>(accounting.getEntries().size());
for (AccountEntry ae : accounting.getEntries()) {
accountingEntries.add(toAccountingEntry.apply(ae));
}
}
return accountingEntries;
}
/**
* To property map.
*
* @param metadata the metadata
* @return the property map
*/
public static PropertyMap toPropertyMap(Metadata metadata){
PropertyMap pm = null;
if(metadata!=null)
pm = new PropertyMap(metadata.getValues());
return pm;
}
/**
* To workspace item.
*
* @param <T> the generic type
* @param item the item
* @param withAccounting the with accounting
* @param withFileDetails the with file details
* @param withMapProperties the with map properties
* @return the t
*/
public static<T extends WorkspaceItem> T toWorkspaceItem(Item item, boolean withAccounting, boolean withFileDetails, boolean withMapProperties){
List<org.gcube.common.storagehubwrapper.shared.tohl.AccountingEntry> accountingEntries = null;
if(withAccounting)
accountingEntries = toAccountingEntries(item);
org.gcube.common.storagehubwrapper.shared.tohl.impl.WorkspaceItem theItem = null;
WorkspaceItemType type = null;
boolean isFolder = false;
String itemName = item.getName();
boolean isRoot = false;
//THE ITEM IS A KIND OF FOLDER
if (item instanceof FolderItem){
isFolder = true;
FolderItem folderItem = (FolderItem) item; //??
theItem = new WorkspaceFolder();
type = WorkspaceItemType.FOLDER;
if (item instanceof SharedFolder || item instanceof VreFolder) {
SharedFolder sharedfolder = (SharedFolder) item; //??
theItem = new WorkspaceSharedFolder();
type = WorkspaceItemType.SHARED_FOLDER;
itemName = sharedfolder.getTitle(); //IN CASE OF SHARED FOLDER THE NAME IS AN UUID, I'M USING THE TITLE
if (sharedfolder.isVreFolder()){
//logger.debug("Converting shared folder: "+theItem.getClass());
//VreFolder vreFolder = (VreFolder) item;
//theItem = new WorkspaceVREFolder(); //NEVER INSTANCE THE WorkspaceVREFolder because VreFolder is never used by HL/StorageHub
itemName = sharedfolder.getDisplayName(); //IN CASE OF VRE FOLDER I'M USING THE DISPLAYNAME
((WorkspaceSharedFolder) theItem).setVreFolder(true);
type = WorkspaceItemType.VRE_FOLDER;
}
}
((WorkspaceFolder) theItem).setPublicFolder(folderItem.isPublicFolder());
if(logger.isTraceEnabled()){
WorkspaceFolder theFolder = (WorkspaceFolder) theItem;
logger.trace("Wrapped Folder: "+theFolder);
}
}
//THE ITEM IS A KIND OF FILE
if(item instanceof AbstractFileItem){
theItem = new FileItem();
type = WorkspaceItemType.FILE_ITEM;
FileItemType fileItemType = null;
if(item instanceof ExternalURL){
ExternalURL eURL = (ExternalURL) item; //??
theItem = new URLFile();
fileItemType = FileItemType.URL_DOCUMENT;
}else if(item instanceof GenericFileItem){
GenericFileItem gFI = (GenericFileItem) item; //??
fileItemType = FileItemType.DOCUMENT;
}else if(item instanceof PDFFileItem){
theItem = new PDFFile();
fileItemType = FileItemType.PDF_DOCUMENT;
if(withFileDetails){
PDFFileItem pdfFI = (PDFFileItem) item;
PDFContent pdfContent = pdfFI.getContent();
PDFFile thePDFFileItem = (PDFFile) theItem;
thePDFFileItem.setNumberOfPages(pdfContent.getNumberOfPages());
thePDFFileItem.setProducer(pdfContent.getProducer());
thePDFFileItem.setVersion(pdfContent.getVersion());
thePDFFileItem.setTitle(pdfContent.getTitle());
thePDFFileItem.setAuthor(pdfContent.getAuthor());
}
}else if(item instanceof org.gcube.common.storagehub.model.items.ImageFile){
theItem = new ImageFile();
fileItemType = FileItemType.IMAGE_DOCUMENT;
if(withFileDetails){
org.gcube.common.storagehub.model.items.ImageFile imgFI = (org.gcube.common.storagehub.model.items.ImageFile) item; //??
ImageFile theImageFileItem = (ImageFile) theItem;
ImageContent content = imgFI.getContent();
theImageFileItem.setHeight(content.getHeight());
theImageFileItem.setWidth(content.getWidth());
theImageFileItem.setThumbnailWidth(content.getThumbnailHeight());
theImageFileItem.setThumbnailHeight(content.getThumbnailHeight());
}
}
AbstractFileItem itemAb = (AbstractFileItem) item;
Content aC = itemAb.getContent();
FileItem theFileItem = (FileItem) theItem;
theFileItem.setFileItemType(fileItemType);
if(aC!=null){
logger.warn("The content is null for item: "+itemAb.getId());
theFileItem.setMimeType(aC.getMimeType());
theFileItem.setSize(aC.getSize());
}
//TODO ADD VERSION NAME
}
//ADDING (GCUBE) PROPERTIES
PropertyMap pm = null;
if(withMapProperties){
pm = toPropertyMap(item.getPropertyMap());
}
//CONVERTING TRASH ITEM
if(item.isTrashed()){
type = WorkspaceItemType.TRASH_ITEM;
TrashItem trashItem = (TrashItem) item; //??
theItem = new org.gcube.common.storagehubwrapper.shared.tohl.impl.TrashItem();
org.gcube.common.storagehubwrapper.shared.tohl.impl.TrashItem theTrashItem = (org.gcube.common.storagehubwrapper.shared.tohl.impl.TrashItem) theItem;
theTrashItem.setDeletedBy(trashItem.getDeletedBy());
theTrashItem.setDeletedTime(trashItem.getDeletedTime());
theTrashItem.setOriginalParentId(trashItem.getOriginalParentId());
theTrashItem.setDeletedFrom(trashItem.getDeletedFrom());
theTrashItem.setLenght(trashItem.getLenght());
theTrashItem.setMimeType(trashItem.getMimeType());
isFolder = trashItem.getFolder(); //DO NOT MOVE THIS SET
logger.trace("Wrapped TrashItem: "+theTrashItem);
}
if(item instanceof GCubeItem){
type = WorkspaceItemType.FILE_ITEM;
GCubeItem gcubeItem = (GCubeItem) item; //??
theItem = new GcubeItem();
GcubeItem theGcubeItem = (GcubeItem) theItem;
theGcubeItem.setFileItemType(FileItemType.GCUBE_ITEM);
theGcubeItem.setScopes(gcubeItem.getScopes());
theGcubeItem.setCreator(gcubeItem.getCreator());
theGcubeItem.setItemType(gcubeItem.getItemType());
theGcubeItem.setShared(gcubeItem.isShared());
if(withMapProperties){
PropertyMap property = toPropertyMap(gcubeItem.getProperty());
theGcubeItem.setProperty(property);
}
logger.trace("Wrapped GcubeItem: "+theGcubeItem);
}
logger.trace("The item: "+item);
logger.trace("The item id: "+item.getId());
theItem.setId(item.getId());
theItem.setName(itemName);
theItem.setPath(item.getPath());
theItem.setParentId(item.getParentId());
theItem.setTrashed(item.isTrashed());
theItem.setShared(item.isShared());
theItem.setLocked(item.isLocked());
theItem.setTitle(item.getTitle());
theItem.setDescription(item.getDescription());
theItem.setLastModifiedBy(item.getLastModifiedBy());
theItem.setLastModificationTime(item.getLastModificationTime());
theItem.setCreationTime(item.getCreationTime());
theItem.setOwner(item.getOwner());
theItem.setHidden(item.isHidden());
theItem.setAccounting(accountingEntries);
theItem.setType(type);
theItem.setFolder(isFolder);
theItem.setRoot(isRoot); //IS ALWAYS FALSE
theItem.setPropertyMap(pm);
logger.trace("Wrapped WsItem: "+theItem);
return (T) theItem;
}
}