upload manager work

This commit is contained in:
Branislav Stojkovic
2016-10-30 12:26:45 +01:00
parent 1849eec874
commit 98087d8889
12 changed files with 244 additions and 53 deletions
@@ -97,11 +97,13 @@ public interface IServiceAPI {
* @param page
* @param itemsPerPage
* @param sort
* @param username
* @return string with error message if the error occurred or a list of {@link ProjectEntity}
* objects.
*/
Object search(@ParamName("criteria") String criteria, @ParamName("category") String category,
@ParamName("page") Integer page, @ParamName("itemsPerPage") Integer itemsPerPage, @ParamName("sort") String sort);
@ParamName("page") Integer page, @ParamName("itemsPerPage") Integer itemsPerPage, @ParamName("sort") String sort,
@ParamName("username") String username);
/**
* Uploads a project using token to authenticate. When projectI is specified, it updates the
@@ -126,9 +128,9 @@ public interface IServiceAPI {
@ParamName("diylcVersion") String diylcVersion, @ParamName("keywords") String keywords,
@ParamName("thumbnail") File thumbnail, @ParamName("project") File project,
@ParamName("projectId") Integer projectId);
/**
*Deletes an existing project. The project must belong to the user
* Deletes an existing project. The project must belong to the user
*
* @param username
* @param token
@@ -134,8 +134,8 @@ public class CloudPresenter {
return categories;
}
public void uploadProject(String projectName, String category, String description, String keywords, String diylcVersion,
File thumbnail, File project) throws IOException, CloudException {
public void uploadProject(String projectName, String category, String description, String keywords,
String diylcVersion, File thumbnail, File project) throws IOException, CloudException {
String username = ConfigurationManager.getInstance().readString(USERNAME_KEY, null);
String token = ConfigurationManager.getInstance().readString(TOKEN_KEY, null);
@@ -234,37 +234,54 @@ public class CloudPresenter {
throws CloudException {
LOG.info(String.format("search(%1$s,%2$s,%3$s,%4$d,%5$d)", criteria, category, sortOrder, pageNumber, itemsPerPage));
try {
Object res = getService().search(criteria, category, pageNumber, itemsPerPage, sortOrder);
// Thread.sleep(2000);
if (res == null)
throw new CloudException("Failed to retreive search results.");
if (res instanceof String)
throw new CloudException(res.toString());
if (res instanceof List<?>) {
@SuppressWarnings("unchecked")
List<ProjectEntity> projects = (List<ProjectEntity>) res;
LOG.info("Received " + projects.size() + " results. Downloading thumbnails...");
// Download thumbnails and replace urls with local paths to speed up loading in the main
// thread
for (int i = 0; i < projects.size(); i++) {
String url = projects.get(i).getThumbnailUrl();
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
File temp = File.createTempFile("thumbnail", ".png");
FileOutputStream fos = new FileOutputStream(temp);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
projects.get(i).setThumbnailUrl(temp.getAbsolutePath());
fos.close();
}
LOG.info("Finished downloading thumbnails.");
return projects;
}
throw new CloudException("Unexpected server response received for search results: " + res.getClass().getName());
Object res = getService().search(criteria, category, pageNumber, itemsPerPage, sortOrder, null);
return processResults(res);
} catch (CloudException ce) {
throw ce;
} catch (Exception e) {
throw new CloudException(e);
}
}
public List<ProjectEntity> fetchUserUploads() throws CloudException {
UserEntity user = getUserDetails();
try {
Object res = getService().search("", "", 1, Integer.MAX_VALUE, getSortings()[0], user.getUsername());
return processResults(res);
} catch (CloudException ce) {
throw ce;
} catch (Exception e) {
throw new CloudException(e);
}
}
private List<ProjectEntity> processResults(Object res) throws CloudException, IOException {
if (res == null)
throw new CloudException("Failed to retreive search results.");
if (res instanceof String)
throw new CloudException(res.toString());
if (res instanceof List<?>) {
@SuppressWarnings("unchecked")
List<ProjectEntity> projects = (List<ProjectEntity>) res;
LOG.info("Received " + projects.size() + " results. Downloading thumbnails...");
// Download thumbnails and replace urls with local paths to speed up loading in the main
// thread
for (int i = 0; i < projects.size(); i++) {
String url = projects.get(i).getThumbnailUrl();
URL website = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
File temp = File.createTempFile("thumbnail", ".png");
FileOutputStream fos = new FileOutputStream(temp);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
projects.get(i).setThumbnailUrl(temp.getAbsolutePath());
fos.close();
}
LOG.info("Finished downloading thumbnails.");
return projects;
}
throw new CloudException("Unexpected server response received for search results: " + res.getClass().getName());
}
public String[] getSortings() throws CloudException {
try {
return getService().getSortings().toArray(new String[0]);
@@ -17,7 +17,12 @@ public class PagingProvider {
private List<ProjectEntity> currentResults;
public PagingProvider(CloudPresenter cloudPresenter) {
this(cloudPresenter, 10);
}
public PagingProvider(CloudPresenter cloudPresenter, int itemsPerPage) {
this.cloudPresenter = cloudPresenter;
this.itemsPerPage = itemsPerPage;
}
public List<ProjectEntity> startSession(String searchFor, String category, String sort) throws CloudException {