1   /*
2    * Copyright 2003 - 2014 The eFaps Team
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   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.db;
22  
23  import java.util.List;
24  import java.util.concurrent.TimeUnit;
25  
26  import org.efaps.util.EFapsException;
27  
28  /**
29   * TODO comment!
30   *
31   * @author The eFaps Team
32   * @version $Id$
33   */
34  public class CachedMultiPrintQuery
35      extends MultiPrintQuery
36      implements ICacheDefinition
37  {
38  
39      /**
40       * Key used for Caching.
41       */
42      private final String key;
43  
44      /**
45       * lifespan of the entry. Negative values are interpreted as unlimited
46       * lifespan. 0 means do not apply
47       */
48      private long lifespan = 0;
49  
50      /**
51       * time unit for lifespan.
52       */
53      private TimeUnit lifespanUnit;
54  
55      /**
56       * the maximum amount of time this key is allowed to be idle for before it
57       * is considered as expired. 0 means do not apply
58       */
59      private long maxIdleTime = 0;
60  
61      /**
62       * time unit for max idle time.
63       */
64      private TimeUnit maxIdleTimeUnit;
65  
66      /**
67       * @param _instances instance to be updated.
68       * @param _key key used for caching
69       * @throws EFapsException on error
70       */
71      public CachedMultiPrintQuery(final List<Instance> _instances,
72                                   final String _key)
73          throws EFapsException
74      {
75          super(_instances);
76          this.key = _key;
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public boolean isCacheEnabled()
84      {
85          return true;
86      }
87  
88      @Override
89      public String getKey()
90      {
91          return this.key;
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public long getLifespan()
99      {
100         return this.lifespan;
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public TimeUnit getLifespanUnit()
108     {
109         return this.lifespanUnit;
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public long getMaxIdleTime()
117     {
118         return this.maxIdleTime;
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public TimeUnit getMaxIdleTimeUnit()
126     {
127         return this.maxIdleTimeUnit;
128     }
129 
130     /**
131      * Setter method for instance variable {@link #lifespan}.
132      *
133      * @param _lifespan value for instance variable {@link #lifespan}
134      * @return this instance to allow chaining
135      */
136     public CachedMultiPrintQuery setLifespan(final long _lifespan)
137     {
138         this.lifespan = _lifespan;
139         return this;
140     }
141 
142     /**
143      * Setter method for instance variable {@link #lifespanUnit}.
144      *
145      * @param _lifespanUnit value for instance variable {@link #lifespanUnit}
146      * @return this instance to allow chaining
147      */
148     public CachedMultiPrintQuery setLifespanUnit(final TimeUnit _lifespanUnit)
149     {
150         this.lifespanUnit = _lifespanUnit;
151         return this;
152     }
153 
154     /**
155      * Setter method for instance variable {@link #maxIdleTime}.
156      *
157      * @param _maxIdleTime value for instance variable {@link #maxIdleTime}
158      * @return this instance to allow chaining
159      */
160     public CachedMultiPrintQuery setMaxIdleTime(final long _maxIdleTime)
161     {
162         this.maxIdleTime = _maxIdleTime;
163         return this;
164     }
165 
166     /**
167      * Setter method for instance variable {@link #maxIdleTimeUnit}.
168      *
169      * @param _maxIdleTimeUnit value for instance variable
170      *            {@link #maxIdleTimeUnit}
171      * @return this instance to allow chaining
172      */
173     public CachedMultiPrintQuery setMaxIdleTimeUnit(final TimeUnit _maxIdleTimeUnit)
174     {
175         this.maxIdleTimeUnit = _maxIdleTimeUnit;
176         return this;
177     }
178 
179     /**
180      * Get a CachedMultiPrintQuery that will only cache during a request.
181      * @param _instances instance to be updated.
182      * @throws EFapsException on error
183      */
184     public static CachedMultiPrintQuery get4Request(final List<Instance> _instances)
185         throws EFapsException
186     {
187         return new CachedMultiPrintQuery(_instances, Context.getThreadContext().getRequestId()).setLifespan(5)
188                         .setLifespanUnit(TimeUnit.MINUTES);
189     }
190 }