001/* 002 * Copyright 2013 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.plugins.cart; 017 018import org.ametys.plugins.repository.AmetysObjectResolver; 019import org.ametys.plugins.repository.AmetysRepositoryException; 020import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 021 022/** 023 * Helper for {@link Cart} 024 * 025 */ 026public final class CartHelper 027{ 028 private static final String __PLUGIN_NODE_NAME = "cart"; 029 030 private CartHelper() 031 { 032 // Private 033 } 034 035 /** 036 * Get the root plugin storage object. 037 * @param resolver The Ametys object resolver 038 * @return the root plugin storage object. 039 * @throws AmetysRepositoryException if a repository error occurs. 040 */ 041 public static ModifiableTraversableAmetysObject getCartsNode(AmetysObjectResolver resolver) throws AmetysRepositoryException 042 { 043 try 044 { 045 ModifiableTraversableAmetysObject pluginsNode = resolver.resolveByPath("/ametys:plugins"); 046 047 ModifiableTraversableAmetysObject pluginNode = getOrCreateNode(pluginsNode, __PLUGIN_NODE_NAME, "ametys:unstructured"); 048 049 return getOrCreateNode(pluginNode, "ametys:carts", CartContainerFactory.CARTS_NODETYPE); 050 } 051 catch (AmetysRepositoryException e) 052 { 053 throw new AmetysRepositoryException("Unable to get the cart root node", e); 054 } 055 } 056 057 private static ModifiableTraversableAmetysObject getOrCreateNode(ModifiableTraversableAmetysObject parentNode, String nodeName, String nodeType) throws AmetysRepositoryException 058 { 059 ModifiableTraversableAmetysObject definitionsNode; 060 if (parentNode.hasChild(nodeName)) 061 { 062 definitionsNode = parentNode.getChild(nodeName); 063 } 064 else 065 { 066 definitionsNode = parentNode.createChild(nodeName, nodeType); 067 parentNode.saveChanges(); 068 } 069 return definitionsNode; 070 } 071 072}