Skip to content

Commit 7f822d7

Browse files
committed
commands in robot's chat
1 parent f7c73ef commit 7f822d7

File tree

3 files changed

+124
-19
lines changed

3 files changed

+124
-19
lines changed

logicaldoc-core/src/main/java/com/logicaldoc/core/automation/DocTool.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.logicaldoc.core.parser.ParsingException;
3434
import com.logicaldoc.core.searchengine.Hit;
3535
import com.logicaldoc.core.searchengine.SearchEngine;
36+
import com.logicaldoc.core.security.Permission;
3637
import com.logicaldoc.core.security.Tenant;
3738
import com.logicaldoc.core.security.TenantDAO;
3839
import com.logicaldoc.core.security.authorization.PermissionException;
@@ -1011,4 +1012,42 @@ public String getText(Document document) {
10111012
text = parse(document, null);
10121013
return text;
10131014
}
1015+
1016+
/**
1017+
* This method checks if the given permission is enabled for a document and
1018+
* an user.
1019+
*
1020+
* @param permission the permission to check
1021+
* @param documentId ID of the folder
1022+
* @param userId ID of the user
1023+
*
1024+
* @return if the permission is granted to the user
1025+
*
1026+
* @throws PersistenceException error at data layer
1027+
*/
1028+
public boolean isPermissionAllowed(String permission, long documentId, long userId) {
1029+
try {
1030+
return Context.get(DocumentDAO.class).isPermissionAllowed(Permission.valueOf(permission), documentId,
1031+
userId);
1032+
} catch (PersistenceException e) {
1033+
log.error(e.getMessage(), e);
1034+
return false;
1035+
}
1036+
}
1037+
1038+
public boolean isWriteAllowed(long documentId, long userId) {
1039+
return isPermissionAllowed(Permission.WRITE.name(), documentId, userId);
1040+
}
1041+
1042+
public boolean isReadAllowed(long documentId, long userId) {
1043+
return isPermissionAllowed(Permission.READ.name(), documentId, userId);
1044+
}
1045+
1046+
public boolean isPreviewAllowed(long documentId, long userId) {
1047+
return isPermissionAllowed(Permission.PREVIEW.name(), documentId, userId);
1048+
}
1049+
1050+
public boolean isDownloadAllowed(long documentId, long userId) {
1051+
return isPermissionAllowed(Permission.DOWNLOAD.name(), documentId, userId);
1052+
}
10141053
}

logicaldoc-core/src/main/java/com/logicaldoc/core/document/HibernateDocumentDAO.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class HibernateDocumentDAO extends HibernatePersistentObjectDAO<Document>
9090

9191
@Resource(name = "GroupDAO")
9292
private GroupDAO groupDAO;
93-
93+
9494
@Resource(name = "DocumentLinkDAO")
9595
private DocumentLinkDAO linkDAO;
9696

@@ -357,23 +357,23 @@ public void store(Document doc, final DocumentHistory transaction) throws Persis
357357

358358
private void removeForbiddenPermissionsForGuests(Document document) throws PersistenceException {
359359
// Remove the forbidden permissions for the guests
360-
for (DocumentAccessControlEntry ace : document.getAccessControlList()) {
361-
Group group = groupDAO.findById(ace.getGroupId());
362-
if (group != null && group.isGuest()) {
363-
ace.setArchive(0);
364-
ace.setAutomation(0);
365-
ace.setCalendar(0);
366-
ace.setDelete(0);
367-
ace.setImmutable(0);
368-
ace.setMove(0);
369-
ace.setPassword(0);
370-
ace.setRename(0);
371-
ace.setSecurity(0);
372-
ace.setSign(0);
373-
ace.setWorkflow(0);
374-
ace.setWrite(0);
375-
}
376-
}
360+
for (DocumentAccessControlEntry ace : document.getAccessControlList()) {
361+
Group group = groupDAO.findById(ace.getGroupId());
362+
if (group != null && group.isGuest()) {
363+
ace.setArchive(0);
364+
ace.setAutomation(0);
365+
ace.setCalendar(0);
366+
ace.setDelete(0);
367+
ace.setImmutable(0);
368+
ace.setMove(0);
369+
ace.setPassword(0);
370+
ace.setRename(0);
371+
ace.setSecurity(0);
372+
ace.setSign(0);
373+
ace.setWorkflow(0);
374+
ace.setWrite(0);
375+
}
376+
}
377377
}
378378

