Commit 27d59c15 by Tuukka Kivilahti

database and basic functionality

1 parent d83e9d33
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.beans;
import fi.codecrew.moya.model.Help;
import fi.codecrew.moya.model.HelpText;
import javax.ejb.Local;
import java.util.Locale;
@Local
public interface HelpBeanLocal {
HelpText getHelpForPage(String pagepath, Locale locale);
Help findOrCreateHelp(String pagepath);
HelpText saveOrCreate(HelpText help);
}
......@@ -343,6 +343,54 @@ public class BootstrapBean implements BootstrapBeanLocal {
deleteMenu("/useradmin/changePassword");
dbUpdates.add(new String[] {
"CREATE TABLE helps (id SERIAL NOT NULL, path TEXT, meta json, PRIMARY KEY (id))",
"CREATE TABLE help_texts (id SERIAL NOT NULL, " +
"title TEXT, " +
"description TEXT, " +
"locale TEXT, " +
"meta json, " +
"last_edit_time TIMESTAMPTZ NOT NULL, " +
"users_id INTEGER NOT NULL, " +
"helps_id INTEGER NOT NULL, " +
"PRIMARY KEY (id))",
"ALTER TABLE help_texts " +
"ADD CONSTRAINT FK_help_texts_users_id " +
"FOREIGN KEY (users_id) REFERENCES users (id)",
"ALTER TABLE help_texts " +
"ADD CONSTRAINT FK_help_texts_helps_id " +
"FOREIGN KEY (helps_id) REFERENCES helps (id)",
"CREATE TABLE help_text_histories (id SERIAL NOT NULL, " +
"title TEXT, " +
"description TEXT, " +
"meta json, " +
"edit_time TIMESTAMPTZ NOT NULL, " +
"help_texts_id INTEGER NOT NULL, " +
"users_id INTEGER NOT NULL, " +
"PRIMARY KEY (id))",
"ALTER TABLE help_text_histories " +
"ADD CONSTRAINT FK_help_text_histories_help_texts_id " +
"FOREIGN KEY (help_texts_id) REFERENCES help_texts (id)",
});
dbUpdates.add(new String[] {
"ALTER TABLE help_texts DROP COLUMN users_id;",
"ALTER TABLE help_texts ADD COLUMN event_users_id INTEGER NOT NULL;",
"ALTER TABLE help_text_histories DROP COLUMN users_id;",
"ALTER TABLE help_text_histories ADD COLUMN event_users_id INTEGER NOT NULL;",
"ALTER TABLE help_texts " +
"ADD CONSTRAINT FK_help_texts_event_users_id " +
"FOREIGN KEY (event_users_id) REFERENCES event_users (id)",
"ALTER TABLE help_text_histories " +
"ADD CONSTRAINT FK_help_text_histories_event_users_id " +
"FOREIGN KEY (event_users_id) REFERENCES event_users (id)",
});
}
public BootstrapBean() {
......
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.beans;
import fi.codecrew.moya.enums.ValidLocale;
import fi.codecrew.moya.facade.HelpFacade;
import fi.codecrew.moya.facade.HelpTextFacade;
import fi.codecrew.moya.facade.HelpTextHistoryFacade;
import fi.codecrew.moya.model.Help;
import fi.codecrew.moya.model.HelpText;
import fi.codecrew.moya.model.HelpTextHistory;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import java.util.Calendar;
import java.util.Locale;
/**
* Session Bean implementation class SitePageBean
*/
@Stateless
@LocalBean
public class HelpBean implements HelpBeanLocal {
@EJB
HelpFacade helpFacade;
@EJB
HelpTextFacade helpTextFacade;
@EJB
HelpTextHistoryFacade helpTextHistoryFacade;
@EJB
PermissionBean permissionBean;
/**
* Default constructor.
*/
public HelpBean() {
// TODO Auto-generated constructor stub
}
/**
* Finds help for specific page on wanted locale.
*
* @param pagepath path or "key" of page
* @param locale Wanted locale
* @return Help for wanted locale, if there is no help for that locale return help on default locale (or null if there is no help for default locale)
*/
@Override
public HelpText getHelpForPage(String pagepath, Locale locale) {
ValidLocale validLocale = ValidLocale.findFromLocale(locale);
HelpText help = helpTextFacade.find(pagepath, validLocale);
if(help == null && validLocale != ValidLocale.getDefaultLocale()) {
help = helpTextFacade.find(pagepath, ValidLocale.getDefaultLocale());
}
return help;
}
@Override
public Help findOrCreateHelp(String pagepath) {
Help help = helpFacade.findByPath(pagepath);
if (help == null) {
help = new Help(pagepath);
helpFacade.create(help);
}
return help;
}
@Override
public HelpText saveOrCreate(HelpText help) {
help.setLastEdited(Calendar.getInstance().getTime());
help.setLastEditor(permissionBean.getCurrentUser());
if(help.getId() == null) {
helpTextFacade.create(help);
} else {
helpTextFacade.merge(help);
}
HelpTextHistory history = new HelpTextHistory(help);
helpTextHistoryFacade.create(history);
return help;
}
}
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.facade;
import fi.codecrew.moya.model.Help;
import fi.codecrew.moya.model.Help_;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
/**
* Created by tuukka on 19/12/15.
*/
@Stateless
@LocalBean
public class HelpFacade extends IntegerPkGenericFacade<Help> {
private static final Logger logger = LoggerFactory.getLogger(HelpFacade.class);
public HelpFacade() {
super(Help.class);
}
public Help findByPath(String pagepath) {
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<Help> cq = cb.createQuery(Help.class);
Root<Help> root = cq.from(Help.class);
cq.where(cb.equal(root.get(Help_.path), pagepath));
TypedQuery<Help> query = getEm().createQuery(cq);
query.setMaxResults(1);
return super.getSingleNullableResult(query);
}
}
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.facade;
import fi.codecrew.moya.enums.ValidLocale;
import fi.codecrew.moya.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
/**
* Created by tuukka on 19/12/15.
*/
@Stateless
@LocalBean
public class HelpTextFacade extends IntegerPkGenericFacade<HelpText> {
private static final Logger logger = LoggerFactory.getLogger(HelpTextFacade.class);
public HelpTextFacade() {
super(HelpText.class);
}
public HelpText find(String path, ValidLocale locale) {
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<HelpText> cq = cb.createQuery(HelpText.class);
Root<HelpText> root = cq.from(HelpText.class);
cq.where(
cb.and(cb.equal(root.get(HelpText_.help).get(Help_.path), path),
cb.equal(root.get(HelpText_.locale), locale))
);
TypedQuery<HelpText> query = getEm().createQuery(cq);
query.setMaxResults(1);
return super.getSingleNullableResult(query);
}
}
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.facade;
import fi.codecrew.moya.model.HelpText;
import fi.codecrew.moya.model.HelpTextHistory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
/**
* Created by tuukka on 19/12/15.
*/
@Stateless
@LocalBean
public class HelpTextHistoryFacade extends IntegerPkGenericFacade<HelpTextHistory> {
private static final Logger logger = LoggerFactory.getLogger(HelpTextHistoryFacade.class);
public HelpTextHistoryFacade() {
super(HelpTextHistory.class);
}
}
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fi.codecrew.moya.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
* Group for lectures, so you can set limits how many of these the user can
* choose
*/
@Entity
@Table(name = "helps")
public class Help extends GenericEntity implements Cloneable {
private static final long serialVersionUID = 1L;
@Column(name = "path")
private String path;
public Help(String path) {
this.path = path;
}
public Help() {
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fi.codecrew.moya.model;
import fi.codecrew.moya.enums.ValidLocale;
import javax.persistence.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* Group for lectures, so you can set limits how many of these the user can
* choose
*/
@Entity
@Table(name = "help_texts")
public class HelpText extends GenericEntity implements Cloneable {
private static final long serialVersionUID = 1L;
@Column(name = "title")
private String title;
@Column(name = "description")
private String text;
@ManyToOne()
@JoinColumn(name = "helps_id", nullable = false)
private Help help;
@Enumerated(EnumType.STRING)
@Column(name = "locale")
private ValidLocale locale;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_edit_time")
private Date lastEdited;
@ManyToOne()
@JoinColumn(name = "event_users_id", nullable = false)
private EventUser lastEditor;
public HelpText(Help help, ValidLocale locale) {
this.help = help;
this.locale = locale;
}
public HelpText() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Help getHelp() {
return help;
}
public void setHelp(Help help) {
this.help = help;
}
public ValidLocale getLocale() {
return locale;
}
public void setLocale(ValidLocale locale) {
this.locale = locale;
}
public Date getLastEdited() {
return lastEdited;
}
public void setLastEdited(Date lastEdited) {
this.lastEdited = lastEdited;
}
public EventUser getLastEditor() {
return lastEditor;
}
public void setLastEditor(EventUser lastEditor) {
this.lastEditor = lastEditor;
}
}
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fi.codecrew.moya.model;
import javax.persistence.*;
import java.util.Date;
/**
* Group for lectures, so you can set limits how many of these the user can
* choose
*/
@Entity
@Table(name = "help_text_histories")
public class HelpTextHistory extends GenericEntity implements Cloneable {
private static final long serialVersionUID = 1L;
@ManyToOne()
@JoinColumn(name = "help_texts_id", nullable = false)
HelpText helpText;
@Column(name = "title")
private String title;
@Column(name = "description")
private String text;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "edit_time")
private Date edited;
@ManyToOne()
@JoinColumn(name = "event_users_id", nullable = false)
private EventUser editor;
public HelpTextHistory() {
}
/**
* Create new history entity for specific helpText
* @param parent
*/
public HelpTextHistory(HelpText parent) {
this.title = parent.getTitle();
this.text = parent.getText();
this.edited = parent.getLastEdited();
this.editor = parent.getLastEditor();
this.helpText = parent;
}
public HelpText getHelpText() {
return helpText;
}
public void setHelpText(HelpText helpText) {
this.helpText = helpText;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Date getEdited() {
return edited;
}
public void setEdited(Date edited) {
this.edited = edited;
}
public EventUser getEditor() {
return editor;
}
public void setEditor(EventUser editor) {
this.editor = editor;
}
}
......@@ -8,14 +8,13 @@
<property name="eclipselink.cache.size.default" value="16384" />
<property name="eclipselink.logging.logger" value="ServerLogger" />
<property name="eclipselink.jdbc.uppercase-columns" value="false" />
<property name="eclipselink.target-database"
value="fi.codecrew.moya.database.eclipselink.MoyaPostgreSQLPlatform" />
<property name="eclipselink.target-database" value="fi.codecrew.moya.database.eclipselink.MoyaPostgreSQLPlatform" />
<property name="eclipselink.create-ddl-jdbc-file-name" value="moyaCreateDDL.sql" />
<property name="eclipselink.drop-ddl-jdbc-file-name" value="moyaDropDDL.sql" />
<property name="eclipselink.target-server" value="Glassfish" />
<property name="eclipselink.session.customizer"
value="fi.codecrew.moya.database.eclipselink.MoyaSessionCustomizer" />
<property name="eclipselink.ddl-generation" value="none"/>
<property name="eclipselink.session.customizer" value="fi.codecrew.moya.database.eclipselink.MoyaSessionCustomizer" />
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
</properties>
</persistence-unit>
......
......@@ -16,7 +16,7 @@
* future versions of the Software.
*
*/
package fi.codecrew.moya.web;
package fi.codecrew.moya.enums;
import java.util.Locale;
......@@ -34,4 +34,19 @@ public enum ValidLocale {
public Locale getLocale() {
return locale;
}
public static ValidLocale getDefaultLocale() {
return ValidLocale.ENGLISH;
}
public static ValidLocale findFromLocale(Locale locale) {
for(ValidLocale l : values()) {
if(l.getLocale().equals(locale)) {
return l;
}
}
return ValidLocale.getDefaultLocale();
}
}
\ No newline at end of file
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:composite="http://java.sun.com/jsf/composite" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:tools="http://java.sun.com/jsf/composite/tools"
xmlns:p="http://primefaces.org/ui"
>
<composite:interface>
</composite:interface>
<composite:implementation>
<b><h:outputText value="#{helpView.helpTitle}" escape="false" /></b><br />
<h:outputText value="#{helpView.helpText}" escape="false" /><br />
<h:form>
<p:inputText value="#{helpView.helpTitle}" /><br />
<p:inputTextarea value="#{helpView.helpText}" /><br />
<p:commandButton actionListener="#{helpView.saveHelpText}" />
</h:form>
</composite:implementation>
</html>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html>
<html class="no-js" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:tools="http://java.sun.com/jsf/composite/cditools" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:p="http://primefaces.org/ui">
xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:p="http://primefaces.org/ui"
xmlns:help="http://java.sun.com/jsf/composite/cditools/help"
>
<f:view contentType="text/html" locale="#{sessionHandler.locale}">
......@@ -48,7 +50,9 @@
<p:dialog widgetVar="keepAliveDialog" showEffect="fade" hideEffect="explode" header="#{i18n['template.keepaliveError.title']}" modal="true">
<br />#{i18n['template.keepaliveError']}<br /><br />
<p:commandButton value="OK" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" onclick="location.reload();" />
<h:form>
<p:commandButton value="OK" type="button" styleClass="ui-confirmdialog-yes" icon="ui-icon-check" onclick="location.reload();" />
</h:form>
</p:dialog>
<script type="text/javascript">
......@@ -217,6 +221,10 @@
</p:layoutUnit>
<p:layoutUnit position="east" resizable="true">
<help:helptool />
</p:layoutUnit>
<p:layoutUnit position="south" size="30">
<footer class="bgColor1">
<h:outputText id="cdiloop" value="#{conversationKeepaliveView.date}">
......
......@@ -22,6 +22,7 @@ import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import fi.codecrew.moya.enums.ValidLocale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.web.cdiview.help;
import fi.codecrew.moya.beans.HelpBeanLocal;
import fi.codecrew.moya.enums.ValidLocale;
import fi.codecrew.moya.handler.SessionStore;
import fi.codecrew.moya.model.Help;
import fi.codecrew.moya.model.HelpText;
import fi.codecrew.moya.web.helper.LayoutView;
import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
/**
* Created by tuukka on 19/12/15.
*/
@Named
@RequestScoped
public class HelpView {
// layoutview.getPagepath()
@Inject
LayoutView layoutView;
@EJB
HelpBeanLocal helpBean;
String helpTitle = null;
String helpText = null;
@Inject
private transient SessionStore sessionstore;
public void populateHelpText() {
HelpText help = helpBean.getHelpForPage(layoutView.getPagepath(), sessionstore.getLocale());
if(help == null) {
helpTitle = helpText = "";
} else {
helpTitle = help.getTitle();
helpText = help.getText();
}
}
public void saveHelpText() {
if(helpText == null || helpText.trim().isEmpty()) {
// todo: errormessage
return;
}
HelpText help = helpBean.getHelpForPage(layoutView.getPagepath(), sessionstore.getLocale());
if(help == null) {
help = new HelpText(helpBean.findOrCreateHelp(layoutView.getPagepath()), ValidLocale.findFromLocale(sessionstore.getLocale()));
}
help.setText(getHelpText());
help.setTitle(getHelpTitle());
helpBean.saveOrCreate(help);
}
public String getHelpTitle() {
if(helpTitle == null) {
populateHelpText();
}
return helpTitle;
}
public void setHelpTitle(String helpTitle) {
this.helpTitle = helpTitle;
}
public void setHelpText(String helpText) {
this.helpText = helpText;
}
public String getHelpText() {
return this.helpText;
}
}
......@@ -77,8 +77,6 @@ public class MenuView {
public List<PageContent> getPagecontent(String pagekey)
{
String key = new StringBuilder(layoutview.getPagepath()).append(":").append(pagekey).toString();
// Removed by tkfftk, enought is enought
// logger.debug("Getting pagecontent for key {}. Matches: {}", key, contents.containsKey(key));
if (!contents.containsKey(key)) {
contents.put(key, pagebean.findContentsForUser(key, sessionstore.getLocale()));
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!