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