Java tutorial
/* Copyright (C) 2016 maik.jablonski@jease.org This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package jease.cms.service; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.function.Consumer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import jease.Registry; import jease.cmf.domain.Node; import jease.cmf.service.Nodes; import jease.cms.domain.Content; import jease.cms.domain.Factory; import jease.cms.domain.property.Property; import jease.cms.domain.property.Provider; import jease.persistence.cache.CachingNodeService; import jease.persistence.repositories.PropertyRepository; /** * Service for handling properties. */ @Service public class Properties { @Autowired private Nodes nodes; @Autowired private PropertyRepository repository; @Autowired private Registry registry; @Autowired private CachingNodeService nodeService; // private Supplier<String[]> providerPaths = () -> { // // }); // return result.toArray(new String[] {}); // }; /** * Returns array of all available property types. */ public Set<Property> getAvailableTypes() { return registry.getProperties(); } /** * Returns all existing property names stored within database. */ public List<String> getPropertyNames() { return repository.findAllNames(); } /** * Returns all paths for properties which can act as item providers for * selectable properties. */ public List<String> getProviderPaths() { final List<String> result = new ArrayList<>(); nodeService.traverse(nodes.getRoot(), new Consumer<Node>() { public void accept(Node node) { Content content = (Content) node; if (content.getProperties() != null) { for (Property property : content.getProperties()) { if (property instanceof Provider) { result.add(getPath(content, property)); } } } } }); return result; } /** * Returns path for given property contained in given content. */ public static String getPath(Content content, Property property) { String path = content.getPath(); if (path.endsWith("/")) { return path + property.getName(); } else { return content.getPath() + "/" + property.getName(); } } /** * Returns property for given path. If the path cannot be resolved, null is * returned. */ public Property getByPath(String path) { int idx = path.lastIndexOf("/"); String contentPath = path.substring(0, idx); String propertyPath = path.substring(idx + 1); Content content = (Content) nodes.getByPath(contentPath); if (content != null) { return content.getProperty(propertyPath); } return null; } /** * Returns the corresponding property factory for given node in relation to * given parent container. If no factory exists, null is returned. */ public Factory getFactory(Long parentContentId, Content content) { return getFactory((Content) nodeService.get(parentContentId), content); } /** * Returns the corresponding property factory for given node in relation to * given parent container. If no factory exists, null is returned. */ public Factory getFactory(Content parentContent, Content content) { Node parent = parentContent; while (parent != null) { for (Factory factoryCandidate : nodeService.getChildren(parent, Factory.class)) { for (Node prototype : nodeService.getChildren(factoryCandidate)) { if (prototype.getClass().equals(content.getClass())) { return factoryCandidate; } } } parent = nodeService.getParent(parent); } return null; } /** * Returns factory-synchronized properties for given content in relation to * given parent container. */ public Set<Property> getProperties(Content parent, Content content) { Factory factory = getFactory(parent, content); if (factory != null && factory != parent) { return factory.getProperties(content, nodeService); } else { return content.getProperties(); } } }