View Javadoc
1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package tools.gsf.facade.assetapi.asset;
17  
18  import com.fatwire.assetapi.data.AssetData;
19  import com.fatwire.assetapi.data.AssetId;
20  import tools.gsf.facade.assetapi.AssetAccessTemplate;
21  import tools.gsf.facade.assetapi.AssetClosure;
22  
23  import java.util.Date;
24  import java.util.Iterator;
25  import java.util.LinkedList;
26  import java.util.List;
27  
28  /**
29   * Iterable that filters a list of assetids based on a future date. It makes use
30   * of a DateFilterClosure.
31   *
32   * @author Dolf.Dijkstra
33   * @see "DateFilterClosure"
34   * @since Apr 20, 2011
35   */
36  public class AssetFilterIterator implements Iterable<AssetId> {
37  
38      final Iterable<AssetId> i;
39  
40      /**
41       * @param aat      asset access template
42       * @param assetIds collection of AssetIds
43       */
44      public AssetFilterIterator(final AssetAccessTemplate aat, final Iterable<AssetId> assetIds) {
45          this(aat, new Date(), assetIds);
46  
47      }
48  
49      /**
50       * @param aat      asset access template
51       * @param date     date
52       * @param assetIds collection of AssetIds
53       */
54  
55      public AssetFilterIterator(final AssetAccessTemplate aat, final Date date, final Iterable<AssetId> assetIds) {
56          super();
57  
58          i = date == null ? assetIds : toIterable(aat, date, assetIds);
59      }
60  
61      private Iterable<AssetId> toIterable(final AssetAccessTemplate aat, final Date date,
62                                           final Iterable<AssetId> assetIds) {
63          final List<AssetId> list = new LinkedList<AssetId>();
64          final AssetClosure target = new AssetClosure() {
65              public boolean work(final AssetData asset) {
66                  list.add(asset.getAssetId());
67                  return true;
68              }
69          };
70          final AssetClosure closure = new DateFilterClosure(date, target);
71  
72          aat.readAsset(assetIds, closure, "startdate", "enddate");
73          return list;
74      }
75  
76      public Iterator<AssetId> iterator() {
77          return i.iterator();
78      }
79  }