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.openmarket.xcelerate.interfaces.IAsset;
20  import org.apache.commons.lang3.StringUtils;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import tools.gsf.facade.assetapi.AssetClosure;
24  import tools.gsf.facade.assetapi.AttributeDataUtils;
25  
26  import java.util.Date;
27  
28  /**
29   * @author Dolf Dijkstra
30   * @since Apr 6, 2011
31   */
32  public class DateFilterClosure implements AssetClosure {
33      protected static final Logger LOG = LoggerFactory.getLogger("tools.gsf.facade.assetapi.asset.DateFilterClosure");
34  
35      private final Date cutoff;
36  
37      private final AssetClosure target;
38  
39      private static Date parseDate(final String date) {
40  
41          return StringUtils.isNotBlank(date) ? com.fatwire.cs.core.db.Util.parseJdbcDate(date) : null;
42      }
43  
44      /**
45       * @param cutoff the cutoff date in the cs date format.
46       * @param target target asset closure
47       */
48      public DateFilterClosure(final String cutoff, final AssetClosure target) {
49          this(parseDate(cutoff), target);
50  
51      }
52  
53      /**
54       * @param cuttoff cut off date
55       * @param target  target asset closure
56       */
57      public DateFilterClosure(final Date cuttoff, final AssetClosure target) {
58          if (target == null) {
59              throw new IllegalArgumentException("target cannot be null");
60          }
61          this.target = target;
62          this.cutoff = cuttoff == null ? null : new Date(cuttoff.getTime());
63  
64      }
65  
66      public boolean work(final AssetData assetData) {
67          if (cutoff != null) {
68              final Date assetStartDate = AttributeDataUtils.asDate(assetData.getAttributeData(IAsset.STARTDATE, true));
69              final Date assetEndDate = AttributeDataUtils.asDate(assetData.getAttributeData(IAsset.ENDDATE, true));
70              if (LOG.isTraceEnabled()) {
71                  LOG.trace("assetStartDate " + assetStartDate + " assetEndDate " + assetEndDate);
72              }
73              if (assetEndDate != null && assetEndDate.before(cutoff)) {
74                  // ignore
75                  return true;
76              }
77              if (assetStartDate != null && assetStartDate.after(cutoff)) {
78                  // ignore, assuming that endDate if after startDate
79                  return true;
80              }
81          }
82          // filter disabled or no date set, just pass-thru
83          if (LOG.isDebugEnabled()) {
84              LOG.debug("passing thru " + assetData.getAssetId());
85          }
86          return target.work(assetData);
87      }
88  
89  }