Skip to content

Commit

Permalink
#98 introduce contentNode
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Dec 3, 2023
1 parent 08f3569 commit 8c72757
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 148 deletions.
10 changes: 5 additions & 5 deletions cms-api/src/main/java/com/github/thmarx/cms/api/db/Content.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
*
* @author thmar
*/
public interface Content<CN> {
public interface Content {
boolean isVisible (String uri);

List<CN> listSections(Path contentFile);
List<ContentNode> listSections(Path contentFile);

List<CN> listContent(final Path base, final String start);
List<ContentNode> listContent(final Path base, final String start);

List<CN> listDirectories(final Path base, final String start);
List<ContentNode> listDirectories(final Path base, final String start);

Optional<CN> byUri (final String uri);
Optional<ContentNode> byUri (final String uri);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.thmarx.cms.api.db;

import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.PreviewContext;
import com.github.thmarx.cms.api.utils.SectionUtil;
import java.time.Instant;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
*
* @author t.marx
*/
public record ContentNode(String uri, String name, Map<String, Object> data,
boolean directory, Map<String, ContentNode> children) {

public ContentNode(String uri, String name, Map<String, Object> data, boolean directory) {
this(uri, name, data, directory, new HashMap<String, ContentNode>());
}

public ContentNode(String uri, String name, Map<String, Object> data) {
this(uri, name, data, false, new HashMap<String, ContentNode>());
}

public boolean isDirectory() {
return directory;
}

public boolean isHidden() {
return name.startsWith(".");
}

public boolean isDraft() {
return (boolean) data().getOrDefault(Constants.MetaFields.DRAFT, false);
}

public boolean isPublished() {
if (PreviewContext.IS_PREVIEW.get()) {
return true;
}
var localDate = (Date) data.getOrDefault(Constants.MetaFields.PUBLISHED, Date.from(Instant.now()));
var now = Date.from(Instant.now());
return !isDraft() && (localDate.before(now) || localDate.equals(now));
}

public boolean isSection() {
return SectionUtil.isSection(name);
}

@Override
public boolean equals (Object other) {

if (this == other) {
return true;
}

if (other == null) {
return false;
}

if (!(other instanceof ContentNode)) {
return false;
}

var otherNode = (ContentNode)other;

return uri.equals(otherNode.uri);
}
}
4 changes: 2 additions & 2 deletions cms-api/src/main/java/com/github/thmarx/cms/api/db/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
*
* @author thmar
*/
public interface DB<CN> extends AutoCloseable{
public interface DB extends AutoCloseable{

public DBFileSystem getFileSystem();

public Content<CN> getContent();
public Content getContent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import com.github.thmarx.cms.api.db.Content;
import com.github.thmarx.cms.api.db.ContentNode;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
Expand All @@ -33,7 +34,7 @@
* @author thmar
*/
@RequiredArgsConstructor
public class FileContent implements Content<MetaData.MetaNode> {
public class FileContent implements Content {

private final FileSystem fileSystem;

Expand All @@ -43,22 +44,22 @@ public boolean isVisible(String uri) {
}

@Override
public List<MetaData.MetaNode> listSections(Path contentFile) {
public List<ContentNode> listSections(Path contentFile) {
return fileSystem.listSections(contentFile);
}

@Override
public List<MetaData.MetaNode> listContent(Path base, String start) {
public List<ContentNode> listContent(Path base, String start) {
return fileSystem.listContent(base, start);
}

@Override
public List<MetaData.MetaNode> listDirectories(Path base, String start) {
public List<ContentNode> listDirectories(Path base, String start) {
return fileSystem.listDirectories(base, start);
}

@Override
public Optional<MetaData.MetaNode> byUri(String uri) {
public Optional<ContentNode> byUri(String uri) {
return fileSystem.getMetaData().byUri(uri);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void close() throws Exception {
}

@Override
public Content<MetaData.MetaNode> getContent() {
public Content getContent() {
return content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.github.thmarx.cms.api.ModuleFileSystem;
import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.annotations.Experimental;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.DBFileSystem;
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.events.ContentChangedEvent;
Expand Down Expand Up @@ -68,11 +69,11 @@ public class FileSystem implements ModuleFileSystem, DBFileSystem {
@Getter
private final MetaData metaData = new MetaData();

public <T> Query<T> query(final BiFunction<MetaData.MetaNode, Integer, T> nodeMapper) {
public <T> Query<T> query(final BiFunction<ContentNode, Integer, T> nodeMapper) {
return new Query(new ArrayList<>(metaData.nodes().values()), nodeMapper);
}

public <T> Query<T> query(final String startURI, final BiFunction<MetaData.MetaNode, Integer, T> nodeMapper) {
public <T> Query<T> query(final String startURI, final BiFunction<ContentNode, Integer, T> nodeMapper) {

final String uri;
if (startURI.startsWith("/")) {
Expand All @@ -87,12 +88,12 @@ public <T> Query<T> query(final String startURI, final BiFunction<MetaData.MetaN
}

@Experimental
protected <T> Dimension<T, MetaData.MetaNode> createDimension(final String name, Function<MetaData.MetaNode, T> dimFunc, Class<T> type) {
protected <T> Dimension<T, ContentNode> createDimension(final String name, Function<ContentNode, T> dimFunc, Class<T> type) {
return metaData.getDataFilter().dimension(name, dimFunc, type);
}

@Experimental
protected Dimension<?, MetaData.MetaNode> getDimension(final String name) {
protected Dimension<?, ContentNode> getDimension(final String name) {
return metaData.getDataFilter().dimension(name);
}

Expand Down Expand Up @@ -146,11 +147,11 @@ public List<String> loadLines(final Path file, final Charset charset) throws IOE
return Files.readAllLines(file, charset);
}

public List<MetaData.MetaNode> listDirectories(final Path base, final String start) {
public List<ContentNode> listDirectories(final Path base, final String start) {
var startPath = base.resolve(start);
String folder = PathUtil.toRelativePath(startPath, contentBase).toString();

List<MetaData.MetaNode> nodes = new ArrayList<>();
List<ContentNode> nodes = new ArrayList<>();

if ("".equals(folder)) {
metaData.tree().values()
Expand All @@ -160,7 +161,7 @@ public List<MetaData.MetaNode> listDirectories(final Path base, final String sta
nodes.add(node);
});
} else if (folder.contains("/")) {
MetaData.MetaNode node = null;
ContentNode node = null;
var parts = folder.split("\\/");
for (var part : parts) {
if (node == null) {
Expand Down Expand Up @@ -189,12 +190,12 @@ public List<MetaData.MetaNode> listDirectories(final Path base, final String sta
return nodes;
}

public List<MetaData.MetaNode> listContent(final Path base, final String start) {
public List<ContentNode> listContent(final Path base, final String start) {
var startPath = base.resolve(start);

String folder = PathUtil.toRelativePath(startPath, contentBase).toString();

List<MetaData.MetaNode> nodes = new ArrayList<>();
List<ContentNode> nodes = new ArrayList<>();

if ("".equals(folder)) {
return metaData.listChildren("");
Expand All @@ -204,12 +205,12 @@ public List<MetaData.MetaNode> listContent(final Path base, final String start)

}

public List<MetaData.MetaNode> listSections(final Path contentFile) {
public List<ContentNode> listSections(final Path contentFile) {
String folder = PathUtil.toRelativePath(contentFile, contentBase).toString();
String filename = contentFile.getFileName().toString();
filename = filename.substring(0, filename.length() - 3);

List<MetaData.MetaNode> nodes = new ArrayList<>();
List<ContentNode> nodes = new ArrayList<>();

final Pattern isSectionOf = Constants.SECTION_OF_PATTERN.apply(filename);
final Pattern isOrderedSectionOf = Constants.SECTION_ORDERED_OF_PATTERN.apply(filename);
Expand All @@ -227,7 +228,7 @@ public List<MetaData.MetaNode> listSections(final Path contentFile) {
nodes.add(node);
});
} else {
Optional<MetaData.MetaNode> findFolder = metaData.findFolder(folder);
Optional<ContentNode> findFolder = metaData.findFolder(folder);
if (findFolder.isPresent()) {
findFolder.get().children().values()
.stream()
Expand Down
Loading

0 comments on commit 8c72757

Please sign in to comment.