KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > license > PageCountValidator


1 package org.jahia.security.license;
2
3 import java.util.Enumeration JavaDoc;
4 import org.jahia.registries.ServicesRegistry;
5 import org.jahia.services.sites.JahiaSite;
6 import org.jahia.exceptions.JahiaException;
7 import org.jahia.resourcebundle.ResourceMessage;
8
9 /**
10  * <p>Title: </p>
11  * <p>Description: </p>
12  * <p>Copyright: Copyright (c) 2002</p>
13  * <p>Company: Jahia Ltd</p>
14  * @author Serge Huber
15  * @version 1.0
16  */

17
18 public class PageCountValidator extends AbstractValidator {
19
20     private static org.apache.log4j.Logger logger =
21         org.apache.log4j.Logger.getLogger(PageCountValidator.class);
22
23     public PageCountValidator(String JavaDoc name, String JavaDoc value, License license) {
24         super(name, value, license);
25     }
26
27     public boolean assertEquals(String JavaDoc value) {
28
29         int maxPages = Integer.parseInt(value);
30
31         // Check if the number of pages is not exceeding the fixed limit
32
try {
33
34             // get all the list of site
35
Enumeration JavaDoc enumeration = ServicesRegistry.getInstance()
36                                                 .getJahiaSitesService()
37                                                 .getSites();
38             JahiaSite aSite = null;
39             int nbItems = 0;
40             while( enumeration.hasMoreElements() ){
41                 aSite = (JahiaSite)enumeration.nextElement();
42
43                 nbItems = ServicesRegistry.getInstance().
44                                            getJahiaPageService().
45                                            getRealActiveNbPages (aSite.getID());
46
47                 if (nbItems > maxPages) {
48                     errorMessage = new ResourceMessage("org.jahia.security.license.PageCountValidator.invalidPageCount.label", new Integer JavaDoc(nbItems), new Integer JavaDoc(maxPages), new Integer JavaDoc(aSite.getID()));
49                     return false;
50                 }
51             }
52
53         } catch (JahiaException ex) {
54             logger.error("Error in page limit check", ex);
55             errorMessage = new ResourceMessage("org.jahia.security.license.PageCountValidator.errorInPageCountCheck.label");
56             return false;
57         }
58
59         return true;
60     }
61
62     public boolean assertInRange(String JavaDoc fromValue, String JavaDoc toValue) {
63
64         int minPages = Integer.parseInt(fromValue);
65         int maxPages = Integer.parseInt(toValue);
66
67         // Check if the number of pages is not exceeding the fixed limit
68
try {
69
70             // get all the list of site
71
Enumeration JavaDoc enumeration = ServicesRegistry.getInstance()
72                                                 .getJahiaSitesService()
73                                                 .getSites();
74             JahiaSite aSite = null;
75             int nbItems = 0;
76             while( enumeration.hasMoreElements() ){
77                 aSite = (JahiaSite)enumeration.nextElement();
78
79                 nbItems = ServicesRegistry.getInstance().
80                                            getJahiaPageService().
81                                            getRealActiveNbPages (aSite.getID());
82
83                 if ((nbItems > maxPages) || (nbItems < minPages)) {
84                     errorMessage = new ResourceMessage("org.jahia.security.license.PageCountValidator.pageCountNotInRange.label", new Integer JavaDoc(nbItems), new Integer JavaDoc(minPages), new Integer JavaDoc(maxPages), new Integer JavaDoc(aSite.getID()));
85                     return false;
86                 }
87             }
88
89         } catch (JahiaException ex) {
90             logger.error("Error in page limit check", ex);
91             errorMessage = new ResourceMessage("org.jahia.security.license.PageCountValidator.errorInPageCountCheck.label");
92             return false;
93         }
94         return true;
95     }
96
97 }
Popular Tags