379379
private void checkMaxDocsPerFolder(Document document) throws PersistenceException {
@@ -1421,6 +1421,9 @@ public Set<Permission> getAllowedPermissions(long docId, long userId) throws Per
14211421
User user = getExistingtUser(userId);
14221422
userDAO.initialize(user);
14231423

1424+
if (findById(docId) == null)
1425+
throw new PersistenceException("Unexisting document " + docId);
1426+
14241427
// If the user is an administrator bypass all controls
14251428
if (user.isAdmin()) {
14261429
return Permission.all();

logicaldoc-gui/src/main/java/com/logicaldoc/gui/frontend/client/ai/robot/MessageBox.java

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.logicaldoc.gui.frontend.client.ai.robot;
22

3+
import com.google.gwt.core.client.UnsafeNativeLong;
4+
import com.google.gwt.regexp.shared.MatchResult;
5+
import com.google.gwt.regexp.shared.RegExp;
36
import com.logicaldoc.gui.common.client.Session;
47
import com.logicaldoc.gui.common.client.util.AwesomeFactory;
58
import com.logicaldoc.gui.common.client.util.Util;
9+
import com.logicaldoc.gui.frontend.client.document.DocumentsPanel;
610
import com.smartgwt.client.types.Alignment;
711
import com.smartgwt.client.widgets.Label;
812

@@ -14,6 +18,10 @@
1418
*/
1519
class MessageBox extends Label {
1620

21+
private static final String CMD_END = "]cmd]";
22+
23+
private static final String CMD_START = "[cmd[";
24+
1725
private static final String ROBOT = "robot";
1826

1927
private String avatarIcon;
@@ -44,14 +52,62 @@ private String prepareContent(String message) {
4452
String copyButton = "<button onclick=\"copy(document.getElementById('" + messageTextElementId
4553
+ "').innerText);\" style='width:32px'>" + AwesomeFactory.getIconHtml("paste") + "</button>";
4654

55+
if (isRobotAnswer())
56+
message = parseRobotAnswer(message);
57+
4758
return "<table border='0' width='100%'><tr><td style='width:36px; align:center; vertical-align: top;'>"
48-
+ avatarIcon + (ROBOT.equals(role) ? copyButton : "") + "</td><td><span id='" + messageTextElementId
59+
+ avatarIcon + (isRobotAnswer() ? copyButton : "") + "</td><td><span id='" + messageTextElementId
4960
+ "'>" + message + "</span></td></tr></table>";
5061
} else {
5162
return "";
5263
}
5364
}
5465

66+
@Override
67+
protected void onInit() {
68+
super.onInit();
69+
declareCommandOpenDocument(DocumentsPanel.get());
70+
}
71+
72+
protected boolean isRobotAnswer() {
73+
return ROBOT.equals(role);
74+
}
75+
76+
/**
77+
* Processes the answer looking for command tokens like
78+
* [cmd[<b>command</b>|<b>arg1</b>|<b>arg2</b>]cmd] and replace them with
79+
* proper rendering.
80+
*
81+
* @param message The original message from the robot
82+
*
83+
* @return The elaborated messages with command tokens expanded
84+
*/
85+
private String parseRobotAnswer(String message) {
86+
RegExp p = RegExp.compile("\\[cmd\\[[a-zA-Z]*\\|.*\\|.*\\]cmd\\]");
87+
MatchResult result = p.exec(message);
88+
89+
if (result != null) {
90+
for (int i = 0; i < result.getGroupCount(); i++) {
91+
String match = result.getGroup(i);
92+
match = match.replace(CMD_START, "");
93+
match = match.replace(CMD_END, "");
94+
String link = processCommand(match.split("\\|"));
95+
message = message.replace(CMD_START + match + CMD_END, link);
96+
}
97+
}
98+
99+
return message;
100+
}
101+
102+
private String processCommand(String[] args) {
103+
String command = args[0];
104+
String link = "<a href='javascript:cmd" + command + "(";
105+
if ("OpenDocument".equals(command)) {
106+
link += args[1] + ");'>" + args[2] + "</a>";
107+
}
108+
return link;
109+
}
110+
55111
public void updateMessage(String message) {
56112
setContents(prepareContent(message));
57113
}
@@ -68,4 +124,11 @@ public boolean equals(Object obj) {
68124
public int hashCode() {
69125
return super.hashCode();
70126
}
127+
128+
@UnsafeNativeLong
129+
public static native void declareCommandOpenDocument(DocumentsPanel panel) /*-{
130+
$wnd.cmdOpenDocument = function(docId) {
131+
return panel.@com.logicaldoc.gui.frontend.client.document.DocumentsPanel::openInFolder(J)(docId);
132+
};
133+
}-*/;
71134
}

0 commit comments

Comments
 (0)