Skip to content

Commit

Permalink
#4 first draft for taxonomies
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Dec 15, 2023
1 parent 8cfbe03 commit 709585c
Show file tree
Hide file tree
Showing 37 changed files with 835 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static class MetaFields {

public static final String REDIRECT_STATUS = "redirect.status";
public static final String REDIRECT_LOCATION = "redirect.location";

public static final String TEMPLATE = "template";
}

public static class Folders {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.thmarx.cms.api.content;

import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.taxonomy.Taxonomy;

/*-
* #%L
* cms-server
* %%
* Copyright (C) 2023 Marx-Software
* %%
* 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/gpl-3.0.html>.
* #L%
*/

/**
*
* @author t.marx
*/
public record TaxonomyResponse(String content, String contentType, Taxonomy taxonomy) {

public TaxonomyResponse (String content, Taxonomy taxonomy) {
this(content, Constants.DEFAULT_CONTENT_TYPE, taxonomy);
}

public TaxonomyResponse (Taxonomy taxonomy) {
this("", Constants.DEFAULT_CONTENT_TYPE, taxonomy);
}
};
4 changes: 4 additions & 0 deletions cms-api/src/main/java/com/github/thmarx/cms/api/db/DB.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.thmarx.cms.api.db;

import com.github.thmarx.cms.api.db.taxonomy.Taxonomies;

/*-
* #%L
* cms-api
Expand Down Expand Up @@ -31,4 +33,6 @@ public interface DB extends AutoCloseable{
public DBFileSystem getFileSystem();

public Content getContent();

public Taxonomies getTaxonomies();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.thmarx.cms.api.db.taxonomy;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 Marx-Software
* %%
* 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/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.db.ContentNode;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
*
* @author t.marx
*/
public interface Taxonomies {

public Optional<Taxonomy> forSlug (String slug);

public Map<String, Integer> valueCount (Taxonomy taxonomy);

public Set<String> values (Taxonomy taxonomy);

public List<ContentNode> withValue (Taxonomy taxonomy, Object value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.thmarx.cms.api.db.taxonomy;

/*-
* #%L
* cms-api
* %%
* Copyright (C) 2023 Marx-Software
* %%
* 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/gpl-3.0.html>.
* #L%
*/

import lombok.Data;

/**
*
* @author t.marx
*/
@Data
public class Taxonomy {
public String title;
public String slug;
public String template = "taxonomy.html";
public String field;
public boolean array = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ public void close() throws Exception {
features.values()
.stream()
.filter(AutoCloseable.class::isInstance)
.map(AutoCloseable.class::cast)
.forEach(feature -> {
try {

feature.close();
} catch (Exception e) {
log.error(null, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@
* #L%
*/

import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.content.ContentParser;
import com.github.thmarx.cms.api.db.Content;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.db.DBFileSystem;
import com.github.thmarx.cms.api.db.taxonomy.Taxonomies;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.events.SitePropertiesChanged;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.filesystem.taxonomy.FileTaxonomies;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;
import lombok.RequiredArgsConstructor;

Expand All @@ -42,17 +49,24 @@ public class FileDB implements DB {
private final Path hostBaseDirectory;
private final EventBus eventBus;
final Function<Path, Map<String, Object>> contentParser;
final SiteProperties siteProperties;

private FileSystem fileSystem;
private FileContent content;

private FileTaxonomies taxonomies;

public void init () throws IOException {
fileSystem = new FileSystem(hostBaseDirectory, eventBus, contentParser);
fileSystem.init();

content = new FileContent(fileSystem);

taxonomies = new FileTaxonomies(siteProperties, fileSystem);
taxonomies.reloadTaxonomies();
eventBus.register(SitePropertiesChanged.class, taxonomies);
}

@Override
public DBFileSystem getFileSystem() {
return fileSystem;
Expand All @@ -67,5 +81,10 @@ public void close() throws Exception {
public Content getContent() {
return content;
}

@Override
public Taxonomies getTaxonomies() {
return taxonomies;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.github.thmarx.cms.api.ServerContext;
import com.github.thmarx.cms.api.content.ContentParser;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package com.github.thmarx.cms.filesystem.taxonomy;

/*-
* #%L
* cms-filesystem
* %%
* Copyright (C) 2023 Marx-Software
* %%
* 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/gpl-3.0.html>.
* #L%
*/
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.taxonomy.Taxonomies;
import com.github.thmarx.cms.api.db.taxonomy.Taxonomy;
import com.github.thmarx.cms.api.eventbus.EventListener;
import com.github.thmarx.cms.api.eventbus.events.SitePropertiesChanged;
import com.github.thmarx.cms.api.utils.MapUtil;
import com.github.thmarx.cms.filesystem.FileSystem;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
*
* @author t.marx
*/
@Slf4j
@RequiredArgsConstructor
public class FileTaxonomies implements Taxonomies, EventListener<SitePropertiesChanged> {

private final SiteProperties siteProperties;
private final FileSystem fileSystem;

private ConcurrentMap<String, Taxonomy> taxonomies = new ConcurrentHashMap<>();

private String defaulTemplate = "taxonomy.html";

public void reloadTaxonomies() {
var tasList = (List<Map>) siteProperties.getOrDefault("taxonomy", Map.of()).getOrDefault("taxonomies", List.of());

tasList.stream().map((taxo) -> {
Taxonomy tax = new Taxonomy();
tax.setTitle((String) taxo.get("title"));
tax.setSlug((String) taxo.get("slug"));
tax.setField((String) taxo.get("field"));
tax.setTemplate((String) taxo.getOrDefault("template", defaulTemplate));
tax.setArray((Boolean) taxo.getOrDefault("array", false));
return tax;
}).forEach(tax -> taxonomies.put(tax.getSlug(), tax));
}

@Override
public Optional<Taxonomy> forSlug(final String slug) {
return Optional.ofNullable(taxonomies.get(slug));
}

@Override
public Map<String, Integer> valueCount(Taxonomy taxonomy) {
fileSystem.query((node, index) -> node).where(taxonomy.getField(), "!=", null);
return Map.of();
}

@Override
public Set<String> values(Taxonomy taxonomy) {
var nodes = fileSystem.query((node, index) -> node).where(taxonomy.getField(), "!=", null).get();

Set<String> values = new HashSet<>();
nodes.forEach(node -> {
var value = MapUtil.getValue(node.data(), taxonomy.getField());
if (value instanceof List) {
values.addAll((List) value);
} else {
values.add((String) value);
}
});

return values;
}

@Override
public List<ContentNode> withValue(final Taxonomy taxonomy, final Object value) {
List<ContentNode> nodes = null;
if (taxonomy.isArray()) {
nodes = fileSystem.query((node, index) -> node).whereContains(taxonomy.getField(), value).get();
} else {
nodes = fileSystem.query((node, index) -> node).where(taxonomy.getField(), value).get();
}

return nodes;
}

@Override
public void consum(SitePropertiesChanged event) {
reloadTaxonomies();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.thmarx.cms.filesystem.taxonomy;

/*-
* #%L
* cms-filesystem
* %%
* Copyright (C) 2023 Marx-Software
* %%
* 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/gpl-3.0.html>.
* #L%
*/
/**
*
* Template resolution:
* 1. taxonomy.html
* 2. taxonomy-taxid.html
*
*/
Loading

0 comments on commit 709585c

Please sign in to comment.