Skip to content

Commit

Permalink
Merge pull request #14 from openviglet/0.3.1
Browse files Browse the repository at this point in the history
- Page Layout Default to Folder
  • Loading branch information
alegauss authored Jul 6, 2018
2 parents 5807020 + efa529e commit 7f1d30b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 50 deletions.
99 changes: 67 additions & 32 deletions src/main/java/com/viglet/shiohara/sites/ShSitesContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.viglet.shiohara.cache.ShCacheManager;
import com.viglet.shiohara.cache.ShCachedObject;
import com.viglet.shiohara.persistence.model.folder.ShFolder;
import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.ShPost;
import com.viglet.shiohara.persistence.model.post.ShPostAttr;
import com.viglet.shiohara.persistence.model.site.ShSite;
Expand Down Expand Up @@ -146,8 +147,8 @@ public String siteContext(String shSiteName, String shFormat, String shLocale, i
String objectName = shSitesContextComponent.objectNameFactory(contentPath);

String folderPath = shSitesContextComponent.folderPathFactory(contentPath);

ShPost shPostItem = shSitesContextComponent.shPostItemFactory(shSite, folderPath, objectName);
ShFolder shFolder = shFolderUtils.folderFromPath(shSite, folderPath);
ShObject shObjectItem = shSitesContextComponent.shObjectItemFactory(shSite, shFolder, objectName);

String javascriptVar = null;

Expand All @@ -156,20 +157,20 @@ public String siteContext(String shSiteName, String shFormat, String shLocale, i
String pageLayoutJS = null;

// Folder
if (shPostItem != null && shPostItem.getShPostType().getName().equals(ShSystemPostType.FOLDER_INDEX)) {

ShFolder shFolderItem = shSitesContextComponent.shFolderItemFactory(shPostItem);
if (shObjectItem instanceof ShFolder) {
ShFolder shFolderItem = (ShFolder) shObjectItem;

Map<String, ShPostAttr> shFolderPageLayoutMap = shSitesContextComponent
.shFolderPageLayoutMapFactory(shPostItem);
.shFolderPageLayoutMapFactory(shFolderItem, shSite);

pageLayoutHTML = shFolderPageLayoutMap.get(ShSystemPostTypeAttr.HTML).getStrValue();
pageLayoutJS = shFolderPageLayoutMap.get(ShSystemPostTypeAttr.JAVASCRIPT).getStrValue();

String shPostThemeId = shFolderPageLayoutMap.get(ShSystemPostTypeAttr.THEME).getStrValue();
JSONObject shThemeAttrs = shSitesContextComponent.shThemeFactory(shPostThemeId);

JSONObject shPostItemAttrs = shPostUtils.toJSON(shPostItem);
JSONObject shPostItemAttrs = new JSONObject();

shPostItemAttrs.put("theme", shThemeAttrs);

JSONObject shFolderItemAttrs = shFolderUtils.toJSON(shFolderItem);
Expand All @@ -181,49 +182,82 @@ public String siteContext(String shSiteName, String shFormat, String shLocale, i
shFolderItemAttrs.put("site", shSiteUtils.toJSON(shSite));

javascriptVar = "var shContent = " + shFolderItemAttrs.toString() + ";";
}
// Post
else if (shObjectItem instanceof ShPost) {
ShPost shPostItem = (ShPost) shObjectItem;

} else {
// Post
JSONObject postTypeLayout = new JSONObject();
if (shPostItem.getShPostType().getName().equals(ShSystemPostType.FOLDER_INDEX)) {
// Folder Index
ShFolder shFolderItem = null;
shFolderItem = shSitesContextComponent.shFolderItemFactory(shPostItem);

if (shSite.getPostTypeLayout() != null) {
postTypeLayout = new JSONObject(shSite.getPostTypeLayout());
}
Map<String, ShPostAttr> shFolderPageLayoutMap = shSitesContextComponent
.shFolderPageLayoutMapFactory(shPostItem, shSite);

pageLayoutHTML = shFolderPageLayoutMap.get(ShSystemPostTypeAttr.HTML).getStrValue();
pageLayoutJS = shFolderPageLayoutMap.get(ShSystemPostTypeAttr.JAVASCRIPT).getStrValue();

String pageLayoutName = (String) postTypeLayout.get(shPostItem.getShPostType().getName());
List<ShPost> shPostPageLayouts = shPostRepository.findByTitle(pageLayoutName);
String shPostThemeId = shFolderPageLayoutMap.get(ShSystemPostTypeAttr.THEME).getStrValue();
JSONObject shThemeAttrs = shSitesContextComponent.shThemeFactory(shPostThemeId);

JSONObject shPostItemAttrs = new JSONObject();

shPostItemAttrs.put("theme", shThemeAttrs);

Map<String, ShPostAttr> shPostPageLayoutMap = null;
JSONObject shFolderItemAttrs = shFolderUtils.toJSON(shFolderItem);

if (shPostPageLayouts != null) {
for (ShPost shPostPageLayout : shPostPageLayouts) {
if (shPostUtils.getSite(shPostPageLayout).getId().equals(shSite.getId())) {
shPostPageLayoutMap = shPostUtils.postToMap(shPostPageLayout);
shFolderItemAttrs.put("theme", shThemeAttrs);
shFolderItemAttrs.put("posts", shSitesContextComponent.shPostItemsFactory(shFolderItem));
shFolderItemAttrs.put("folders", shSitesContextComponent.shChildFolderItemsFactory(shFolderItem));
shFolderItemAttrs.put("post", shPostItemAttrs);
shFolderItemAttrs.put("site", shSiteUtils.toJSON(shSite));

javascriptVar = "var shContent = " + shFolderItemAttrs.toString() + ";";

} else {
// Other Post
JSONObject postTypeLayout = new JSONObject();

if (shSite.getPostTypeLayout() != null) {
postTypeLayout = new JSONObject(shSite.getPostTypeLayout());
}

String pageLayoutName = (String) postTypeLayout.get(shPostItem.getShPostType().getName());
List<ShPost> shPostPageLayouts = shPostRepository.findByTitle(pageLayoutName);

Map<String, ShPostAttr> shPostPageLayoutMap = null;

if (shPostPageLayouts != null) {
for (ShPost shPostPageLayout : shPostPageLayouts) {
if (shPostUtils.getSite(shPostPageLayout).getId().equals(shSite.getId())) {
shPostPageLayoutMap = shPostUtils.postToMap(shPostPageLayout);

}
}
}
}

if (shPostPageLayoutMap != null) {
if (shPostPageLayoutMap != null) {

pageLayoutHTML = shPostPageLayoutMap.get(ShSystemPostTypeAttr.HTML).getStrValue();
pageLayoutJS = shPostPageLayoutMap.get(ShSystemPostTypeAttr.JAVASCRIPT).getStrValue();
pageLayoutHTML = shPostPageLayoutMap.get(ShSystemPostTypeAttr.HTML).getStrValue();
pageLayoutJS = shPostPageLayoutMap.get(ShSystemPostTypeAttr.JAVASCRIPT).getStrValue();

String shPostThemeId = shPostPageLayoutMap.get(ShSystemPostTypeAttr.THEME).getStrValue();
JSONObject shThemeAttrs = shSitesContextComponent.shThemeFactory(shPostThemeId);
String shPostThemeId = shPostPageLayoutMap.get(ShSystemPostTypeAttr.THEME).getStrValue();
JSONObject shThemeAttrs = shSitesContextComponent.shThemeFactory(shPostThemeId);

JSONObject shSiteItemAttrs = shSiteUtils.toJSON(shSite);
JSONObject shSiteItemAttrs = shSiteUtils.toJSON(shSite);

JSONObject shPostItemAttrs = shPostUtils.toJSON(shPostItem);
JSONObject shPostItemAttrs = shPostUtils.toJSON(shPostItem);

shPostItemAttrs.put("theme", shThemeAttrs);
shPostItemAttrs.put("site", shSiteItemAttrs);
shPostItemAttrs.put("theme", shThemeAttrs);
shPostItemAttrs.put("site", shSiteItemAttrs);

javascriptVar = "var shContent = " + shPostItemAttrs.toString() + ";";

javascriptVar = "var shContent = " + shPostItemAttrs.toString() + ";";
}

}
}

String shPageLayoutHTML = shSitesContextComponent.shPageLayoutFactory(javascriptVar, pageLayoutJS,
pageLayoutHTML, request, shSite);

Expand All @@ -232,5 +266,6 @@ public String siteContext(String shSiteName, String shFormat, String shLocale, i
response.getWriter().write(shPageLayoutHTML);

return shPageLayoutHTML;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.web.servlet.HandlerMapping;

import com.viglet.shiohara.persistence.model.folder.ShFolder;
import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.ShPost;
import com.viglet.shiohara.persistence.model.post.ShPostAttr;
import com.viglet.shiohara.persistence.model.site.ShSite;
Expand Down Expand Up @@ -117,19 +118,19 @@ public String objectNameFactory(ArrayList<String> contentPath) {
return objectName;
}

public ShPost shPostItemFactory(ShSite shSite, String folderPath, String objectName) {
ShPost shPostItem = null;
public ShObject shObjectItemFactory(ShSite shSite, ShFolder shFolder, String objectName) {
ShObject shObjectItem = null;

// If shPostItem is not null, so is a Post, otherwise is a Folder
if (objectName != null) {
ShFolder shParentFolder = shFolderUtils.folderFromPath(shSite, folderPath);
shPostItem = shPostRepository.findByShFolderAndFurl(shParentFolder, objectName);
ShFolder shParentFolder = shFolder;
shObjectItem = shPostRepository.findByShFolderAndFurl(shParentFolder, objectName);
}

if (shPostItem != null) {
shPostItem = this.shPostAlias(shPostItem);
if (shObjectItem != null) {
shObjectItem = this.shPostAlias((ShPost) shObjectItem);
} else {
String folderPathCurrent = folderPath;
String folderPathCurrent = shFolderUtils.folderPath(shFolder, true);
if (objectName != null) {
folderPathCurrent = folderPathCurrent + objectName + "/";
}
Expand All @@ -139,11 +140,13 @@ public ShPost shPostItemFactory(ShSite shSite, String folderPath, String objectN
ShPost shFolderIndex = shPostRepository.findByShFolderAndFurl(shFolderItem, "index");

if (shFolderIndex != null) {
shPostItem = shFolderIndex;
shObjectItem = shFolderIndex;
} else {
shObjectItem = shFolderItem;
}
}
}
return shPostItem;
return shObjectItem;
}

public JSONObject shThemeFactory(String postThemeId) {
Expand Down Expand Up @@ -184,18 +187,40 @@ public JSONArray shChildFolderItemsFactory(ShFolder shFolderItem) {

public ShFolder shFolderItemFactory(ShPost shPostItem) {
ShFolder shFolderItem = null;
if (shPostItem.getShPostType().getName().equals(ShSystemPostType.FOLDER_INDEX)) {
if (shPostItem != null && shPostItem.getShPostType().getName().equals(ShSystemPostType.FOLDER_INDEX)) {
shFolderItem = shPostItem.getShFolder();
}

return shFolderItem;
}

public Map<String, ShPostAttr> shFolderPageLayoutMapFactory(ShPost shPostItem) {
Map<String, ShPostAttr> shFolderIndexMap = shPostUtils.postToMap(shPostItem);
public Map<String, ShPostAttr> shFolderPageLayoutMapFactory(ShObject shObjectItem, ShSite shSite) {
String shPostFolderPageLayoutId = null;
ShPost shFolderPageLayout = null;

if (shObjectItem instanceof ShPost) {
Map<String, ShPostAttr> shFolderIndexMap = shPostUtils.postToMap((ShPost) shObjectItem);
shPostFolderPageLayoutId = shFolderIndexMap.get(ShSystemPostTypeAttr.PAGE_LAYOUT).getStrValue();

String shPostFolderPageLayoutId = shFolderIndexMap.get(ShSystemPostTypeAttr.PAGE_LAYOUT).getStrValue();
ShPost shFolderPageLayout = shPostRepository.findById(shPostFolderPageLayoutId).get();
if (shPostFolderPageLayoutId != null) {
shFolderPageLayout = shPostRepository.findById(shPostFolderPageLayoutId).get();
}
} else if (shObjectItem instanceof ShFolder) {
// If Folder doesn't have PageLayout, it will try use default Folder Page Layout
if (shSite.getPostTypeLayout() != null) {
JSONObject postTypeLayout = new JSONObject(shSite.getPostTypeLayout());
String pageLayoutName = (String) postTypeLayout.get("FOLDER");
List<ShPost> shPostPageLayouts = shPostRepository.findByTitle(pageLayoutName);

if (shPostPageLayouts != null) {
for (ShPost shPostPageLayout : shPostPageLayouts) {
if (shPostUtils.getSite(shPostPageLayout).getId().equals(shSite.getId())) {
shFolderPageLayout = shPostPageLayout;
}
}
}
}
}

return shPostUtils.postToMap(shFolderPageLayout);
}
Expand All @@ -218,8 +243,8 @@ public String shPageLayoutFactory(String javascriptVar, String pageLayoutJS, Str
return this.shRegionFactory(engine, bindings, javascriptVar, pageLayoutResult.toString(), shSite).html();
}

private Document shRegionFactory(ScriptEngine engine, Bindings bindings, String javascriptVar, String regionResult, ShSite shSite)
throws IOException, ScriptException {
private Document shRegionFactory(ScriptEngine engine, Bindings bindings, String javascriptVar, String regionResult,
ShSite shSite) throws IOException, ScriptException {
StringBuilder shObjectJS = this.shObjectJSFactory();

Document doc = Jsoup.parse(regionResult);
Expand Down Expand Up @@ -254,8 +279,8 @@ private Document shRegionFactory(ScriptEngine engine, Bindings bindings, String

Object regionResultChild = engine.eval(javascript, bindings);

element.html(
this.shRegionFactory(engine, bindings, javascriptVar, regionResultChild.toString(), shSite).html());
element.html(this.shRegionFactory(engine, bindings, javascriptVar, regionResultChild.toString(), shSite)
.html());
}
}

Expand Down

0 comments on commit 7f1d30b

Please sign in to comment.