1 package org.jahia.security.license; 2 3 import java.util.Enumeration ; 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 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 name, String value, License license) { 24 super(name, value, license); 25 } 26 27 public boolean assertEquals(String value) { 28 29 int maxPages = Integer.parseInt(value); 30 31 try { 33 34 Enumeration 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 (nbItems), new Integer (maxPages), new Integer (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 fromValue, String toValue) { 63 64 int minPages = Integer.parseInt(fromValue); 65 int maxPages = Integer.parseInt(toValue); 66 67 try { 69 70 Enumeration 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 (nbItems), new Integer (minPages), new Integer (maxPages), new Integer (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 |