示例#1
0
  public boolean canAccept(IMediaResource res) {
    if (res instanceof IMediaFolder) return true;

    if (res instanceof IMediaFile) {
      return (MediaFileAPI.IsBluRay(res.getMediaObject()));
    } else {
      return false;
    }
  }
  protected List<IMediaResource> decorate(List<IMediaResource> originalChildren) {
    // TODO: If our children is a LazyList, then what?
    // ie, online videos are lazy, so we need to load a single "waiting"
    // item until the
    // the items are loaded. We also, need to ensure that we can reload this
    // list once all items
    // are loaded. We could add a "wait" here, but that defeats the purpose
    // of using a lazy list
    // the goal is only force a list to load once you actually get() the
    // first item

    try {
      String parentTitle = null;
      if (getParent() != null) parentTitle = getParent().getTitle();
      String title = getTitle();
      ElapsedTimer timer = new ElapsedTimer();
      Loggers.VFS_LOG.debug(
          "Begin Decoring View Items for: "
              + title
              + "; in "
              + parentTitle
              + " Original Size: "
              + originalChildren.size());

      if (Loggers.VFS_LOG.isDebugEnabled()) {
        StringBuffer sb = new StringBuffer();
        sb.append(
            "\nBefore Decorating Folder: "
                + title
                + "; in "
                + parentTitle
                + "; Items: "
                + originalChildren.size()
                + "\n");
        for (IMediaResource r : originalChildren) {
          sb.append(" + " + r.getTitle());
          if (r instanceof IMediaFolder) sb.append(" (Folder)");
          sb.append("\n");
        }
        sb.append("\n\n");
        Loggers.VFS_LOG.debug(sb);
      }

      List<IMediaResource> set = new ArrayList<IMediaResource>();
      Map<String, GroupNameMediaFolder> groups = new LinkedHashMap<String, GroupNameMediaFolder>();
      boolean grouping = presentation.hasGroupers();
      for (IMediaResource r : originalChildren) {

        if (set.contains(r)) {
          Loggers.VFS_LOG.warn("Set already contains " + r + " so it will not be added again.");
          continue;
        }

        if (presentation.canAccept(r)) {
          if (r instanceof IMediaFolder) {
            Loggers.VFS_LOG.debug("Adding subfolder: " + r.getTitle() + " to " + title);
            set.add(newViewFolder(factory, level + 1, this, (IMediaFolder) r));
          } else if (r instanceof IMediaFile) {
            if (!grouping) {
              Loggers.VFS_LOG.debug("Adding Non Grouped Item: " + r.getTitle() + " to " + title);
              // if we are not grouping, the just add it
              if (!set.add(newViewItem(this, (IMediaFile) getItem(r)))) {
                log.warn(
                    "Failed to add duplicate item to the view; item: " + r + "; view: " + title);
              }
            } else {
              boolean grouped = false;
              List<String> groupNames = presentation.getGroupNames(r);
              if (groupNames != null && groupNames.size() > 0) {
                for (String groupName : groupNames) {
                  if (!StringUtils.isEmpty(groupName)) {
                    String groupKey = createGroupKey(groupName);
                    GroupNameMediaFolder mf = groups.get(groupKey);
                    if (mf == null) {
                      Loggers.VFS_LOG.info(
                          "Creating Group Folder: "
                              + groupName
                              + " in "
                              + title
                              + " for "
                              + r.getTitle());
                      mf = new GroupNameMediaFolder(this, groupName);
                      groups.put(groupKey, mf);
                    }

                    grouped = true;
                    Loggers.VFS_LOG.debug(
                        "Adding Resourse: " + r.getTitle() + " to group: " + groupName);
                    mf.addMediaResource(r);
                  }
                }
              }

              if (!grouped) {
                Loggers.VFS_LOG.warn(
                    "No Group Name for: " + r.getTitle() + "; Adding as a top level item");
                // groupname return null, just add this as a
                // toplevel item
                set.add(newViewItem(this, (IMediaFile) getItem(r)));
              }
            }
          } else {
            Loggers.LOG.warn("ViewFolder cannot handle media type: " + r);
          }
        }
      }

      // transfer our grouped items to the set
      if (groups.size() > 0) {
        boolean pruning = presentation.isPruningSingleItems();
        for (IMediaResource r : groups.values()) {
          if (r instanceof IMediaFolder) {
            IMediaFolder f = (IMediaFolder) r;
            if (pruning && f.getChildren().size() == 1) {
              IMediaResource ch = f.getChildren().get(0);
              if (ch instanceof IMediaFolder) {
                set.add(newViewFolder(factory, level + 1, this, (IMediaFolder) ch));
              } else {
                set.add(newViewItem(this, (IMediaFile) ch));
              }
            } else {
              set.add(newViewFolder(factory, level + 1, this, (IMediaFolder) r));
            }
          } else {
            set.add(newViewFolder(factory, level + 1, this, (IMediaFolder) r));
          }
        }
      }

      if (Loggers.VFS_LOG.isDebugEnabled()) {
        StringBuffer sb = new StringBuffer();
        sb.append(
            "\nAfter Decorating Folder: "
                + title
                + "; in "
                + parentTitle
                + "; Items: "
                + set.size()
                + "\n");
        for (IMediaResource r : set) {
          sb.append(" + " + r.getTitle());
          if (r instanceof IMediaFolder) sb.append(" (Folder)");
          sb.append("\n");
        }
        sb.append("\n\n");
        Loggers.VFS_LOG.debug(sb);
      }

      if (presentation.hasSorters()) {
        Loggers.VFS_LOG.debug("Sorting View Items");
        presentation.sort(set);
      }

      Loggers.VFS_LOG.info(
          "End Decoring View Items for: "
              + getTitle()
              + "; in "
              + parentTitle
              + "; Time: "
              + timer.delta()
              + "ms");
      return new ArrayList<IMediaResource>(set);
    } catch (Throwable t) {
      log.warn("Failed to create ViewFolder for: " + getTitle(), t);
      return new ArrayList<IMediaResource>();
    }
  }