public static <T> PaginatedResult<T> buildPaginatedResult(Result result, ItemFactory<T> factory) { if (!result.isSuccessful()) { return new PaginatedResult<T>(0, 0, Collections.<T>emptyList()); } DomElement contentElement = result.getContentElement(); return buildPaginatedResult(contentElement, contentElement, factory); }
/** * Get the tags applied by an individual user to an album on Last.fm. * * @param artist The artist name in question * @param album The album name in question * @param session A Session instance * @return a list of tags */ public static Collection<String> getTags(String artist, String album, Session session) { Result result = Caller.getInstance().call("album.getTags", session, "artist", artist, "album", album); if (!result.isSuccessful()) return Collections.emptyList(); DomElement element = result.getContentElement(); Collection<String> tags = new ArrayList<String>(); for (DomElement domElement : element.getChildren("tag")) { tags.add(domElement.getChildText("name")); } return tags; }
public static PaginatedResult<User> getMembers(String group, int page, String apiKey) { Result result = Caller.getInstance() .call("group.getMembers", apiKey, "group", group, "page", String.valueOf(page)); if (!result.isSuccessful()) return new PaginatedResult<User>(0, 0, Collections.<User>emptyList()); DomElement root = result.getContentElement(); Collection<DomElement> children = root.getChildren("user"); List<User> users = new ArrayList<User>(children.size()); for (DomElement child : children) { users.add(User.userFromElement(child)); } page = Integer.parseInt(root.getAttribute("page")); int total = Integer.parseInt(root.getAttribute("totalPages")); return new PaginatedResult<User>(page, total, users); }
/** * Use the last.fm corrections data to check whether the supplied artist has a correction to a * canonical artist. This method returns a new {@link Artist} object containing the corrected * data, or <code>null</code> if the supplied Artist was not found. * * @param artist The artist name to correct * @return a new {@link Artist}, or <code>null</code> */ public static final Artist getCorrection(final Context context, final String artist) { Result result = null; try { result = Caller.getInstance(context).call("artist.getCorrection", "<key>", "artist", artist); if (!result.isSuccessful()) { return null; } final DomElement correctionElement = result.getContentElement().getChild("correction"); if (correctionElement == null) { return new Artist(artist, null); } final DomElement artistElem = correctionElement.getChild("artist"); return FACTORY.createItemFromElement(artistElem); } catch (final Exception ignored) { return null; } }
public static <T> Collection<T> buildCollection(Result result, ItemFactory<T> factory) { if (!result.isSuccessful()) return Collections.emptyList(); return buildCollection(result.getContentElement(), factory); }
public static <T> T buildItem(Result result, ItemFactory<T> factory) { if (!result.isSuccessful()) return null; return buildItem(result.getContentElement(), factory); }
private Result execute( COMMAND_TYPE commandType, Command command, SortedEventList openTasks, SortedEventList completeTasks) { // TODO Auto-generated method stub Result r = new Result(false, null, null, null); switch (commandType) { case ADD: r = openTasks.addEvent(command.getEventFromCommand()); r.setCommandType("add"); return r; case GET: r = openTasks.getEventWithId(command.getId()); r.setCommandType("get"); return r; case SET: r = openTasks.editEventWithId(command.getEventFromCommand(), command.getId()); r.setCommandType("set"); return r; case DELETEOPEN: r = openTasks.deleteEventWithId(command.getId()); r.setCommandType("deleteOpen"); return r; case DELETECOMPLETE: r = completeTasks.deleteEventWithId(command.getId()); r.setCommandType("deleteComplete"); return r; case GETOPEN: r = openTasks.getArrayList(); r.setCommandType("getOpen"); return r; case GETCOMPLETE: r = completeTasks.getArrayList(); r.setCommandType("getComplete"); return r; case SORT: r = openTasks.sortBy(command.getSortKeyWords()); r.setCommandType("sort"); return r; case SEARCHOPEN: r = openTasks.searchBy(command.getSearchKeyWords(), command.getSearchType()); r.setCommandType("SEARCHOPEN"); return r; case SEARCHCOMPLETE: r = completeTasks.searchBy(command.getSearchKeyWords(), command.getSearchType()); r.setCommandType("searchComplete"); return r; case COMPLETE: r.setCommandType("COMPLETE"); Result r1 = completeTasks.addEvent(openTasks.getEventWithId(command.getId()).getData().get(0)); Result r2 = openTasks.deleteEventWithId(command.getId()); if (r1.isSuccessful() && r2.isSuccessful()) { ArrayList<Event> data = new ArrayList<Event>(); data.add(r1.getData().get(0)); r.setData(r1.getData()); r.setSuccessful(true); return r; } else { ArrayList<Event> data = new ArrayList<Event>(); data.add(r1.getData().get(0)); r.setSuccessful(false); r.setData(data); r.setError(INVALID_COMPLETED_TASK_ID_MSG); return r; } case INCOMPLETE: Result r3 = openTasks.addEvent(completeTasks.getEventWithId(command.getId()).getData().get(0)); Result r4 = completeTasks.deleteEventWithId(command.getId()); r.setCommandType("INCOMPLETE"); if (r3.isSuccessful() && r4.isSuccessful()) { ArrayList<Event> data = new ArrayList<Event>(); data.add(r3.getData().get(0)); r.setData(r3.getData()); r.setSuccessful(true); return r; } else { ArrayList<Event> data = new ArrayList<Event>(); data.add(r3.getData().get(0)); r.setSuccessful(false); r.setData(data); r.setError(INVALID_INCOMPLETE_TASK_ID_MSG); return r; } case SAVE: Boolean re1 = openTasks.write(); Boolean re2 = completeTasks.write(); r.setCommandType("save"); if (re1 && re2) { return r; } else { r.setSuccessful(false); r.setError(IO_EXCEPTION_MSG); return r; } case BACKUP: r.setCommandType("backup"); if (completeTasks.readBackUp().isSuccessful() && openTasks.readBackUp().isSuccessful()) { r.setSuccessful(true); return r; } else { r.setSuccessful(false); r.setError(IO_EXCEPTION_MSG); return r; } default: throw new Error("Unrecognized command type"); } }