Exemple #1
0
 /**
  * Given the title of the item, get the Stock object of the item.
  *
  * @param item The title of the item.
  * @return The Stock object of the item.
  */
 public Stock getItem(String item) {
   for (Album a : musicDb) {
     if (a.getTitle().equalsIgnoreCase(item)) return a;
     for (Song s : a.getSongs()) {
       if (s.getTitle().equalsIgnoreCase(item)) return s;
     }
   }
   return null;
 }
Exemple #2
0
 /**
  * Check if a given song belongs to the given album.
  *
  * @param song The name of the song.
  * @param album The title of the album.
  * @return True if the song belongs to the album. False otherwise.
  */
 public boolean songBelongsToAlbum(String song, String album) {
   for (Album a : musicDb) {
     if (a.getTitle().equalsIgnoreCase(album)) {
       for (Song s : a.getSongs()) {
         if (s.getTitle().equalsIgnoreCase(song)) return true;
       }
     }
   }
   return false;
 }
Exemple #3
0
  /**
   * Search in database for the album with matching substring.
   *
   * @param searchString The substring in which the user typed in that is to be used in searching
   *     for the matching albums.
   * @return The list of albums with matching substring.
   */
  public LinkedList<Album> searchForAlbum(String searchString) {
    LinkedList<Album> results = new LinkedList<Album>();

    // Traverse the database and look for the album that contains
    // the search string.
    for (Album a : musicDb) {
      if (a.getTitle().contains(searchString)) {
        results.add(a);
      }
    }
    return results;
  }
  @Override
  public void insertAlbum(Album album) {
    Document albumToAdd = new Document();
    try {

      db.getCollection("album")
          .insertOne(
              albumToAdd
                  .append("title", album.getTitle())
                  .append("score", album.getScore())
                  .append("released", album.getReleased())
                  .append("genre", album.getGenre())
                  .append(
                      "artist",
                      new Document()
                          .append("name", this.getArtists())
                          .append("nation", this.getArtists())));

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Retrieve the album information and each photo in the album. The photos are retrieved in
   * parallel.
   */
  private void getAlbum(PrintWriter respWriter, long albumId) throws RemoteInvocationException {
    // get the specific album
    final Request<Album> getAlbumReq = _albumBuilders.get().id(albumId).build();
    final ResponseFuture<Album> getAlbumFuture = _restClient.sendRequest(getAlbumReq);
    final Response<Album> getResp = getAlbumFuture.getResponse();
    final Album album = getResp.getEntity();

    respWriter.println(album.getTitle());
    respWriter.println("Created on " + new Date(album.getCreationTime()));

    // get the album's entries
    final FindRequest<AlbumEntry> searchReq =
        _albumEntryBuilders.findBySearch().albumIdParam(albumId).build();
    final ResponseFuture<CollectionResponse<AlbumEntry>> responseFuture =
        _restClient.sendRequest(searchReq);
    final Response<CollectionResponse<AlbumEntry>> response = responseFuture.getResponse();
    final List<AlbumEntry> entries = new ArrayList<AlbumEntry>(response.getEntity().getElements());

    entries.add(new AlbumEntry().setAlbumId(-1).setPhotoId(9999));

    // don't return until all photo requests done
    final CountDownLatch latch = new CountDownLatch(entries.size());

    // fetch every photo asynchronously
    // store either a photo or an exception
    final Object[] photos = new Object[entries.size()];
    for (int i = 0; i < entries.size(); i++) {
      final int finalI = i; // need final version for callback
      final AlbumEntry entry = entries.get(i);
      final long photoId = entry.getPhotoId();
      final Request<Photo> getPhotoReq = _photoBuilders.get().id(photoId).build();
      _restClient.sendRequest(
          getPhotoReq,
          new Callback<Response<Photo>>() {
            @Override
            public void onSuccess(Response<Photo> result) {
              photos[finalI] = result.getEntity();
              latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
              photos[finalI] = e;
            }
          });
    }

    try {
      // wait for all requests to finish
      latch.await(2, TimeUnit.SECONDS);
      if (latch.getCount() > 0) {
        respWriter.println("Failed to retrieve some photo(s)");
      }
    } catch (InterruptedException e) {
      e.printStackTrace(respWriter);
    }

    // print photo data
    for (int i = 0; i < entries.size(); i++) {
      final Object val = photos[i];
      final AlbumEntry entry = entries.get(i);
      if (val instanceof Throwable) {
        respWriter.println("Failed to load photo " + entry.getPhotoId());
        respWriter.println("Stack trace:");
        ((Throwable) val).printStackTrace(respWriter);
        respWriter.println();
      } else if (val instanceof Photo) {
        final Photo photo = (Photo) val;
        respWriter.println("Photo " + photo.getTitle() + ":");
        respWriter.println(photo);
        respWriter.println("Added on " + new Date(entry.getAddTime()));
      } else {
        throw new AssertionError("expected photo or exception");
      }
    }
  }