View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.model;
12  
13  import java.io.Serializable;
14  import java.util.ArrayList;
15  import java.util.List;
16  import java.util.regex.Pattern;
17  import java.util.regex.PatternSyntaxException;
18  
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  /**
23   * Data model class used by session search feature of application session screen.
24   */
25  public class SessionSearchInfo implements Serializable {
26  
27    /** The Constant serialVersionUID. */
28    private static final long serialVersionUID = 1L;
29  
30    /** The Constant logger. */
31    private static final Logger logger = LoggerFactory.getLogger(SessionSearchInfo.class);
32  
33    /** The Constant SESS_ATTR_NAME. */
34    public static final String SESS_ATTR_NAME = "sessionSearchInfo";
35  
36    /** The Constant ACTION_NONE. */
37    public static final String ACTION_NONE = "none";
38  
39    /** The Constant ACTION_APPLY. */
40    public static final String ACTION_APPLY = "apply";
41  
42    /** The Constant ACTION_CLEAR. */
43    public static final String ACTION_CLEAR = "clear";
44  
45    /** The search action. */
46    private String searchAction = ACTION_NONE;
47  
48    /** The apply. */
49    private boolean apply;
50  
51    /** The clear. */
52    private boolean clear;
53  
54    /** The session id. */
55    private String sessionId;
56  
57    /** The session id pattern. */
58    private Pattern sessionIdPattern;
59  
60    /** The session id msg. */
61    private String sessionIdMsg;
62  
63    /** The attr name. */
64    private String attrName;
65  
66    /** The attr name patterns. */
67    private final List<Pattern> attrNamePatterns = new ArrayList<>();
68  
69    /** The attr name msgs. */
70    private final List<String> attrNameMsgs = new ArrayList<>();
71  
72    /** The age from. */
73    private String ageFrom;
74  
75    /** The age from sec. */
76    private Integer ageFromSec;
77  
78    /** The age to. */
79    private String ageTo;
80  
81    /** The age to sec. */
82    private Integer ageToSec;
83  
84    /** The idle time from. */
85    private String idleTimeFrom;
86  
87    /** The idle time from sec. */
88    private Integer idleTimeFromSec;
89  
90    /** The idle time to. */
91    private String idleTimeTo;
92  
93    /** The idle time to sec. */
94    private Integer idleTimeToSec;
95  
96    /** The last ip. */
97    private String lastIp;
98  
99    /** The info message. */
100   private String infoMessage;
101 
102   /** The error messages. */
103   private final List<String> errorMessages = new ArrayList<>();
104 
105   /**
106    * Checks if is empty.
107    *
108    * @return true, if is empty
109    */
110   public boolean isEmpty() {
111     return sessionId == null && attrName == null && ageFrom == null && ageTo == null
112         && idleTimeFrom == null && idleTimeTo == null && lastIp == null;
113   }
114 
115   /**
116    * Checks if is session id valid.
117    *
118    * @return true, if is session id valid
119    */
120   public boolean isSessionIdValid() {
121     return sessionId == null || sessionIdPattern != null;
122   }
123 
124   /**
125    * Checks if is attr name valid.
126    *
127    * @return true, if is attr name valid
128    */
129   public boolean isAttrNameValid() {
130     return attrName == null || !attrNamePatterns.isEmpty();
131   }
132 
133   /**
134    * Checks if is age from valid.
135    *
136    * @return true, if is age from valid
137    */
138   public boolean isAgeFromValid() {
139     return ageFrom == null || ageFromSec != null;
140   }
141 
142   /**
143    * Checks if is age to valid.
144    *
145    * @return true, if is age to valid
146    */
147   public boolean isAgeToValid() {
148     return ageTo == null || ageToSec != null;
149   }
150 
151   /**
152    * Checks if is idle time from valid.
153    *
154    * @return true, if is idle time from valid
155    */
156   public boolean isIdleTimeFromValid() {
157     return idleTimeFrom == null || idleTimeFromSec != null;
158   }
159 
160   /**
161    * Checks if is idle time to valid.
162    *
163    * @return true, if is idle time to valid
164    */
165   public boolean isIdleTimeToValid() {
166     return idleTimeTo == null || idleTimeToSec != null;
167   }
168 
169   /**
170    * Checks if is valid.
171    *
172    * @return true, if is valid
173    */
174   public boolean isValid() {
175     return isSessionIdValid() && isAttrNameValid() && isAgeFromValid() && isAgeToValid()
176         && isIdleTimeFromValid() && isIdleTimeToValid();
177   }
178 
179   /**
180    * Checks if is use search.
181    *
182    * @return true, if is use search
183    */
184   public boolean isUseSearch() {
185     return isApply() && !isEmpty() && isValid();
186   }
187 
188   /**
189    * Checks if is use session id.
190    *
191    * @return true, if is use session id
192    */
193   public boolean isUseSessionId() {
194     return sessionIdPattern != null;
195   }
196 
197   /**
198    * Checks if is use attr name.
199    *
200    * @return true, if is use attr name
201    */
202   public boolean isUseAttrName() {
203     return !attrNamePatterns.isEmpty();
204   }
205 
206   /**
207    * Checks if is use attr.
208    *
209    * @return true, if is use attr
210    */
211   public boolean isUseAttr() {
212     return isUseSearch() && isUseAttrName();
213   }
214 
215   /**
216    * Checks if is use age from.
217    *
218    * @return true, if is use age from
219    */
220   public boolean isUseAgeFrom() {
221     return ageFromSec != null;
222   }
223 
224   /**
225    * Checks if is use age to.
226    *
227    * @return true, if is use age to
228    */
229   public boolean isUseAgeTo() {
230     return ageToSec != null;
231   }
232 
233   /**
234    * Checks if is use idle time from.
235    *
236    * @return true, if is use idle time from
237    */
238   public boolean isUseIdleTimeFrom() {
239     return idleTimeFromSec != null;
240   }
241 
242   /**
243    * Checks if is use idle time to.
244    *
245    * @return true, if is use idle time to
246    */
247   public boolean isUseIdleTimeTo() {
248     return idleTimeToSec != null;
249   }
250 
251   /**
252    * Checks if is use last ip.
253    *
254    * @return true, if is use last ip
255    */
256   public boolean isUseLastIp() {
257     return lastIp != null;
258   }
259 
260   /**
261    * Gets the search action.
262    *
263    * @return the search action
264    */
265   public String getSearchAction() {
266     return searchAction;
267   }
268 
269   /**
270    * Sets the search action.
271    *
272    * @param searchAction the new search action
273    */
274   public void setSearchAction(String searchAction) {
275     this.searchAction = searchAction;
276     if (searchAction == null) {
277       apply = false;
278       clear = false;
279     } else {
280       apply = ACTION_APPLY.equals(searchAction);
281       clear = ACTION_CLEAR.equals(searchAction);
282     }
283   }
284 
285   /**
286    * Checks if is apply.
287    *
288    * @return true, if is apply
289    */
290   public boolean isApply() {
291     return apply;
292   }
293 
294   /**
295    * Checks if is clear.
296    *
297    * @return true, if is clear
298    */
299   public boolean isClear() {
300     return clear;
301   }
302 
303   /**
304    * Gets the session id.
305    *
306    * @return the session id
307    */
308   public String getSessionId() {
309     return sessionId;
310   }
311 
312   /**
313    * Sets the session id.
314    *
315    * @param sessionId the new session id
316    */
317   public void setSessionId(String sessionId) {
318     this.sessionId = sessionId;
319     sessionIdPattern = null;
320     sessionIdMsg = null;
321 
322     if (sessionId != null) {
323       try {
324         sessionIdPattern = Pattern.compile(sessionId);
325       } catch (PatternSyntaxException e) {
326         logger.trace("", e);
327         sessionIdMsg = e.getDescription();
328       }
329     }
330   }
331 
332   /**
333    * Gets the session id pattern.
334    *
335    * @return the session id pattern
336    */
337   public Pattern getSessionIdPattern() {
338     return sessionIdPattern;
339   }
340 
341   /**
342    * Gets the session id msg.
343    *
344    * @return the session id msg
345    */
346   public String getSessionIdMsg() {
347     return sessionIdMsg;
348   }
349 
350   /**
351    * Gets the attr name.
352    *
353    * @return the attr name
354    */
355   public String getAttrName() {
356     return attrName;
357   }
358 
359   /**
360    * Sets the attr name.
361    *
362    * @param attrName the new attr name
363    */
364   public void setAttrName(String attrName) {
365     this.attrName = attrName;
366     attrNamePatterns.clear();
367     attrNameMsgs.clear();
368 
369     if (attrName != null) {
370       String[] attrNames = attrName.split(",", -1);
371       if (attrNames.length == 0 && !attrName.isEmpty()) {
372         attrNameMsgs.add("");
373       } else {
374         for (String regex : attrNames) {
375           try {
376             attrNamePatterns.add(Pattern.compile(regex));
377           } catch (PatternSyntaxException e) {
378             logger.trace("", e);
379             attrNameMsgs.add(e.getDescription());
380           }
381         }
382       }
383     }
384   }
385 
386   /**
387    * Gets the attr name patterns.
388    *
389    * @return the attr name patterns
390    */
391   public List<Pattern> getAttrNamePatterns() {
392     return new ArrayList<>(attrNamePatterns);
393   }
394 
395   /**
396    * Gets the attr name msgs.
397    *
398    * @return the attr name msgs
399    */
400   public List<String> getAttrNameMsgs() {
401     return new ArrayList<>(attrNameMsgs);
402   }
403 
404   /**
405    * Gets the age from.
406    *
407    * @return the age from
408    */
409   public String getAgeFrom() {
410     return ageFrom;
411   }
412 
413   /**
414    * Sets the age from.
415    *
416    * @param ageFrom the new age from
417    */
418   public void setAgeFrom(String ageFrom) {
419     this.ageFrom = ageFrom;
420     ageFromSec = null;
421 
422     if (ageFrom != null) {
423       try {
424         ageFromSec = Integer.valueOf(ageFrom);
425       } catch (NumberFormatException e) {
426         logger.trace("", e);
427       }
428     }
429   }
430 
431   /**
432    * Gets the age from sec.
433    *
434    * @return the age from sec
435    */
436   public Integer getAgeFromSec() {
437     return ageFromSec;
438   }
439 
440   /**
441    * Gets the age to.
442    *
443    * @return the age to
444    */
445   public String getAgeTo() {
446     return ageTo;
447   }
448 
449   /**
450    * Sets the age to.
451    *
452    * @param ageTo the new age to
453    */
454   public void setAgeTo(String ageTo) {
455     this.ageTo = ageTo;
456     ageToSec = null;
457 
458     if (ageTo != null) {
459       try {
460         ageToSec = Integer.valueOf(ageTo);
461       } catch (NumberFormatException e) {
462         logger.trace("", e);
463       }
464     }
465   }
466 
467   /**
468    * Gets the age to sec.
469    *
470    * @return the age to sec
471    */
472   public Integer getAgeToSec() {
473     return ageToSec;
474   }
475 
476   /**
477    * Gets the idle time from.
478    *
479    * @return the idle time from
480    */
481   public String getIdleTimeFrom() {
482     return idleTimeFrom;
483   }
484 
485   /**
486    * Sets the idle time from.
487    *
488    * @param idleTimeFrom the new idle time from
489    */
490   public void setIdleTimeFrom(String idleTimeFrom) {
491     this.idleTimeFrom = idleTimeFrom;
492     idleTimeFromSec = null;
493 
494     if (idleTimeFrom != null) {
495       try {
496         idleTimeFromSec = Integer.valueOf(idleTimeFrom);
497       } catch (NumberFormatException e) {
498         logger.trace("", e);
499       }
500     }
501   }
502 
503   /**
504    * Gets the idle time from sec.
505    *
506    * @return the idle time from sec
507    */
508   public Integer getIdleTimeFromSec() {
509     return idleTimeFromSec;
510   }
511 
512   /**
513    * Gets the idle time to.
514    *
515    * @return the idle time to
516    */
517   public String getIdleTimeTo() {
518     return idleTimeTo;
519   }
520 
521   /**
522    * Sets the idle time to.
523    *
524    * @param idleTimeTo the new idle time to
525    */
526   public void setIdleTimeTo(String idleTimeTo) {
527     this.idleTimeTo = idleTimeTo;
528     idleTimeToSec = null;
529 
530     if (idleTimeTo != null) {
531       try {
532         idleTimeToSec = Integer.valueOf(idleTimeTo);
533       } catch (NumberFormatException e) {
534         logger.trace("", e);
535       }
536     }
537   }
538 
539   /**
540    * Gets the idle time to sec.
541    *
542    * @return the idle time to sec
543    */
544   public Integer getIdleTimeToSec() {
545     return idleTimeToSec;
546   }
547 
548   /**
549    * Gets the last ip.
550    *
551    * @return the last ip
552    */
553   public String getLastIp() {
554     return lastIp;
555   }
556 
557   /**
558    * Sets the last ip.
559    *
560    * @param lastIp the new last ip
561    */
562   public void setLastIp(String lastIp) {
563     this.lastIp = lastIp;
564   }
565 
566   /**
567    * Gets the info message.
568    *
569    * @return the info message
570    */
571   public String getInfoMessage() {
572     return infoMessage;
573   }
574 
575   /**
576    * Sets the info message.
577    *
578    * @param infoMessage the new info message
579    */
580   public void setInfoMessage(String infoMessage) {
581     this.infoMessage = infoMessage;
582   }
583 
584   /**
585    * Gets the error messages.
586    *
587    * @return the error messages
588    */
589   public List<String> getErrorMessages() {
590     return new ArrayList<>(errorMessages);
591   }
592 
593   /**
594    * Adds the error message.
595    *
596    * @param msg the msg
597    */
598   public void addErrorMessage(String msg) {
599     errorMessages.add(msg);
600   }
601 
602 }