KickJava   Java API By Example, From Geeks To Geeks.

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


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