Commit fd914ee5 by Antti Tönkyrä

Initial support for ActionLog tags

1 parent a925af00
package fi.codecrew.moya.beans; package fi.codecrew.moya.beans;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -9,6 +10,7 @@ import javax.ejb.EJB; ...@@ -9,6 +10,7 @@ import javax.ejb.EJB;
import javax.ejb.Stateless; import javax.ejb.Stateless;
import fi.codecrew.moya.facade.ActionLogFacade; import fi.codecrew.moya.facade.ActionLogFacade;
import fi.codecrew.moya.facade.ActionLogMessageTagFacade;
import fi.codecrew.moya.beans.ActionLogBeanLocal; import fi.codecrew.moya.beans.ActionLogBeanLocal;
import fi.codecrew.moya.beans.PermissionBeanLocal; import fi.codecrew.moya.beans.PermissionBeanLocal;
import fi.codecrew.moya.beans.RoleBeanLocal; import fi.codecrew.moya.beans.RoleBeanLocal;
...@@ -16,6 +18,7 @@ import fi.codecrew.moya.enums.ActionLogMessageState; ...@@ -16,6 +18,7 @@ import fi.codecrew.moya.enums.ActionLogMessageState;
import fi.codecrew.moya.enums.apps.ContentPermission; import fi.codecrew.moya.enums.apps.ContentPermission;
import fi.codecrew.moya.model.ActionLogMessage; import fi.codecrew.moya.model.ActionLogMessage;
import fi.codecrew.moya.model.ActionLogMessageResponse; import fi.codecrew.moya.model.ActionLogMessageResponse;
import fi.codecrew.moya.model.ActionLogMessageTag;
import fi.codecrew.moya.model.Role; import fi.codecrew.moya.model.Role;
/** /**
...@@ -36,12 +39,20 @@ public class ActionLogBean implements ActionLogBeanLocal { ...@@ -36,12 +39,20 @@ public class ActionLogBean implements ActionLogBeanLocal {
@EJB @EJB
private PermissionBeanLocal permissionBean; private PermissionBeanLocal permissionBean;
@EJB
private ActionLogMessageTagFacade actionLogMessageTagFacade;
@EJB
private EventBean eventBean;
public ActionLogBean() { public ActionLogBean() {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
} }
@RolesAllowed(ContentPermission.S_MANAGE_ACTIONLOG) @RolesAllowed(ContentPermission.S_MANAGE_ACTIONLOG)
public void createActionLogEvent(String message, Role crew, boolean isTask) { public void createActionLogEvent(String message, Role crew, boolean isTask) {
ArrayList<ActionLogMessageTag> almts = resolveTags(message);
ActionLogMessage alm = new ActionLogMessage(); ActionLogMessage alm = new ActionLogMessage();
alm.setCrew(crew); alm.setCrew(crew);
if (isTask) { if (isTask) {
...@@ -98,4 +109,62 @@ public class ActionLogBean implements ActionLogBeanLocal { ...@@ -98,4 +109,62 @@ public class ActionLogBean implements ActionLogBeanLocal {
if(!alm.getLanEvent().equals(permissionBean.getCurrentUser().getEvent())) return null; if(!alm.getLanEvent().equals(permissionBean.getCurrentUser().getEvent())) return null;
else return alm; else return alm;
} }
private ArrayList<ActionLogMessageTag> resolveTags(String message) {
ArrayList<ActionLogMessageTag> almts = new ArrayList<>();
StringBuilder sb = null;
boolean rflag = false;
char ch;
for(int i=0; i < message.length(); i++) {
if(rflag) {
if((ch = message.charAt(i)) != ' ') {
sb.append(ch);
} else {
if(sb.length() > 0) {
ActionLogMessageTag almt = getActionLogMessageTagByString(sb.toString());
if(!almts.contains(almt)) {
almts.add(almt);
}
}
rflag = false;
sb = null;
}
} else if(!rflag) {
if(message.charAt(i) == '#') {
rflag=true;
sb=new StringBuilder();
}
}
}
if(sb != null && sb.length() > 0) {
ActionLogMessageTag almt = getActionLogMessageTagByString(sb.toString());
if(!almts.contains(almt)) {
almts.add(almt);
}
}
return almts;
}
private ActionLogMessageTag getActionLogMessageTagByString(String s) {
s = s.toLowerCase();
ActionLogMessageTag almt = null;
if((almt = actionLogMessageTagFacade.findByTagStringInEvent(s, eventBean.getCurrentEvent())) == null) {
almt = new ActionLogMessageTag();
almt.setEvent(eventBean.getCurrentEvent());
almt.setTag(s);
almt = actionLogMessageTagFacade.create(almt);
System.out.println("creating tag: "+s);
return almt;
} else {
System.out.println("re-using tag: "+s);
return almt;
}
}
} }
\ No newline at end of file
package fi.codecrew.moya.facade;
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;
import fi.codecrew.moya.model.ActionLogMessageTag;
import fi.codecrew.moya.model.ActionLogMessageTag_;
import fi.codecrew.moya.model.LanEvent;
@Stateless
@LocalBean
public class ActionLogMessageTagFacade extends IntegerPkGenericFacade<ActionLogMessageTag> {
public ActionLogMessageTagFacade() {
super(ActionLogMessageTag.class);
}
public ActionLogMessageTag findByTagStringInEvent(String tagString, LanEvent event) {
CriteriaBuilder cb = getEm().getCriteriaBuilder();
CriteriaQuery<ActionLogMessageTag> cq = cb.createQuery(ActionLogMessageTag.class);
Root<ActionLogMessageTag> root = cq.from(ActionLogMessageTag.class);
cq.where(
cb.and(
cb.equal(root.get(ActionLogMessageTag_.event), event),
cb.equal(root.get(ActionLogMessageTag_.tag), tagString)
)
);
TypedQuery<ActionLogMessageTag> tq = getEm().createQuery(cq);
ActionLogMessageTag almt;
try {
almt = tq.getSingleResult();
} catch(Exception e) {
return null;
}
return almt;
}
}
...@@ -10,6 +10,8 @@ import javax.persistence.Entity; ...@@ -10,6 +10,8 @@ import javax.persistence.Entity;
import javax.persistence.EnumType; import javax.persistence.EnumType;
import javax.persistence.Enumerated; import javax.persistence.Enumerated;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.OrderBy; import javax.persistence.OrderBy;
import javax.persistence.Table; import javax.persistence.Table;
...@@ -49,6 +51,18 @@ public class ActionLogMessage extends GenericEntity { ...@@ -49,6 +51,18 @@ public class ActionLogMessage extends GenericEntity {
@OrderBy("id") @OrderBy("id")
private List<ActionLogMessageResponse> actionLogMessageResponses = new ArrayList<ActionLogMessageResponse>(); private List<ActionLogMessageResponse> actionLogMessageResponses = new ArrayList<ActionLogMessageResponse>();
@ManyToMany()
@JoinTable(
name = "actionlog_message_tag_sets",
joinColumns = {
@JoinColumn(name = "actionlog_message_id", referencedColumnName = ActionLogMessage.ID_COLUMN)
},
inverseJoinColumns = {
@JoinColumn(name = "actionlog_message_tag_id", referencedColumnName = ActionLogMessageTag.ID_COLUMN)
}
)
private List<ActionLogMessageTag> parents = new ArrayList<ActionLogMessageTag>();
@Column(name = "state", nullable = true) @Column(name = "state", nullable = true)
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
private ActionLogMessageState state; private ActionLogMessageState state;
......
package fi.codecrew.moya.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import org.eclipse.persistence.annotations.OptimisticLocking;
import org.eclipse.persistence.annotations.OptimisticLockingType;
import org.eclipse.persistence.annotations.PrivateOwned;
@Entity
@Table(name = "actionlog_message_tags")
@OptimisticLocking(type = OptimisticLockingType.CHANGED_COLUMNS)
public class ActionLogMessageTag extends GenericEntity {
private static final long serialVersionUID = -2902547412412000488L;
@Column(name = "tag", nullable = false)
private String tag;
@JoinColumn(name="event")
private LanEvent event;
public LanEvent getEvent() {
return event;
}
public void setEvent(LanEvent lanEvent) {
this.event = lanEvent;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getTag() {
return tag;
}
}
...@@ -56,6 +56,7 @@ public class ActionLogCreateView extends GenericCDIView { ...@@ -56,6 +56,7 @@ public class ActionLogCreateView extends GenericCDIView {
} }
public String send() { public String send() {
actionLogBean.createActionLogEvent(message, role, task); actionLogBean.createActionLogEvent(message, role, task);
return "success"; return "success";
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!