public static JwMap<Integer, AcDomesticCandidateRouteOffer> toIdMap(
     Collection<AcDomesticCandidateRouteOffer> v) {
   JwMap<Integer, AcDomesticCandidateRouteOffer> m =
       new JwMap<Integer, AcDomesticCandidateRouteOffer>();
   for (AcDomesticCandidateRouteOffer e : v) m.put(e.getId(), e);
   return m;
 }
 public void loadAll() {
   JwMap<AcUspsDomesticSupplyCircuityPkIF, AcUspsDomesticSupplyCircuity> m =
       getUspsDomesticSupplyCircuityMap();
   JwList<AcUspsDomesticSupplyCircuity> v = getAllFromDb();
   for (AcUspsDomesticSupplyCircuity e : v) m.put(e, e);
   setAllLoaded(true);
 }
 public static JwMap<Integer, AcUspsDomesticSkeletonRouteOffer> toIdMap(
     Collection<AcUspsDomesticSkeletonRouteOffer> v) {
   JwMap<Integer, AcUspsDomesticSkeletonRouteOffer> m =
       new JwMap<Integer, AcUspsDomesticSkeletonRouteOffer>();
   for (AcUspsDomesticSkeletonRouteOffer e : v) m.put(e.getId(), e);
   return m;
 }
  private void fillNests(JwSet<Integer> nestIds) {
    enterMonitor("AcActionSnapshotLoader.fillNest");
    try {
      if (nestIds.isEmpty()) return;

      // we need to continue loading nests until all parent and
      // top nests have been loaded into the snapshot.
      // also load all any aliases for nests newly loaded nests.
      JwSet<Integer> aliasIds = new JwSet<Integer>();
      while (nestIds.isNotEmpty()) {
        fillDatabaseNests(nestIds);
        fillMissingNests(nestIds);

        // determine missing nest ids
        JwSet<Integer> allNestIds = AcBatchNest.collectNestIds(_nests.values());
        JwSet<Integer> loadedNestIds = AcBatchNest.collectIds(_nests.values());
        nestIds = allNestIds;
        nestIds.removeAll(loadedNestIds);

        // determine missing alias ids
        JwSet<Integer> allAliasIds = AcBatchNest.collectAliasIds(_nests.values());
        JwSet<Integer> loadedAliasIds = AcBatchAlias.collectIds(_aliases.values());
        aliasIds = allAliasIds;
        aliasIds.removeAll(loadedAliasIds);
        fillAliases(aliasIds);
      }
    } finally {
      exitMonitor();
    }
  }
 public void loadAll() {
   JwMap<AcUspsInternationalRouteStatusPkIF, AcUspsInternationalRouteStatus> m =
       getUspsInternationalRouteStatusMap();
   JwList<AcUspsInternationalRouteStatus> v = getAllFromDb();
   for (AcUspsInternationalRouteStatus e : v) m.put(e, e);
   setAllLoaded(true);
 }
 private AcAirport getAirport(JwMap<String, AcAirport> airportCache, String code) {
   AcAirport e = airportCache.get(code);
   if (e == null) {
     e = getAccess().getAirportDb().getAirport(code);
     airportCache.put(code, e);
   }
   return e;
 }
 protected AcConsignment getConsignment(Integer id) {
   AcConsignment c = _consignmentMap.get(id);
   if (c == null) {
     c = _access.getConsignmentDb().getConsignment(id);
     _consignmentMap.put(id, c);
   }
   return c;
 }
  private void testAllNestsLoaded() {
    JwSet<Integer> missingIds = new JwSet<Integer>();
    for (Integer flightId : _flights.keySet()) {
      JwList<Integer> dbIds = _access.getNestDb().getAllIdsByRootFlightId(flightId);
      missingIds.addAll(dbIds);
    }
    removeLoaded(missingIds, _nests.keySet());

    if (missingIds.isNotEmpty()) debug(null, "    MISSING NESTS: " + missingIds);
  }
 public AcUploadDownloadFile getUploadDownloadFile(AcUploadDownloadFilePkIF pk) {
   JwMap<AcUploadDownloadFilePkIF, AcUploadDownloadFile> m = getUploadDownloadFileMap();
   AcUploadDownloadFile e = m.get(pk);
   if (isAllLoaded()) return e;
   if (e == null) {
     if (isSingleLoad() && m.containsKey(pk)) return null;
     e = getUploadDownloadFileFromDb(pk);
     m.put(pk, e);
   }
   return e;
 }
  private void loadLatestModelsRelatedToGroups(
      JwMap<Integer, JwSet<Integer>> m, Integer bId, JwSet<Integer> groups) {
    enterMonitor("AcActionSnapshotLoader.loadGroups");
    try {
      JwList<AcBatchItem> is;
      JwList<AcBatchNest> ns;
      JwList<AcBatchAlias> as;
      JwList<AcBatchFlight> fs;

      JwTimestamp utcTs = _batch.getStartUtcTs();
      is = _access.getBatchItemDb().getLatestViewBeforeByBatchGroups(bId, groups, utcTs);
      ns = _access.getBatchNestDb().getLatestViewBeforeByBatchGroups(bId, groups, utcTs);
      as = _access.getBatchAliasDb().getLatestViewBeforeByBatchGroups(bId, groups, utcTs);
      fs = _access.getBatchFlightDb().getLatestViewBeforeByBatchGroups(bId, groups, utcTs);

      for (AcBatchItem e : is) {
        //                if ( e.hasItemId(DEBUG_ITEM_ID) )
        //                {
        //                    debug("x","Item "+ e.getItemId()+ " batch "+ e.getBatchId()+ " group
        // "+ e.getGroupCode());
        //                    Thread.dumpStack();
        //                }
        _items.putIfMissing(e.getItemId(), e);
        addBatchGroup(m, e.getBatchId(), e.getGroupCode());
      }
      for (AcBatchNest e : ns) {
        _nests.putIfMissing(e.getNestId(), e);
        addBatchGroup(m, e.getBatchId(), e.getGroupCode());
      }
      for (AcBatchAlias e : as) {
        _aliases.putIfMissing(e.getAliasId(), e);
        //                if ( _aliases.putIfMissing(e.getAliasId(), e) )
        //                {
        //                    if ( e.hasAliasId(DEBUG_ALIAS_ID) )
        //                    {
        //                        debug("x", "Alias "
        //                            + e.getAliasId()
        //                            + " batch "
        //                            + e.getBatchId()
        //                            + " group "
        //                            + e.getGroupCode());
        //                        Thread.dumpStack();
        //                    }
        //                }
        addBatchGroup(m, e.getBatchId(), e.getGroupCode());
      }
      for (AcBatchFlight e : fs) {
        _flights.putIfMissing(e.getFlightId(), e);
        addBatchGroup(m, e.getBatchId(), e.getGroupCode());
      }
    } finally {
      exitMonitor();
    }
  }
 public AcEquipment getEquipment(AcEquipmentPkIF pk) {
   JwMap<AcEquipmentPkIF, AcEquipment> m = getEquipmentMap();
   AcEquipment e = m.get(pk);
   if (isAllLoaded()) return e;
   if (e == null) {
     if (isSingleLoad() && m.containsKey(pk)) return null;
     e = getEquipmentFromDb(pk);
     m.put(pk, e);
   }
   return e;
 }
 public AcGlobalLanguage getGlobalLanguage(AcGlobalLanguagePkIF pk) {
   JwMap<AcGlobalLanguagePkIF, AcGlobalLanguage> m = getGlobalLanguageMap();
   AcGlobalLanguage e = m.get(pk);
   if (isAllLoaded()) return e;
   if (e == null) {
     if (isSingleLoad() && m.containsKey(pk)) return null;
     e = getGlobalLanguageFromDb(pk);
     m.put(pk, e);
   }
   return e;
 }
 public AcMonitorAlert getMonitorAlert(AcMonitorAlertPkIF pk) {
   JwMap<AcMonitorAlertPkIF, AcMonitorAlert> m = getMonitorAlertMap();
   AcMonitorAlert e = m.get(pk);
   if (isAllLoaded()) return e;
   if (e == null) {
     if (isSingleLoad() && m.containsKey(pk)) return null;
     e = getMonitorAlertFromDb(pk);
     m.put(pk, e);
   }
   return e;
 }
 public AcInclusionOperator getInclusionOperator(AcInclusionOperatorPkIF pk) {
   JwMap<AcInclusionOperatorPkIF, AcInclusionOperator> m = getInclusionOperatorMap();
   AcInclusionOperator e = m.get(pk);
   if (isAllLoaded()) return e;
   if (e == null) {
     if (isSingleLoad() && m.containsKey(pk)) return null;
     e = getInclusionOperatorFromDb(pk);
     m.put(pk, e);
   }
   return e;
 }
  public AcActionSnapshot getSnapshot(boolean logMissing) {
    // debug("A", "getSnapshot");

    loadMissing(logMissing);
    AcActionSnapshot e = new AcActionSnapshot(getAccountCode());
    e.setItems(_items.getValues());
    e.setNests(_nests.getValues());
    e.setAliases(_aliases.getValues());
    e.setFlights(_flights.getValues());
    e.linkParentsFromIds();
    return e;
  }
 public AcUspsDomesticSupplyCircuity getUspsDomesticSupplyCircuity(
     AcUspsDomesticSupplyCircuityPkIF pk) {
   JwMap<AcUspsDomesticSupplyCircuityPkIF, AcUspsDomesticSupplyCircuity> m =
       getUspsDomesticSupplyCircuityMap();
   AcUspsDomesticSupplyCircuity e = m.get(pk);
   if (isAllLoaded()) return e;
   if (e == null) {
     if (isSingleLoad() && m.containsKey(pk)) return null;
     e = getUspsDomesticSupplyCircuityFromDb(pk);
     m.put(pk, e);
   }
   return e;
 }
 public AcUspsInternationalRouteStatus getUspsInternationalRouteStatus(
     AcUspsInternationalRouteStatusPkIF pk) {
   JwMap<AcUspsInternationalRouteStatusPkIF, AcUspsInternationalRouteStatus> m =
       getUspsInternationalRouteStatusMap();
   AcUspsInternationalRouteStatus e = m.get(pk);
   if (isAllLoaded()) return e;
   if (e == null) {
     if (isSingleLoad() && m.containsKey(pk)) return null;
     e = getUspsInternationalRouteStatusFromDb(pk);
     m.put(pk, e);
   }
   return e;
 }
  private void fillDatabaseAliases(JwSet<Integer> aliasIds) {
    JwList<AcBatchAlias> v =
        _access.getBatchAliasDb().getBatchAliasesBefore(_batch.getStartUtcTs(), aliasIds);
    for (AcBatchAlias e : v) {
      Integer id = e.getAliasId();
      if (isAliasLoaded(id)) continue;

      _aliases.put(id, e);
      //            if ( id == DEBUG_ALIAS_ID )
      //            {
      //                debug(
      //                    "x",
      //                    "Alias "
      //                        + e.getAliasId()
      //                        + " batch "
      //                        + e.getBatchId()
      //                        + " group "
      //                        + e.getGroupCode());
      //                Thread.dumpStack();
      //            }
      if (e.hasGroupCode()) {
        // debug("1","Alias "+ e.getAliasId()+ " batch "+ e.getBatchId()+ " group "+
        // e.getGroupCode());
        addBatchGroup(_batchGroups, e.getBatchId(), e.getGroupCode());
      }
    }
  }
  private void fillMissingFlights(JwSet<Integer> flightIds) {
    for (Integer id : flightIds) {
      if (isFlightLoaded(id)) continue;

      AcBatchFlight e = new AcBatchFlight(getAccountCode());
      e.setFlightId(id);
      _flights.put(id, e);
    }
  }
  private void fillMissingNests(JwSet<Integer> nestIds) {
    for (Integer id : nestIds) {
      if (isNestLoaded(id)) continue;

      AcBatchNest e = new AcBatchNest(getAccountCode());
      e.setNestId(id);
      _nests.put(id, e);
    }
  }
  private void fillActions(JwList<AcAction> actions, boolean skipRevokedActions) {
    enterMonitor("AcActionSnapshotLoader.fillActions");
    try {
      // debug("B", "fillNonRevokedActions");
      JwSet<Integer> itemIds = new JwSet<Integer>();
      JwSet<Integer> nestIds = new JwSet<Integer>();
      JwSet<Integer> aliasIds = new JwSet<Integer>();
      JwSet<Integer> flightIds = new JwSet<Integer>();
      if (hasEntanglement()) {
        itemIds.addAll(_entanglement.getItemIds());
        nestIds.addAll(_entanglement.getNestIds());
        aliasIds.addAll(_entanglement.getAliasIds());
        flightIds.addAll(_entanglement.getFlightIds());
      }

      for (AcAction a : actions) {
        if (skipRevokedActions && a.hasStatusRevoked() && a.hasPendingChangeNone()) continue;
        a.collectItemIdsOn(itemIds);
        a.collectNestIdsOn(nestIds);
        a.collectAliasIdsOn(aliasIds);
        a.collectFlightIdsOn(flightIds);
      }

      fillItems(itemIds);
      AcBatchItem.collectNestIdsOn(_items.values(), nestIds);
      AcBatchItem.collectFlightIdsOn(_items.values(), flightIds);

      // load aliases before nests
      fillAliases(aliasIds);
      AcBatchAlias.collectNestIdsOn(_aliases.values(), nestIds);

      fillNests(nestIds);
      AcBatchNest.collectFlightIdsOn(_nests.values(), flightIds);

      fillFlights(flightIds);
      fillGroups();

      testAllItemsAndNestsLoaded();
    } finally {
      exitMonitor();
    }
  }
  protected byte[] run() {
    _nowUtcTs = AcGlobals.nowUtc();

    makeModels();

    String accountCode = _access.getAccountCode();
    AcPropertyRegistry pr = AcPropertyRegistries.getAccount(accountCode);
    AcCn46Document document = new AcCn46Document(pr);
    byte[] pdf = document.makeDocument(_models.getValues());
    _isValidCn46Document = document.isValidDocument();
    return pdf;
  }
  protected AcCn46ActualConveyanceModel getModel(
      AcItem i, AcUpuTag upuTag, String offloadingAirport, boolean splitOnDispatchDestination) {
    AcFlight f = getDepartingFlight(i.getId());
    AcResult last = getLastHandledAction(i.getId());

    _dummy.clear();
    _dummy.setTypeFromTag(upuTag);
    if (splitOnDispatchDestination)
      _dummy.setDispatchDestination(upuTag.getDestinationDispatchImpcCode());
    if (last != null) {
      AcAirport a = getAirport(last.getAirportCode());
      _dummy.setLastHandledAirport(last.getAirportCode());
      _dummy.setGeneratedTz(a.getTimeZone());
    }
    _dummy.setOriginAdministration(i.getUpuShortPostName());
    _dummy.setAirportOfOffloading(offloadingAirport);
    if (f != null) {
      _dummy.setAirportOfLoading(f.getOriginAirportCode());
      _dummy.setFlightCarrierCode(f.getOperatorCarrierCode());
      _dummy.setFlightNumber(f.getOperatorNumber());
      _dummy.setFlightNumberDisplayLabel(f.getOperatorNumberDisplayLabel());
      _dummy.setDepartureDt(
          f.hasActualDepartureLocalTs()
              ? f.getActualDepartureLocalTs().getDate()
              : f.getScheduledDepartureLocalDt());
    }
    String key = _dummy.getHashKey();
    AcCn46ActualConveyanceModel model = _models.get(key);
    if (model == null) {
      model = _dummy.getCopy();
      model.setCarrierCode(AcUtility.getAccountCarrierName(_access.getAccountCode()));
      model.setGeneratedTs(_nowUtcTs);
      model.setNote("** Note: This document was generated out of VelocityMail **");
      _models.put(key, model);
    }

    return model;
  }
  private void fillDatabaseFlights(JwSet<Integer> flightIds) {
    JwList<AcBatchFlight> v =
        _access.getBatchFlightDb().getBatchFlightsBefore(_batch.getStartUtcTs(), flightIds);
    for (AcBatchFlight e : v) {
      Integer id = e.getFlightId();
      if (isFlightLoaded(id)) continue;

      _flights.put(id, e);
      if (e.hasGroupCode()) {
        // debug("1","Flight "+ e.getFlightId()+ " batch "+ e.getBatchId()+ " group "+
        // e.getGroupCode());
        addBatchGroup(_batchGroups, e.getBatchId(), e.getGroupCode());
      }
    }
  }
  private void fillMissingItems(JwSet<Integer> itemIds) {
    for (Integer id : itemIds) {
      if (isItemLoaded(id)) continue;

      AcBatchItem e = new AcBatchItem(getAccountCode());
      e.setItemId(id);
      //            if ( e.hasItemId(DEBUG_ITEM_ID) )
      //            {
      //                debug("x","Item "+ e.getItemId()+ " batch "+ e.getBatchId()+ " group "+
      // e.getGroupCode());
      //                Thread.dumpStack();
      //            }
      _items.put(id, e);
    }
  }
  private void fillDatabaseItems(JwSet<Integer> itemIds) {
    JwList<AcBatchItem> v =
        _access.getBatchItemDb().getBatchItemsBefore(_batch.getStartUtcTs(), itemIds);
    for (AcBatchItem e : v) {
      Integer id = e.getItemId();
      if (isItemLoaded(id)) continue;

      _items.put(id, e);
      if (e.hasGroupCode()) {
        // debug("1","Item "+ e.getItemId()+ " batch "+ e.getBatchId()+ " group "+
        // e.getGroupCode());
        addBatchGroup(_batchGroups, e.getBatchId(), e.getGroupCode());
      }
    }
  }
  /**
   * Load the batch nest associated with the specified nest. This is used when processing alias
   * actions where the alias has not yet been mapped to a nest. The nest being passed in could be
   * new or it could be an existing nest. Either way we need to load or create the associated batch
   * nest.
   */
  public AcActionSnapshot loadNest(AcBatch batch, AcNest nest) {
    _batch = batch;

    _items = new JwMap<Integer, AcBatchItem>();
    _nests = new JwMap<Integer, AcBatchNest>();
    _aliases = new JwMap<Integer, AcBatchAlias>();
    _flights = new JwMap<Integer, AcBatchFlight>();

    JwSet<Integer> flightIds = new JwSet<Integer>();
    JwSet<Integer> nestIds = new JwSet<Integer>();
    nestIds.add(nest.getId());

    fillNests(nestIds);
    AcBatchNest.collectFlightIdsOn(_nests.values(), flightIds);

    fillFlights(flightIds);
    return getSnapshot(true);
  }
  private void fillMissingAliases(JwSet<Integer> aliasIds) {
    for (Integer id : aliasIds) {
      if (isAliasLoaded(id)) continue;

      AcBatchAlias e = new AcBatchAlias(getAccountCode());
      e.setAliasId(id);
      _aliases.put(id, e);
      //            if ( id == DEBUG_ALIAS_ID )
      //            {
      //                debug(
      //                    "x",
      //                    "Alias "
      //                        + e.getAliasId()
      //                        + " batch "
      //                        + e.getBatchId()
      //                        + " group "
      //                        + e.getGroupCode());
      //                Thread.dumpStack();
      //            }
    }
  }
  private void loadMissing(boolean logMissing) {
    JwSet<Integer> nestIds = new JwSet<Integer>();
    AcBatchItem.collectNestIdsOn(_items.values(), nestIds);
    removeLoadedIds(nestIds, _nests.keySet());
    if (logMissing && nestIds.isNotEmpty()) logMissing("nests", nestIds);
    fillNests(nestIds);

    JwSet<Integer> aliasIds = new JwSet<Integer>();
    AcBatchNest.collectAliasIdsOn(_nests.values(), aliasIds);
    removeLoadedIds(aliasIds, _aliases.keySet());
    if (logMissing && aliasIds.isNotEmpty()) logMissing("aliases", aliasIds);
    fillAliases(aliasIds);

    JwSet<Integer> flightIds = new JwSet<Integer>();
    AcBatchItem.collectFlightIdsOn(_items.values(), flightIds);
    AcBatchNest.collectFlightIdsOn(_nests.values(), flightIds);
    removeLoadedIds(flightIds, _flights.keySet());
    if (logMissing && flightIds.isNotEmpty()) logMissing("flights", flightIds);
    fillFlights(flightIds);
  }
 public void loadAll() {
   JwMap<AcGlobalLanguagePkIF, AcGlobalLanguage> m = getGlobalLanguageMap();
   JwList<AcGlobalLanguage> v = getAllFromDb();
   for (AcGlobalLanguage e : v) m.put(e, e);
   setAllLoaded(true);
 }