001/* 002 * Copyright 2025 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.helper; 017 018import javax.jcr.Node; 019import javax.jcr.RepositoryException; 020 021import org.apache.avalon.framework.component.Component; 022import org.apache.avalon.framework.logger.AbstractLogEnabled; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026 027import org.ametys.plugins.repository.AmetysObjectResolver; 028import org.ametys.plugins.repository.AmetysRepositoryException; 029import org.ametys.plugins.repository.RepositoryConstants; 030import org.ametys.plugins.repository.jcr.JCRTraversableAmetysObject; 031import org.ametys.plugins.repository.jcr.NameHelper; 032 033/** 034 * Component for identifiers based on an incremental counter 035 */ 036public class AmetysIdentifiers extends AbstractLogEnabled implements Component, Serviceable 037{ 038 /** The Avalon role */ 039 public static final String ROLE = AmetysIdentifiers.class.getName(); 040 041 private static final String __ROOT_IDENTIFIERS = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":identifiers"; 042 043 private AmetysObjectResolver _resolver; 044 045 public void service(ServiceManager manager) throws ServiceException 046 { 047 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 048 } 049 050 /** 051 * Read counter value for a given category and counter 052 * @param categoryName The plugin name 053 * @param counterName The counter name 054 * @return the counter value or 0 if no counter found 055 * @throws AmetysRepositoryException if failed to get counter 056 */ 057 public long readCounter(String categoryName, String counterName) throws AmetysRepositoryException 058 { 059 try 060 { 061 Node counterNode = _getOrCreateCounterNode(categoryName).getNode(); 062 063 if (counterNode.hasProperty(counterName)) 064 { 065 return counterNode.getProperty(counterName).getLong(); 066 } 067 068 return 0; // Start at 0 if no counter 069 } 070 catch (RepositoryException e) 071 { 072 throw new AmetysRepositoryException("Fail to get incremental counter for type " + categoryName + " / " + counterName, e); 073 } 074 } 075 076 /** 077 * Save counter value for a given category and counter 078 * @param categoryName The plugin name 079 * @param counterName The counter name 080 * @param counter the counter value 081 * @throws AmetysRepositoryException if failed to save counter 082 */ 083 public void saveCounter(String categoryName, String counterName, long counter) throws AmetysRepositoryException 084 { 085 try 086 { 087 Node counterNode = _getOrCreateCounterNode(categoryName).getNode(); 088 counterNode.setProperty(counterName, counter); 089 } 090 catch (RepositoryException e) 091 { 092 throw new AmetysRepositoryException("Fail to save incremental counter for type " + categoryName + " / " + counterName, e); 093 } 094 } 095 096 private JCRTraversableAmetysObject _getOrCreateCounterNode(String categoryName) throws AmetysRepositoryException 097 { 098 JCRTraversableAmetysObject rootNode = _resolver.resolveByPath("/"); 099 JCRTraversableAmetysObject pluginsNode = _getOrCreateNode(rootNode, __ROOT_IDENTIFIERS, "ametys:unstructured"); 100 return _getOrCreateNode(pluginsNode, NameHelper.filterName(categoryName), "ametys:unstructured"); 101 } 102 103 private JCRTraversableAmetysObject _getOrCreateNode(JCRTraversableAmetysObject parentNode, String nodeName, String nodeType) throws AmetysRepositoryException 104 { 105 JCRTraversableAmetysObject definitionsNode; 106 if (parentNode.hasChild(nodeName)) 107 { 108 definitionsNode = parentNode.getChild(nodeName); 109 } 110 else 111 { 112 definitionsNode = parentNode.createChild(nodeName, nodeType); 113 parentNode.saveChanges(); 114 } 115 return definitionsNode; 116 } 117}