Commit 84d6c52c by Juho Juopperi Committed by Tuomas Riihimäki

start of viplist

1 parent e3c61103
package fi.codecrew.moya.beans;
import fi.codecrew.moya.model.Vip;
import javax.ejb.Local;
import java.util.List;
import java.util.Set;
@Local
public interface VipBeanLocal {
List<Vip> getAvailableVips();
}
......@@ -29,7 +29,7 @@ import javax.ejb.Startup;
import fi.codecrew.moya.facade.DBModelFacade;
import fi.codecrew.moya.facade.UserFacade;
import fi.codecrew.moya.model.DBModel;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.sql.DDLBuilder;
@Singleton
@Startup
......@@ -257,6 +257,19 @@ public class BootstrapBean implements BootstrapBeanLocal {
});
dbUpdates.add(new String[]{
new DDLBuilder().createTable("vips")
.serialPK("id")
.text("description", false)
.timestampTZ("created", false, "default", "now()")
.reference("event_id", false, "events")
.reference("vip_event_user_id", true, "event_users")
.reference("creator_event_user_id", false, "event_users")
.reference("host_event_user_id", false, "event_users")
.field("meta", "json", true)
.toString()
});
}
public BootstrapBean() {
......
package fi.codecrew.moya.beans;
import fi.codecrew.moya.enums.apps.VipPermission;
import fi.codecrew.moya.facade.VipFacade;
import fi.codecrew.moya.model.Vip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import java.util.List;
@Stateless
@LocalBean
@DeclareRoles({
VipPermission.S_VIEW,
VipPermission.S_USAGE,
VipPermission.S_EDIT
})
public class VipBean implements VipBeanLocal {
private static final Logger log = LoggerFactory.getLogger(VipBean.class);
@EJB
private EventBeanLocal eventBean;
//@EJB
//private PermissionBeanLocal permissionBean;
//@EJB
//private VipFacade vipFacade;
@Override
@RolesAllowed({VipPermission.S_VIEW})
public List<Vip> getAvailableVips() {
return eventBean.getCurrentEvent().getVips();
}
}
package fi.codecrew.moya.facade;
import fi.codecrew.moya.model.Vip;
public class VipFacade extends IntegerPkGenericFacade<Vip> {
public VipFacade() {
super(Vip.class);
}
}
......@@ -39,6 +39,21 @@
<version>${moya.version}</version>
</dependency>
<!--
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>3.2</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-web</artifactId>
<version>4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<parent>
<groupId>fi.codecrew.moya</groupId>
......
package fi.codecrew.moya.beans;
import org.testng.annotations.BeforeTest;
import javax.ejb.embeddable.EJBContainer;
import javax.naming.NamingException;
public abstract class AbstractEjbTest {
@BeforeTest
public void setUpInjector() throws NamingException {
EJBContainer container = EJBContainer.createEJBContainer();
container.getContext().bind("inject", this);
}
}
package fi.codecrew.moya.beans;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.Test;
@Test
public class VipBeanTest {
private static final Logger log = LoggerFactory.getLogger(VipBeanTest.class);
@Test
public void testVip() {
log.info("testVip");
VipBean vipBean = new VipBean();
}
}
......@@ -19,6 +19,7 @@
package fi.codecrew.moya.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
......@@ -128,6 +129,9 @@ public class LanEvent extends GenericEntity {
@OneToMany(mappedBy = "event", cascade = CascadeType.ALL)
private List<LanEventProperty> properties = new ArrayList<LanEventProperty>();
@OneToMany(mappedBy = "event")
private List<Vip> vips;
public LanEvent() {
}
......@@ -333,4 +337,11 @@ public class LanEvent extends GenericEntity {
this.theme = theme;
}
public List<Vip> getVips() {
return vips;
}
public void setVips(List<Vip> vips) {
this.vips = vips;
}
}
package fi.codecrew.moya.model;
import javax.persistence.*;
import java.util.Calendar;
/**
* VIP list. Info staff will use the VIP list information to allow certain
* special treatment of people :)
*
* The VIP list entry can be just a free form description containing the names
* of VIPs and what makes them VIP, or the entry may refer to an EventUser.
*
* Every VIP entry must have a responsible person in the organization.
* Creator must also be recorder.
*
*/
@Entity
@Table(name="vips")
public class Vip extends GenericEntity {
private static final long serialVersionUID = 1L;
private static final String EVENT_ID_COLUMN = "event_id";
private static final String VIP_EVENT_USER_ID_COLUMN = "vip_event_user_id";
private static final String CREATOR_EVENT_USER_ID_COLUMN = "creator_event_user_id";
private static final String HOST_EVENT_USER_ID_COLUMN = "host_event_user_id";
@Column(name = "description", nullable = false)
@Lob
private String description;
@Column(name = "created", nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Calendar created = Calendar.getInstance();
@ManyToOne
@JoinColumn(name = EVENT_ID_COLUMN, referencedColumnName = LanEvent.ID_COLUMN, nullable = false)
private LanEvent event;
@ManyToOne
@JoinColumn(name = VIP_EVENT_USER_ID_COLUMN, referencedColumnName = EventUser.ID_COLUMN, nullable = true)
private EventUser eventUser;
/**
* Mandatory creator of this VIP list entry.
*/
@ManyToOne
@JoinColumn(name = CREATOR_EVENT_USER_ID_COLUMN, referencedColumnName = EventUser.ID_COLUMN, nullable = false)
private EventUser creator;
/**
* Mandatory responsible person of this VIP list entry.
*/
@ManyToOne
@JoinColumn(name = HOST_EVENT_USER_ID_COLUMN, referencedColumnName = EventUser.ID_COLUMN, nullable = false)
private EventUser host;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Calendar getCreated() {
return created;
}
public void setCreated(Calendar created) {
this.created = created;
}
public LanEvent getEvent() {
return event;
}
public void setEvent(LanEvent event) {
this.event = event;
}
public EventUser getEventUser() {
return eventUser;
}
public void setEventUser(EventUser eventUser) {
this.eventUser = eventUser;
}
public EventUser getCreator() {
return creator;
}
public void setCreator(EventUser creator) {
this.creator = creator;
}
public EventUser getHost() {
return host;
}
public void setHost(EventUser host) {
this.host = host;
}
}
......@@ -18,21 +18,7 @@
*/
package fi.codecrew.moya.enums;
import fi.codecrew.moya.enums.apps.BillPermission;
import fi.codecrew.moya.enums.apps.CompoPermission;
import fi.codecrew.moya.enums.apps.ContentPermission;
import fi.codecrew.moya.enums.apps.EventPermission;
import fi.codecrew.moya.enums.apps.IAppPermission;
import fi.codecrew.moya.enums.apps.LecturePermission;
import fi.codecrew.moya.enums.apps.LicensePermission;
import fi.codecrew.moya.enums.apps.MapPermission;
import fi.codecrew.moya.enums.apps.NetworkAssociationPermission;
import fi.codecrew.moya.enums.apps.PollPermission;
import fi.codecrew.moya.enums.apps.SalespointPermission;
import fi.codecrew.moya.enums.apps.ShopPermission;
import fi.codecrew.moya.enums.apps.TerminalPermission;
import fi.codecrew.moya.enums.apps.TournamentPermission;
import fi.codecrew.moya.enums.apps.UserPermission;
import fi.codecrew.moya.enums.apps.*;
public enum BortalApplication {
USER(UserPermission.class),
......@@ -48,7 +34,8 @@ public enum BortalApplication {
EVENT(EventPermission.class),
LICENSE(LicensePermission.class),
TOURNAMENT(TournamentPermission.class),
NETWORKASSOCIATION(NetworkAssociationPermission.class)
NETWORKASSOCIATION(NetworkAssociationPermission.class),
VIP(VipPermission.class)
;
private final String key;
......
package fi.codecrew.moya.enums.apps;
import fi.codecrew.moya.enums.BortalApplication;
/**
* Created by jkj on 2015-01-11.
*/
public enum VipPermission implements IAppPermission {
VIEW,
USAGE,
EDIT;
public static final String S_VIEW = "VIP/VIEW";
public static final String S_USAGE = "VIP/USAGE";
public static final String S_EDIT = "VIP/EDIT";
private final String fullName;
private final String key;
public static final String I18N_HEADER = "bortalApplication.vip.";
private VipPermission() {
fullName = new StringBuilder().append(getParent().toString()).append(DELIMITER).append(toString()).toString();
key = I18N_HEADER + name();
}
@Override
public BortalApplication getParent() {
return BortalApplication.VIP;
}
@Override
public String getFullName() {
return fullName;
}
@Override
public String getI18nKey() {
return key;
}
}
package fi.codecrew.moya.sql;
import java.util.ArrayList;
import java.util.List;
/**
* CREATE TABLE tableName
*/
public class CreateTableDDL {
private final String tableName;
private final boolean ifNotExists;
private final List<Field> tableFields;
public CreateTableDDL(String tableName) {
this.tableName = tableName;
this.ifNotExists = false;
this.tableFields = new ArrayList<Field>();
}
public CreateTableDDL(String tableName, boolean iFNotExists) {
this.tableName = tableName;
this.ifNotExists = iFNotExists;
this.tableFields = new ArrayList<Field>();
}
CreateTableDDL(String tableName, boolean ifNotExists, List<Field> fields) {
this.tableName = tableName;
this.ifNotExists = ifNotExists;
this.tableFields = new ArrayList<Field>(fields);
}
private CreateTableDDL withNewField(Field field) {
ArrayList<Field> newFieldSet = new ArrayList<Field>(tableFields.size() + 1);
newFieldSet.addAll(tableFields);
newFieldSet.add(field);
return new CreateTableDDL(tableName, ifNotExists, newFieldSet);
}
private String fieldsString() {
String[] strs = new String[tableFields.size()];
for (int i = 0; i<strs.length; i++) {
strs[i] = tableFields.get(i).toString();
}
return String.join(", ", strs);
}
@Override
public String toString() {
return String.join(" ", "CREATE TABLE", tableName, "(" + fieldsString() + ");");
}
public CreateTableDDL serialPK(String name) {
return withNewField(new Field(name, "serial", false, "PRIMARY KEY"));
}
public CreateTableDDL integerField(String name, boolean nullable, String... params) {
return withNewField(new Field(name, "integer", nullable, params));
}
public CreateTableDDL reference(String name, boolean nullable, String targetTable) {
return withNewField(new Field(name, "integer", nullable, "REFERENCES", targetTable));
}
public CreateTableDDL reference(String name, boolean nullable, String targetTable, String targetColumn) {
return withNewField(new Field(name, "integer", nullable, "REFERENCES", targetTable, "(" + targetColumn + ")"));
}
public CreateTableDDL text(String name, boolean nullable, String... params) {
return withNewField(new Field(name, "text", nullable, params));
}
public CreateTableDDL timestampTZ(String name, boolean nullable, String... params) {
return withNewField(new Field(name, "timestamptz", nullable, params));
}
public CreateTableDDL field(String name, String type, boolean nullable, String... params) {
return withNewField(new Field(name, type, nullable, params));
}
}
package fi.codecrew.moya.sql;
/**
* Make PostgreSQL DDL for table creation, etc.
*/
public class DDLBuilder {
public CreateTableDDL createTable(String tableName) {
return new CreateTableDDL(tableName);
}
public CreateTableDDL createTable(String tableName, boolean iFNotExists) {
return new CreateTableDDL(tableName, iFNotExists);
}
/**
* @param tableName
* @param params extra params such as CASCADE or RESTRICT
* @return
*/
public DropTableDDL dropTable(String tableName, String... params) {
return new DropTableDDL(tableName, params);
}
/**
* @param tableName
* @param ifExists use IF EXISTS flag to ignore error when table is not found
* @param params extra params such as CASCADE or RESTRICT
* @return
*/
public DropTableDDL dropTable(String tableName, boolean ifExists, String... params) {
return new DropTableDDL(tableName, ifExists, params);
}
}
package fi.codecrew.moya.sql;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DropTableDDL {
private final String tableName;
private final String[] params;
private final boolean ifExists;
public DropTableDDL(String tableName, String... params) {
this.tableName = tableName;
this.ifExists = false;
this.params = params;
}
public DropTableDDL(String tableName, boolean ifExists, String... params) {
this.tableName = tableName;
this.ifExists = ifExists;
this.params = params;
}
@Override
public String toString() {
List<String> words = new ArrayList<String>();
words.add("DROP TABLE");
// IF EXISTS flag
if (ifExists) {
words.add("IF EXISTS");
}
// Extra params such as CASCADE or RESTRICT
if (params.length>0) {
words.addAll(Arrays.asList(params));
}
return String.join(" ", "DROP TABLE", tableName);
}
}
package fi.codecrew.moya.sql;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
class Field {
private String fieldName;
private String fieldType;
private boolean nullable;
private final String[] params;
Field(String fieldName, String fieldType, boolean nullable) {
this.fieldName = fieldName;
this.fieldType = fieldType;
this.nullable = nullable;
this.params = new String[]{};
}
Field(String fieldName, String fieldType, boolean nullable, String... params) {
this.fieldName = fieldName;
this.fieldType = fieldType;
this.nullable = nullable;
this.params = params;
}
@Override
public String toString() {
List<String> words = new ArrayList<String>();
words.add(fieldName);
words.add(fieldType);
if (nullable == false) {
words.add("NOT NULL");
}
if (params.length>0) {
words.addAll(Arrays.asList(params));
}
return String.join(" ", words);
}
}
package fi.codecrew.moya.sql;
import org.testng.Assert;
import org.testng.annotations.Test;
@Test
public class CreateTableTest {
@Test
void testCreateSimpleTable() {
String expectedSql = "CREATE TABLE taulu (id serial NOT NULL PRIMARY KEY, numero integer);";
String actualSql = new DDLBuilder()
.createTable("taulu")
.serialPK("id")
.integerField("numero", true)
.toString();
Assert.assertEquals(actualSql, expectedSql);
}
@Test
void testCreateVipTable() {
String actual = new DDLBuilder().createTable("vips")
.serialPK("id")
.text("description", false)
.timestampTZ("created", false, "default", "now()")
.reference("event_id", false, "events")
.reference("vip_event_user_id", true, "event_users")
.reference("creator_event_user_id", false, "event_users")
.reference("host_event_user_id", false, "event_users")
.field("meta", "json", true)
.toString();
String expected = "CREATE TABLE vips (id serial NOT NULL PRIMARY KEY, description text NOT NULL, created timestamptz NOT NULL default now(), event_id integer NOT NULL REFERENCES events, vip_event_user_id integer REFERENCES event_users, creator_event_user_id integer NOT NULL REFERENCES event_users, host_event_user_id integer NOT NULL REFERENCES event_users, meta json);";
Assert.assertEquals(actual, expected);
}
}
<?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:ui="http://java.sun.com/jsf/facelets">
<h:body>
<ui:composition template="#{sessionHandler.template}">
<f:metadata>
<f:event type="preRenderView" listener="#{vipListView.initView}"/>
</f:metadata>
<ui:define name="content">
<h:dataTable id="reader" value="#{vipListView.vips}" var="vip">
<h:column>
<f:facet name="header">
<h:outputText value="${i18n['vip.description']}"/>
</f:facet>
<h:outputText value="#{vip.description}"/>
</h:column>
<h:column>
<h:link outcome="/vip/vipuse" value="#{i18n['vip.use']}">
<f:param value="#{vip.id}" name="vipId"/>
</h:link>
</h:column>
<h:column>
<h:link outcome="/vip/vipedit" value="#{i18n['vip.edit']}">
<f:param value="#{vip.id}" name="vipId"/>
</h:link>
</h:column>
</h:dataTable>
</ui:define>
</ui:composition>
</h:body>
</html>
\ No newline at end of file
......@@ -70,6 +70,16 @@ public abstract class GenericCDIView implements Serializable {
return permbean.hasPermission(perm);
}
/**
* Check that we have the permission in the first argument and also require
* that all of the booleans in varargs are true. If we don't have the perm
* or one of the booleans is false, we redirect to permission denied page and
* return false.
*
* @param perm IAppPermission to be checked
* @param externalChecks
* @return true if we had the permission and all of the externalChecks were true.
*/
protected boolean requirePermissions(IAppPermission perm, boolean... externalChecks) {
boolean ret = requirePermissions(hasPermission(perm));
......@@ -84,6 +94,13 @@ public abstract class GenericCDIView implements Serializable {
return ret;
}
/**
* Check that all of the given booleans are true. If any of them is false,
* redirect to permission denied page and return false.
* @param externalChecks bunch of booleans
* @return were all externalChecks true? false also means that we're redirecting
* to parmission denied page.
*/
protected boolean requirePermissions(boolean... externalChecks) {
boolean ret = true;
......
package fi.codecrew.moya.web.cdiview.vip;
import fi.codecrew.moya.beans.VipBeanLocal;
import fi.codecrew.moya.enums.apps.VipPermission;
import fi.codecrew.moya.model.Vip;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
import javax.ejb.EJB;
import javax.faces.model.ListDataModel;
/**
* Created by jkj on 2015-01-11.
*/
public class VipListView extends GenericCDIView {
@EJB
VipBeanLocal vipBean;
protected ListDataModel<Vip> vipListDataModel;
public void initView() {
if (super.requirePermissions(VipPermission.VIEW)) {
vipListDataModel = new ListDataModel<Vip>(vipBean.getAvailableVips());
super.beginConversation();
}
}
public ListDataModel<Vip> getVips() {
return vipListDataModel;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!