This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

API - read Items: avoid reading profile for each items

  • Loading branch information...
bastiensaquet committed Oct 16, 2015
1 parent c742813 commit 9ee2e6b2181865f2e2f202c9bf6ec6ea9ae9b9cd
@@ -0,0 +1 @@
/global-15.st
@@ -21,6 +21,7 @@
import de.mpg.imeji.logic.vo.Album;
import de.mpg.imeji.logic.vo.Item;
import de.mpg.imeji.logic.vo.User;
import de.mpg.imeji.rest.helper.ProfileCache;
import de.mpg.imeji.rest.process.CommonUtils;
import de.mpg.imeji.rest.process.TransferObjectFactory;
import de.mpg.imeji.rest.to.AlbumTO;
@@ -57,14 +58,15 @@ public AlbumTO apply(Album vo) {
public List<ItemTO> readItems(String id, User u, String q) throws ImejiException {
AlbumController cc = new AlbumController();
return Lists.transform(cc.retrieveItems(id, u, q), new Function<Item, ItemTO>() {
@Override
public ItemTO apply(Item vo) {
ItemTO to = new ItemTO();
TransferObjectFactory.transferItem(vo, to);
return to;
}
});
ProfileCache profileCache = new ProfileCache();
List<ItemTO> tos = new ArrayList<>();
for (Item vo : cc.retrieveItems(id, u, q)) {
ItemTO to = new ItemTO();
TransferObjectFactory.transferItem(vo, to,
profileCache.read(vo.getMetadataSet().getProfile()));
tos.add(to);
}
return tos;
}
@Override
@@ -6,6 +6,7 @@
import static de.mpg.imeji.rest.process.ReverseTransferObjectFactory.TRANSFER_MODE.UPDATE;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
@@ -25,6 +26,7 @@
import de.mpg.imeji.logic.vo.MetadataProfile;
import de.mpg.imeji.logic.vo.User;
import de.mpg.imeji.rest.defaultTO.DefaultItemTO;
import de.mpg.imeji.rest.helper.ProfileCache;
import de.mpg.imeji.rest.process.CommonUtils;
import de.mpg.imeji.rest.process.TransferObjectFactory;
import de.mpg.imeji.rest.to.CollectionProfileTO;
@@ -67,14 +69,15 @@ public CollectionTO read(String id, User u) throws ImejiException {
*/
public List<ItemTO> readItems(String id, User u, String q) throws ImejiException {
CollectionController cc = new CollectionController();
return Lists.transform(cc.retrieveItems(id, u, q), new Function<Item, ItemTO>() {
@Override
public ItemTO apply(Item vo) {
ItemTO to = new ItemTO();
TransferObjectFactory.transferItem(vo, to);
return to;
}
});
ProfileCache profileCache = new ProfileCache();
List<ItemTO> tos = new ArrayList<>();
for (Item vo : cc.retrieveItems(id, u, q)) {
ItemTO to = new ItemTO();
TransferObjectFactory.transferItem(vo, to,
profileCache.read(vo.getMetadataSet().getProfile()));
tos.add(to);
}
return tos;
}
/**
@@ -89,14 +92,15 @@ public ItemTO apply(Item vo) {
*/
public Object readDefaultItems(String id, User u, String q) throws ImejiException {
CollectionController cc = new CollectionController();
return Lists.transform(cc.retrieveItems(id, u, q), new Function<Item, DefaultItemTO>() {
@Override
public DefaultItemTO apply(Item vo) {
DefaultItemTO to = new DefaultItemTO();
TransferObjectFactory.transferDefaultItem(vo, to);
return to;
}
});
ProfileCache profileCache = new ProfileCache();
List<DefaultItemTO> tos = new ArrayList<>();
for (Item vo : cc.retrieveItems(id, u, q)) {
DefaultItemTO to = new DefaultItemTO();
TransferObjectFactory.transferDefaultItem(vo, to,
profileCache.read(vo.getMetadataSet().getProfile()));
tos.add(to);
}
return tos;
}
public List<CollectionTO> readAll(User u, String q) throws ImejiException {
@@ -152,9 +156,9 @@ public CollectionTO update(CollectionTO to, User u) throws ImejiException {
CollectionImeji vo = getCollectionVO(cc, to.getId(), u);
MetadataProfile originalMp = pc.retrieve(vo.getProfile(), u);
String hasStatements =
originalMp.getStatements().size() > 0 ? " Existing metadata profile has already defined metadata elements. It is not allowed to update it: remove the profileId from your input."
: "";
String hasStatements = originalMp.getStatements().size() > 0
? " Existing metadata profile has already defined metadata elements. It is not allowed to update it: remove the profileId from your input."
: "";
// profile is defined
CollectionProfileTO profTO = to.getProfile();
@@ -5,21 +5,20 @@
import static de.mpg.imeji.rest.process.ReverseTransferObjectFactory.TRANSFER_MODE.UPDATE;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ObjectUtils;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import de.mpg.imeji.exceptions.BadRequestException;
import de.mpg.imeji.exceptions.ImejiException;
import de.mpg.imeji.logic.controller.ItemController;
import de.mpg.imeji.logic.util.ObjectHelper;
import de.mpg.imeji.logic.vo.Item;
import de.mpg.imeji.logic.vo.User;
import de.mpg.imeji.rest.defaultTO.DefaultItemTO;
import de.mpg.imeji.rest.helper.ProfileCache;
import de.mpg.imeji.rest.process.ReverseTransferObjectFactory;
import de.mpg.imeji.rest.process.TransferObjectFactory;
import de.mpg.imeji.rest.to.ItemTO;
@@ -40,12 +39,13 @@ public ItemTO create(ItemTO to, User u) throws ImejiException {
ReverseTransferObjectFactory.transferItem(to, item, u, CREATE);
item =
controller.create(item, ((ItemWithFileTO) to).getFile(), filename, u,
((ItemWithFileTO) to).getFetchUrl(), ((ItemWithFileTO) to).getReferenceUrl());
item = controller.create(item, ((ItemWithFileTO) to).getFile(), filename, u,
((ItemWithFileTO) to).getFetchUrl(), ((ItemWithFileTO) to).getReferenceUrl());
// transfer item into ItemTO
ItemTO itemTO = new ItemTO();
TransferObjectFactory.transferItem(item, itemTO);
ProfileCache profileCache = new ProfileCache();
TransferObjectFactory.transferItem(item, itemTO,
profileCache.read(item.getMetadataSet().getProfile()));
return itemTO;
} else {
throw new BadRequestException(
@@ -56,41 +56,46 @@ public ItemTO create(ItemTO to, User u) throws ImejiException {
public DefaultItemTO readDefault(String id, User u) throws ImejiException {
DefaultItemTO defaultTO = new DefaultItemTO();
Item item = controller.retrieve(ObjectHelper.getURI(Item.class, id), u);
TransferObjectFactory.transferDefaultItem(item, defaultTO);
ProfileCache profileCache = new ProfileCache();
TransferObjectFactory.transferDefaultItem(item, defaultTO,
profileCache.read(item.getMetadataSet().getProfile()));
return defaultTO;
}
@Override
public ItemTO read(String id, User u) throws ImejiException {
Item item = controller.retrieve(ObjectHelper.getURI(Item.class, id), u);
ItemTO to = new ItemTO();
TransferObjectFactory.transferItem(item, to);
ProfileCache profileCache = new ProfileCache();
TransferObjectFactory.transferItem(item, to,
profileCache.read(item.getMetadataSet().getProfile()));
return to;
}
public List<ItemTO> readItems(User u, String q) throws ImejiException, IOException {
return Lists.transform(new ItemController().retrieve(u, q, null), new Function<Item, ItemTO>() {
@Override
public ItemTO apply(Item vo) {
ItemTO to = new ItemTO();
TransferObjectFactory.transferItem(vo, to);
return to;
}
});
ProfileCache profileCache = new ProfileCache();
List<ItemTO> tos = new ArrayList<>();
for (Item vo : new ItemController().retrieve(u, q, null)) {
ItemTO to = new ItemTO();
TransferObjectFactory.transferItem(vo, to,
profileCache.read(vo.getMetadataSet().getProfile()));
tos.add(to);
}
return tos;
}
public List<DefaultItemTO> readDefaultItems(User u, String q) throws ImejiException, IOException {
return Lists.transform(new ItemController().retrieve(u, q, null),
new Function<Item, DefaultItemTO>() {
public DefaultItemTO apply(Item item) {
DefaultItemTO defaultTO = new DefaultItemTO();
TransferObjectFactory.transferDefaultItem(item, defaultTO);
return defaultTO;
}
});
ProfileCache profileCache = new ProfileCache();
List<DefaultItemTO> tos = new ArrayList<>();
for (Item vo : new ItemController().retrieve(u, q, null)) {
DefaultItemTO to = new DefaultItemTO();
TransferObjectFactory.transferDefaultItem(vo, to,
profileCache.read(vo.getMetadataSet().getProfile()));
tos.add(to);
}
return tos;
}
@@ -103,15 +108,16 @@ public ItemTO update(ItemTO to, User u) throws ImejiException {
if (tof.getFile() != null) {
item = controller.updateFile(item, tof.getFile(), to.getFilename(), u);
} else if (!isNullOrEmpty(url)) {
item =
controller.updateWithExternalFile(item, getExternalFileUrl(tof), to.getFilename(),
downloadFile(tof), u);
item = controller.updateWithExternalFile(item, getExternalFileUrl(tof), to.getFilename(),
downloadFile(tof), u);
}
} else {
item = controller.update(item, u);
}
to = new ItemTO();
TransferObjectFactory.transferItem(item, to);
ProfileCache profileCache = new ProfileCache();
TransferObjectFactory.transferItem(item, to,
profileCache.read(item.getMetadataSet().getProfile()));
return to;
}
@@ -0,0 +1,58 @@
package de.mpg.imeji.rest.helper;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import de.mpg.imeji.logic.vo.MetadataProfile;
import de.mpg.imeji.rest.api.ProfileService;
/**
* This class is a cache to read profiles, to avoid to read too much the profiles
*
* @author bastiens
*
*/
public class ProfileCache {
private Map<String, MetadataProfile> profiles = new HashMap<>();
private ProfileService profileService = new ProfileService();
private static final Logger logger = Logger.getLogger(ProfileCache.class);
/**
* If the profile is cached, read it from cache, else, read it from the database and cache it
*
* @param uri
* @return
*/
public MetadataProfile read(URI uri) {
if (profiles.containsKey(uri.toString())) {
return profiles.get(uri.toString());
}
MetadataProfile profile = readFromDatabase(uri);
profiles.put(uri.toString(), profile);
return profile;
}
/**
* Read the profile from the database
*
* @param uri
* @return
*/
private MetadataProfile readFromDatabase(URI uri) {
try {
System.out.println("read from db " + uri);
return profileService.read(uri);
} catch (Exception e) {
logger.info("Something nasty happend after reading the profile", e);
return null;
}
}
}
@@ -26,7 +26,6 @@
import de.mpg.imeji.logic.vo.predefinedMetadata.Number;
import de.mpg.imeji.logic.vo.predefinedMetadata.Publication;
import de.mpg.imeji.logic.vo.predefinedMetadata.Text;
import de.mpg.imeji.rest.api.ProfileService;
import de.mpg.imeji.rest.api.UserService;
import de.mpg.imeji.rest.defaultTO.DefaultItemTO;
import de.mpg.imeji.rest.defaultTO.DefaultOrganizationTO;
@@ -255,7 +254,7 @@ public static void transferProperties(Properties vo, PropertiesTO to) {
* @param vo
* @param to
*/
public static void transferItem(Item vo, ItemTO to) {
public static void transferItem(Item vo, ItemTO to, MetadataProfile profile) {
transferProperties(vo, to);
// set visibility
to.setVisibility(vo.getVisibility().toString());
@@ -268,21 +267,11 @@ public static void transferItem(Item vo, ItemTO to) {
to.setWebResolutionUrlUrl(vo.getWebImageUrl());
to.setThumbnailUrl(vo.getThumbnailImageUrl());
to.setFileUrl(vo.getFullImageUrl());
// set Metadata
ProfileService pcrud = new ProfileService();
MetadataProfile profile = new MetadataProfile();
try {
profile = pcrud.read(vo.getMetadataSet().getProfile());
} catch (Exception e) {
// TODO Auto-generated catch block
LOGGER.info("Something nasty happend after reading the profile", e);
}
transferItemMetadata(profile, vo.getMetadataSet().getMetadata(), to);
}
public static void transferDefaultItem(Item vo, DefaultItemTO to) {
public static void transferDefaultItem(Item vo, DefaultItemTO to, MetadataProfile profile) {
transferProperties(vo, to);
// set visibility
to.setVisibility(vo.getVisibility().toString());
@@ -295,16 +284,6 @@ public static void transferDefaultItem(Item vo, DefaultItemTO to) {
to.setWebResolutionUrlUrl(vo.getWebImageUrl());
to.setThumbnailUrl(vo.getThumbnailImageUrl());
to.setFileUrl(vo.getFullImageUrl());
// set Metadata
ProfileService pcrud = new ProfileService();
MetadataProfile profile = new MetadataProfile();
try {
profile = pcrud.read(vo.getMetadataSet().getProfile());
} catch (Exception e) {
// TODO Auto-generated catch block
LOGGER.info("Something nasty happend after reading the profile", e);
}
transferItemMetadataDefault(profile, vo.getMetadataSet().getMetadata(), to);
}

0 comments on commit 9ee2e6b

Please sign in to comment.