Skip to content

Commit

Permalink
- Form using into Post Page Layout
Browse files Browse the repository at this point in the history
  • Loading branch information
alegauss committed Aug 25, 2018
1 parent a46fe75 commit a3a6e5f
Show file tree
Hide file tree
Showing 18 changed files with 115 additions and 95 deletions.
48 changes: 6 additions & 42 deletions src/main/java/com/viglet/shiohara/component/ShFormComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.type.ShPostType;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;
import com.viglet.shiohara.persistence.model.widget.ShWidget;
import com.viglet.shiohara.persistence.repository.object.ShObjectRepository;
import com.viglet.shiohara.persistence.repository.post.type.ShPostTypeRepository;
import com.viglet.shiohara.persistence.repository.widget.ShWidgetRepository;
import com.viglet.shiohara.widget.ShSystemWidget;
Expand All @@ -32,12 +34,13 @@ public class ShFormComponent {
@Autowired
private SpringTemplateEngine templateEngine;
@Autowired
private ShWidgetRepository shWidgetRepository;
private ShObjectRepository shObjectRepository;

public String byPostType(String shPostTypeName, HttpServletRequest request)
public String byPostType(String shPostTypeName, String shObjectId, HttpServletRequest request)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
final Context ctx = new Context();

ShObject shObject = shObjectRepository.findById(shObjectId).get();
ShPostType shPostType = shPostTypeRepository.findByName(shPostTypeName);
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());

Expand All @@ -56,48 +59,9 @@ public int compare(ShPostTypeAttr o1, ShPostTypeAttr o2) {
String className = shPostTypeAttr.getShWidget().getClassName();
ShWidgetImplementation object = (ShWidgetImplementation) Class.forName(className).newInstance();
applicationContext.getAutowireCapableBeanFactory().autowireBean(object);
fields.add(object.render(shPostTypeAttr));
fields.add(object.render(shPostTypeAttr, shObject));
}

String token = null;
if (csrf != null) {
token = csrf.getToken();
}

ctx.setVariable("token", token);
ctx.setVariable("shPostType", shPostType);
ctx.setVariable("shPostTypeAttrs", shPostType.getShPostTypeAttrs());
ctx.setVariable("fields", fields);

return templateEngine.process("form", ctx);
}

public String byPostTypeAndFolder(String shPostTypeName, String folderId, HttpServletRequest request)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
final Context ctx = new Context();

ShPostType shPostType = shPostTypeRepository.findByName(shPostTypeName);
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());

List<String> fields = new ArrayList<String>();

List<ShPostTypeAttr> postTypeAttrByOrdinal = new ArrayList<ShPostTypeAttr>(shPostType.getShPostTypeAttrs());

Collections.sort(postTypeAttrByOrdinal, new Comparator<ShPostTypeAttr>() {

public int compare(ShPostTypeAttr o1, ShPostTypeAttr o2) {
return o1.getOrdinal() - o2.getOrdinal();
}
});

for (ShPostTypeAttr shPostTypeAttr : postTypeAttrByOrdinal) {
String className = shPostTypeAttr.getShWidget().getClassName();
ShWidgetImplementation object = (ShWidgetImplementation) Class.forName(className).newInstance();
applicationContext.getAutowireCapableBeanFactory().autowireBean(object);
fields.add(object.render(shPostTypeAttr));
}


