|
| 1 | +package com.condation.cms.templates.functions.impl; |
| 2 | + |
| 3 | +/*- |
| 4 | + * #%L |
| 5 | + * cms-templates |
| 6 | + * %% |
| 7 | + * Copyright (C) 2023 - 2026 CondationCMS |
| 8 | + * %% |
| 9 | + * This program is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as |
| 11 | + * published by the Free Software Foundation, either version 3 of the |
| 12 | + * License, or (at your option) any later version. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public |
| 20 | + * License along with this program. If not, see |
| 21 | + * <http://www.gnu.org/licenses/gpl-3.0.html>. |
| 22 | + * #L% |
| 23 | + */ |
| 24 | + |
| 25 | +import com.condation.cms.api.content.MapAccess; |
| 26 | +import com.condation.cms.api.db.ContentNode; |
| 27 | +import com.condation.cms.api.db.DB; |
| 28 | +import com.condation.cms.api.db.cms.ReadOnlyFile; |
| 29 | +import com.condation.cms.api.feature.features.InjectorFeature; |
| 30 | +import com.condation.cms.api.request.RequestContext; |
| 31 | +import com.condation.cms.api.utils.PathUtil; |
| 32 | +import com.condation.cms.core.content.ContentResolvingStrategy; |
| 33 | +import com.condation.cms.templates.functions.TemplateFunction; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Optional; |
| 37 | +import lombok.RequiredArgsConstructor; |
| 38 | + |
| 39 | +/** |
| 40 | + * |
| 41 | + * @author thmar |
| 42 | + */ |
| 43 | +@RequiredArgsConstructor |
| 44 | +public abstract class AbstractNodeFunction implements TemplateFunction { |
| 45 | + protected final RequestContext requestContext; |
| 46 | + |
| 47 | + protected void extendMap (Map<String, Object> node, ReadOnlyFile contentFile) { |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Object invoke(Object... params) { |
| 53 | + |
| 54 | + if (params == null || params.length == 0 ) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + if (!(params[0] instanceof String)) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + String uri = ContentResolvingStrategy.uriToPath((String)params[0]); |
| 61 | + |
| 62 | + var db = requestContext.get(InjectorFeature.class).injector().getInstance(DB.class); |
| 63 | + var contentBase = db.getReadOnlyFileSystem().contentBase(); |
| 64 | + |
| 65 | + Optional<ReadOnlyFile> contentFileOpt = ContentResolvingStrategy.resolve(uri, db); |
| 66 | + |
| 67 | + if (contentFileOpt.isPresent()) { |
| 68 | + var node_uri = PathUtil.toRelativeFile(contentFileOpt.get(), contentBase); |
| 69 | + final Optional<ContentNode> nodeByUri = db.getContent().byUri(node_uri); |
| 70 | + if (nodeByUri.isPresent()) { |
| 71 | + var node = new HashMap<String, Object>(); |
| 72 | + node.put("meta", new MapAccess(nodeByUri.get().data())); |
| 73 | + node.put("uri", PathUtil.toURL(contentFileOpt.get(), contentBase)); |
| 74 | + |
| 75 | + extendMap(node, contentFileOpt.get()); |
| 76 | + |
| 77 | + return node; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return null; |
| 82 | + } |
| 83 | +} |
0 commit comments