public static UnaryFunctor<Individual, Boolean> getFilterForTab(
      final Tab tab, final Portal portalThatTabIsIn, final boolean isFlag1Filtering) {

    DateTime now = new DateTime();
    UnaryFunctor<Individual, Boolean> entFilter = getTimeFilter(tab, now);
    UnaryFunctor<Individual, Boolean> tabPortalFilter = getPortalFilter(tab);

    if (isFlag1Filtering
        && tabPortalFilter != null
        && portalThatTabIsIn != null
        && portalThatTabIsIn.isFlag1Filtering())
      entFilter = AdaptorFunctors.and(entFilter, tabPortalFilter);

    String flag2set = tab.getFlag2Set();
    if (flag2set != null && flag2set.trim().length() > 0) {
      String[] flags = flag2set.split(",");
      if ("OMIT".equalsIgnoreCase(tab.getFlag2Mode())) {
        UnaryFunctor<Individual, Boolean> negate =
            LogicalFunctors.unaryNegate(getCollegeFilter(flags));
        entFilter = AdaptorFunctors.and(entFilter, negate);
      } else {
        entFilter = AdaptorFunctors.and(entFilter, getCollegeFilter(flags));
      }
    }

    /* need more?
    entFilter = AdaptorFunctors.and( getSomeFilter(tab), entFilter);
    entFilter = AdaptorFunctors.and( getOtherFilter(tab), entFilter);
     */
    return entFilter;
  }
 /** Filter the entities based on the specifications of the tab. */
 protected static UnaryFunctor<Individual, Boolean> getPortalFilter(Tab tab) {
   ApplicationBean appBean = new ApplicationBean();
   if (tab.getPortalId() == appBean.getSharedPortalFlagNumeric()) {
     return VitroFilterUtils.getCalsPortalFilter().getIndividualFilter();
     // } else if (tab.getPortalId() == appBean.getAllPortalFlagNumeric()){
   } else if (tab.getPortalId() == 65535) {
     return VitroFilterUtils.t;
   } else {
     return VitroFilterUtils.getFlag1ExclusiveFilter(
         FlagMathUtils.portalId2Numeric(tab.getPortalId()));
   }
 }
  /** Create a filter for the entities based on the specifications of the tab. */
  public static UnaryFunctor<Individual, Boolean> getTimeFilter(final Tab tab, final DateTime now) {
    UnaryFunctor<Individual, Boolean> out = null;

    if (tab.getDayLimit() == 0) {
      out = VitroFilterUtils.getSunsetWindowFilter(now.toDate()).getIndividualFilter();

    } else if (tab.getDayLimit() > 0) {
      out =
          new UnaryFunctor<Individual, Boolean>() {
            public Boolean fn(Individual arg) {
              if (arg.getTimekey() == null) return Boolean.FALSE;

              DateTime timekey = new DateTime(arg.getTimekey());
              DateTime startShowingDate = timekey.minusDays(tab.getDayLimit());

              /* This is the filtering for events in the future */
              return now.isAfter(startShowingDate) && now.isBefore(timekey);
            }

            public String toString() {
              return "DayLimit Filter: is timekey after now ("
                  + now.toString()
                  + ") and timekey + daylimit is before now?";
            }
          };

    } else if (tab.getDayLimit() < 0) {
      out =
          new UnaryFunctor<Individual, Boolean>() {
            public Boolean fn(Individual arg) {
              if (arg.getSunrise() == null) return Boolean.FALSE;

              DateTime sunrise = new DateTime(arg.getSunrise());
              DateTime stopShowingDate = sunrise.plusDays(Math.abs(tab.getDayLimit()));

              /* This is the filtering for press releases */
              return (sunrise.isBefore(now) || sunrise.isEqual(now))
                  && now.isBefore(stopShowingDate);
            }

            public String toString() {
              return "Sunrise Filter: is sunrise before now and"
                  + " days between now and sunrise greater than or eq to daylimit?";
            }
          };
    }

    return out;
  }