String token = null;
if (csrf != null) {
token = csrf.getToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.viglet.shiohara.persistence.model.post.ShPostAttr;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;
import com.viglet.shiohara.persistence.repository.post.ShPostRepository;
import com.viglet.shiohara.sites.ShSitesContextURL;
import com.viglet.shiohara.utils.ShPostUtils;
import com.viglet.shiohara.widget.ShSystemWidget;

Expand All @@ -36,7 +37,8 @@ public class ShPaymentSlip {
@Autowired
private ShPostRepository shPostRepository;

public void payment(ShPost shPost, HttpServletResponse response) throws IOException {
public void payment(ShPost shPost, ShSitesContextURL shSitesContextURL) throws IOException {

Set<ShPostTypeAttr> shPostTypeAttrs = shPost.getShPostType().getShPostTypeAttrs();
String paymentTypeId = null;
JSONObject ptdSettings = null;
Expand All @@ -47,7 +49,12 @@ public void payment(ShPost shPost, HttpServletResponse response) throws IOExcept
if (shPostTypeAttr.getShWidget().getName().equals(ShSystemWidget.PAYMENT)) {
JSONObject settings = new JSONObject(shPostTypeAttr.getWidgetSettings());
product = settings.getJSONObject("product");
shProductPost = shPostRepository.findById(product.getString("post")).get();

if (shSitesContextURL.getShObject() instanceof ShPost) {
shProductPost = (ShPost) shSitesContextURL.getShObject();
} else {
shProductPost = shPostRepository.findById(product.getString("post")).get();
}
payer = settings.getJSONObject("payer");
JSONArray paymentTypes = settings.getJSONArray("paymentTypes");
for (int i = 0; i < paymentTypes.length(); i++) {
Expand Down Expand Up @@ -98,7 +105,8 @@ public void payment(ShPost shPost, HttpServletResponse response) throws IOExcept
// Quem paga o boleto
Map<String, ShPostAttr> shPostMap = shPostUtils.postToMap(shPost);
Map<String, ShPostAttr> shProductPostMap = shPostUtils.postToMap(shProductPost);
double paymentSlipValue = Double.parseDouble(shProductPostMap.get(product.getString("value")).getStrValue());
double paymentSlipValue = Double
.parseDouble(shProductPostMap.get(product.getString("value")).getStrValue());

Pagador pagador = Pagador.novoPagador().comNome(shPostMap.get(payer.getString("name")).getStrValue())
.comDocumento(shPostMap.get(payer.getString("documentId")).getStrValue());
Expand All @@ -110,17 +118,18 @@ public void payment(ShPost shPost, HttpServletResponse response) throws IOExcept
.comNumeroDoDocumento(ptdSettings.getString("documentNumber")).comInstrucoes(instructions)
.comLocaisDePagamento(ptdSettings.getString("local"));

Boleto segundaParcela = Boleto.novoBoleto().comBanco(banco).comDatas(datas2nd).comBeneficiario(beneficiario)
/* Boleto segundaParcela = Boleto.novoBoleto().comBanco(banco).comDatas(datas2nd).comBeneficiario(beneficiario)
.comPagador(pagador).comValorBoleto(paymentSlipValue)
.comNumeroDoDocumento(ptdSettings.getString("documentNumber")).comInstrucoes(instructions)
.comLocaisDePagamento(ptdSettings.getString("local"));
.comLocaisDePagamento(ptdSettings.getString("local"));*/

Boleto[] boletos = { primeraParcela, segundaParcela };
Boleto[] boletos = { primeraParcela };
GeradorDeBoleto gerador = new GeradorDeBoleto(boletos);

byte[] bPDF = gerador.geraPDF();

MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
HttpServletResponse response = shSitesContextURL.getResponse();
response.setContentType(mimetypesFileTypeMap.getContentType("PaymentSlip.pdf"));
response.setHeader("Content-disposition", "attachment; filename=PaymentSlip.pdf");
response.getOutputStream().write(bPDF);
Expand Down
35 changes: 23 additions & 12 deletions src/main/java/com/viglet/shiohara/sites/ShSitesContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.viglet.shiohara.persistence.model.post.type.ShPostType;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;
import com.viglet.shiohara.persistence.model.site.ShSite;
import com.viglet.shiohara.persistence.repository.folder.ShFolderRepository;
import com.viglet.shiohara.persistence.repository.post.ShPostRepository;
import com.viglet.shiohara.persistence.repository.post.type.ShPostTypeAttrRepository;
import com.viglet.shiohara.persistence.repository.post.type.ShPostTypeRepository;
Expand All @@ -58,6 +59,8 @@ public class ShSitesContext {
@Autowired
private ShPostRepository shPostRepository;
@Autowired
private ShFolderRepository shFolderRepository;
@Autowired
private ShFolderUtils shFolderUtils;
@Autowired
private ShPostUtils shPostUtils;
Expand All @@ -73,11 +76,11 @@ public class ShSitesContext {
private ShSitesContextURL shSitesContextURL;
@Autowired
private ShPaymentWidget shPaymentWidget;

@PostMapping("/sites/**")
private RedirectView sitesPostForm(HttpServletRequest request, HttpServletResponse response) throws IOException,
ScriptException, InstantiationException, IllegalAccessException, ClassNotFoundException {
shSitesContextURL.init(request);
shSitesContextURL.init(request, response);

this.siteContextPost(shSitesContextURL, request, response);
RedirectView redirectView = new RedirectView(
Expand All @@ -90,18 +93,25 @@ public byte[] siteContextPost(ShSitesContextURL shSitesContextURL, HttpServletRe
HttpServletResponse response) throws IOException, ScriptException, InstantiationException,
IllegalAccessException, ClassNotFoundException {
ShPost shPost = null;
if (shSitesContextURL.getShObject() instanceof ShFolder
if (request.getParameter("__sh-post-type-attr-__FORM_FOLDER_ID") != null
|| shSitesContextURL.getShObject() instanceof ShFolder
|| (shSitesContextURL.getShObject() instanceof ShPost && ((ShPost) shSitesContextURL.getShObject())
.getShPostType().getName().equals(ShSystemPostType.FOLDER_INDEX))) {
ShFolder shFolder = null;
if (shSitesContextURL.getShObject() instanceof ShFolder) {
shFolder = (ShFolder) shSitesContextURL.getShObject();

if (request.getParameter("__sh-post-type-attr-__FORM_FOLDER_ID") != null) {
shFolder = shFolderRepository.findById(request.getParameter("__sh-post-type-attr-__FORM_FOLDER_ID"))
.get();
} else {
shFolder = ((ShPost) shSitesContextURL.getShObject()).getShFolder();
if (shSitesContextURL.getShObject() instanceof ShFolder) {
shFolder = (ShFolder) shSitesContextURL.getShObject();
} else {
shFolder = ((ShPost) shSitesContextURL.getShObject()).getShFolder();
}
}
String shPostTypeName = request.getParameter("__sh-post-type");
ShPostType shPostType = shPostTypeRepository.findByName(shPostTypeName);

Enumeration<String> parameters = request.getParameterNames();
if (shPostTypeName != null) {
shPost = new ShPost();
Expand All @@ -115,7 +125,8 @@ public byte[] siteContextPost(ShSitesContextURL shSitesContextURL, HttpServletRe
String param = parameters.nextElement();
String paramValue = request.getParameter(param);

if (param.startsWith("__sh-post-type-attr-")) {
if (param.startsWith("__sh-post-type-attr-")
&& !param.equals("__sh-post-type-attr-__FORM_FOLDER_ID")) {
String attribute = param.replaceFirst("__sh-post-type-attr-", "");

ShPostTypeAttr shPostTypeAttr = shPostTypeAttrRepository.findByShPostTypeAndName(shPostType,
Expand All @@ -124,7 +135,7 @@ public byte[] siteContextPost(ShSitesContextURL shSitesContextURL, HttpServletRe
String className = shPostTypeAttr.getShWidget().getClassName();
ShWidgetImplementation object = (ShWidgetImplementation) Class.forName(className).newInstance();
applicationContext.getAutowireCapableBeanFactory().autowireBean(object);

@SuppressWarnings("unused")
boolean attrStatus = object.validateForm(request, shPostTypeAttr);
// TODO: Create validation Form logic
Expand All @@ -142,8 +153,8 @@ public byte[] siteContextPost(ShSitesContextURL shSitesContextURL, HttpServletRe
shPostAPI.postSave(shPost);
}
}
shPaymentWidget.postRender(shPost, request, response);

shPaymentWidget.postRender(shPost, shSitesContextURL);
this.sitesFullGeneric(request, response);

return null;
Expand All @@ -155,7 +166,7 @@ private void sitesFullGeneric(HttpServletRequest request, HttpServletResponse re

final int cacheMinutes = 1;

shSitesContextURL.init(request);
shSitesContextURL.init(request, response);
if (shSitesContextURL.getShSite() != null) {
if (!shSitesContextURL.isCacheEnabled()) {
this.siteContext(shSitesContextURL, request, response);
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/com/viglet/shiohara/sites/ShSitesContextURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -24,6 +25,8 @@ public class ShSitesContextURL {
@Autowired
private ShFolderUtils shFolderUtils;

private HttpServletRequest request = null;
private HttpServletResponse response = null;
private String shContext = null;
private String contextURL = null;
private ShSite shSite = null;
Expand All @@ -33,7 +36,10 @@ public class ShSitesContextURL {
private ShObject shObject = null;
private boolean cacheEnabled = true;

public void init(HttpServletRequest request) {
public void init(HttpServletRequest request, HttpServletResponse response) {
this.setRequest(request);
this.setResponse(response);

String shXSiteName = request.getHeader("x-sh-site");
cacheEnabled = request.getHeader("x-sh-nocache") != null && request.getHeader("x-sh-nocache").equals("1")
? false
Expand Down Expand Up @@ -158,4 +164,20 @@ public void setCacheEnabled(boolean cacheEnabled) {
this.cacheEnabled = cacheEnabled;
}

public HttpServletRequest getRequest() {
return request;
}

public void setRequest(HttpServletRequest request) {
this.request = request;
}

public HttpServletResponse getResponse() {
return response;
}

public void setResponse(HttpServletResponse response) {
this.response = response;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;

@Component
Expand All @@ -15,7 +16,7 @@ public class ShAceEditorWidget implements ShWidgetImplementation {
@Autowired
private SpringTemplateEngine templateEngine;

public String render(ShPostTypeAttr shPostTypeAttr) {
public String render(ShPostTypeAttr shPostTypeAttr, ShObject shObject) {
final Context ctx = new Context();
ctx.setVariable("shPostTypeAttr", shPostTypeAttr);
return templateEngine.process("widget/text/text-widget", ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;

@Component
public class ShComboBoxWidget implements ShWidgetImplementation {
@Autowired
private SpringTemplateEngine templateEngine;

public String render(ShPostTypeAttr shPostTypeAttr) {
public String render(ShPostTypeAttr shPostTypeAttr, ShObject shObject) {

String widgetSettings = shPostTypeAttr.getWidgetSettings();
JSONObject settings = new JSONObject(widgetSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;

@Component
Expand All @@ -15,7 +16,7 @@ public class ShContentSelectWidget implements ShWidgetImplementation {
@Autowired
private SpringTemplateEngine templateEngine;

public String render(ShPostTypeAttr shPostTypeAttr) {
public String render(ShPostTypeAttr shPostTypeAttr, ShObject shObject) {
final Context ctx = new Context();
ctx.setVariable("shPostTypeAttr", shPostTypeAttr);
return templateEngine.process("widget/text/text-widget", ctx);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/viglet/shiohara/widget/ShFileWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;

@Component
Expand All @@ -15,7 +16,7 @@ public class ShFileWidget implements ShWidgetImplementation {
@Autowired
private SpringTemplateEngine templateEngine;

public String render(ShPostTypeAttr shPostTypeAttr) {
public String render(ShPostTypeAttr shPostTypeAttr, ShObject shObject) {
final Context ctx = new Context();
ctx.setVariable("shPostTypeAttr", shPostTypeAttr);
return templateEngine.process("widget/text/text-widget", ctx);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/viglet/shiohara/widget/ShHTMLWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;

@Component
Expand All @@ -15,7 +16,7 @@ public class ShHTMLWidget implements ShWidgetImplementation {
@Autowired
private SpringTemplateEngine templateEngine;

public String render(ShPostTypeAttr shPostTypeAttr) {
public String render(ShPostTypeAttr shPostTypeAttr, ShObject shObject) {
final Context ctx = new Context();
ctx.setVariable("shPostTypeAttr", shPostTypeAttr);
return templateEngine.process("widget/text/text-widget", ctx);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/viglet/shiohara/widget/ShHiddenWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import com.viglet.shiohara.persistence.model.object.ShObject;
import com.viglet.shiohara.persistence.model.post.type.ShPostTypeAttr;

@Component
public class ShHiddenWidget implements ShWidgetImplementation {
@Autowired
private SpringTemplateEngine templateEngine;

public String render(ShPostTypeAttr shPostTypeAttr) {
public String render(ShPostTypeAttr shPostTypeAttr, ShObject shObject) {

String widgetSettings = shPostTypeAttr.getWidgetSettings();
JSONObject settings = new JSONObject(widgetSettings);
Expand Down
Loading

0 comments on commit a3a6e5f

Please sign in to comment.