Skip to content

Commit

Permalink
Minore refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparez15 committed Aug 27, 2024
1 parent 2c29a40 commit f7aeed5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.logicaldoc.core.security.Session;
import com.logicaldoc.core.security.SessionManager;
import com.logicaldoc.gui.common.client.InvalidSessionServerException;
Expand Down Expand Up @@ -52,7 +52,7 @@ public class OnlyOfficeEditor extends HttpServlet {

private SettingsManagerImpl settingsManager;

private ObjectMapper om = new ObjectMapper();
private ObjectMapper objectMapper = new ObjectMapper();

private OODocumentManagerImpl dm;

Expand Down Expand Up @@ -168,52 +168,27 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
} catch (Exception e) { e.printStackTrace();
}

request.setAttribute("config", om.writeValueAsString(config));
request.setAttribute("config", objectMapper.writeValueAsString(config));

// an image that will be inserted into the document
Map<String, Object> dataInsertImage = new HashMap<>();
dataInsertImage.put("fileType", "png");
dataInsertImage.put("url", OODocumentManager.getServerUrl(true) + "/css/img/logo.png");
if (isEnableDirectUrl) {
dataInsertImage.put("directUrl", OODocumentManager.getServerUrl(false) + "/css/img/logo.png");
}

// a document that will be compared with the current document
Map<String, Object> dataDocument = new HashMap<>();
dataDocument.put("fileType", "docx");
dataDocument.put("url", OODocumentManager.getServerUrl(true) + "/onlyoffice/IndexServlet?type=assets&"
+ "name=sample.docx");
if (isEnableDirectUrl) {
dataDocument.put("directUrl", OODocumentManager.getServerUrl(false) + "/onlyoffice/IndexServlet?"
+ "type=assets&name=sample.docx");
}

// recipients data for mail merging
Map<String, Object> dataSpreadsheet = new HashMap<>();
dataSpreadsheet.put("fileType", "csv");
dataSpreadsheet.put("url", OODocumentManager.getServerUrl(true) + "/onlyoffice/IndexServlet?"
+ "type=csv");
if (isEnableDirectUrl) {
dataSpreadsheet.put("directUrl", OODocumentManager.getServerUrl(false)
+ "/onlyoffice/IndexServlet?type=csv");
}

// users data for mentions
List<Map<String, Object>> usersForMentions = Users.getUsersForMentions(user.getId());
List<Map<String, Object>> usersForProtect = Users.getUsersForProtect(user.getId());
List<Map<String, Object>> usersInfo = Users.getUsersInfo(user.getId());

Gson gson = new Gson();
request.setAttribute("docserviceApiUrl", ConfigManager.getProperty("files.docservice.url.site")
+ ConfigManager.getProperty("files.docservice.url.api"));

request.setAttribute("dataInsertImage", gson.toJson(dataInsertImage).substring(1, gson.toJson(dataInsertImage).length() - 1));
request.setAttribute("dataDocument", gson.toJson(dataDocument));
request.setAttribute("dataSpreadsheet", gson.toJson(dataSpreadsheet));
// get an image and add it to the model
request.setAttribute("dataInsertImage", getInsertImage(isEnableDirectUrl));

// get a document for comparison and add it to the model
request.setAttribute("dataDocument", getCompareFile(isEnableDirectUrl));

request.setAttribute("usersForMentions", !user.getId().equals("uid-0") ? gson.toJson(usersForMentions) : null);
request.setAttribute("usersInfo", gson.toJson(usersInfo));
request.setAttribute("usersForProtect", !user.getId().equals("uid-0") ? gson.toJson(usersForProtect) : null);
// get recipients data for mail merging and add it to the model
request.setAttribute("dataSpreadsheet", getSpreadsheet(isEnableDirectUrl));

// get user data for mentions and add it to the model
request.setAttribute("usersForMentions", getUserMentions(user.getId()));

request.setAttribute("usersInfo", getUsersInfo(user.getId()));

// get user data for protect and add it to the model
request.setAttribute("usersForProtect", getUserProtect(user.getId()));

request.getRequestDispatcher("/onlyoffice/editor.jsp").forward(request, response);

Expand All @@ -222,6 +197,62 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
System.err.println(e);
handleError(response, e);
}
}

private String getUserMentions(final String uid) throws JsonProcessingException {
List<Map<String, Object>> usersForMentions = Users.getUsersForMentions(uid);
return !uid.equals("uid-0") ? objectMapper.writeValueAsString(usersForMentions) : null;
}

private String getUsersInfo(final String uid) throws JsonProcessingException {
List<Map<String, Object>> usersInfo = Users.getUsersInfo(uid);
return objectMapper.writeValueAsString(usersInfo);
}

private String getUserProtect(final String uid) throws JsonProcessingException {
List<Map<String, Object>> usersForProtect = Users.getUsersForProtect(uid);
return !uid.equals("uid-0") ? objectMapper.writeValueAsString(usersForProtect) : null;
}

// get recipients data for mail merging and add it to the model
private String getSpreadsheet(Boolean isEnableDirectUrl) throws JsonProcessingException {
Map<String, Object> dataSpreadsheet = new HashMap<>();
dataSpreadsheet.put("fileType", "csv");
dataSpreadsheet.put("url", OODocumentManager.getServerUrl(true) + "/onlyoffice/IndexServlet?"
+ "type=csv");
if (isEnableDirectUrl) {
dataSpreadsheet.put("directUrl", OODocumentManager.getServerUrl(false)
+ "/onlyoffice/IndexServlet?type=csv");
}
return objectMapper.writeValueAsString(dataSpreadsheet);
}

// get a document that will be compared with the current document
private String getCompareFile(Boolean isEnableDirectUrl) throws JsonProcessingException {

Map<String, Object> dataDocument = new HashMap<>();
dataDocument.put("fileType", "docx");
dataDocument.put("url", OODocumentManager.getServerUrl(true) + "/onlyoffice/IndexServlet?type=assets&"
+ "name=sample.docx");
if (isEnableDirectUrl) {
dataDocument.put("directUrl", OODocumentManager.getServerUrl(false) + "/onlyoffice/IndexServlet?"
+ "type=assets&name=sample.docx");
}

return objectMapper.writeValueAsString(dataDocument);
}

// get an image that will be inserted into the document
private String getInsertImage(Boolean isEnableDirectUrl) throws JsonProcessingException {
Map<String, Object> dataInsertImage = new HashMap<>();
dataInsertImage.put("fileType", "png");
dataInsertImage.put("url", OODocumentManager.getServerUrl(true) + "/onlyoffice/css/img/logo.png");
if (isEnableDirectUrl) {
dataInsertImage.put("directUrl", OODocumentManager.getServerUrl(false) + "/onlyoffice/css/img/logo.png");
}

return objectMapper.writeValueAsString(dataInsertImage)
.substring(1, objectMapper.writeValueAsString(dataInsertImage).length() - 1);
}

private void handleError(HttpServletResponse response, Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ public static String getCorrectName(final String fileName, final String userAddr
}

// create meta information
public static void createMeta(final String fileName, final String uid, final String uname,
@SuppressWarnings("unchecked")
public static void createMeta(final String fileName, final String uid, final String uname,
final String userAddress) throws Exception {
String histDir = historyDir(storagePath(fileName, userAddress));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,9 @@ private static byte[] getDownloadFile(final String url) throws Exception {

return getAllBytes(stream);
}



// create a command request
public static void commandRequest(final String method, final String key, final HashMap meta) throws Exception {
public static void commandRequest(final String method, final String key, final HashMap<String, String> meta) throws Exception {

System.out.println("TrackManager commandRequest()");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static List<Map<String, Object>> getUsersInfo(final String id) {
data.put("name", user.getName());
data.put("email", user.getEmail());
data.put("image", user.getAvatar() ? OODocumentManager.getServerUrl(false)
+ "/css/img/" + user.getId() + ".png" : null);
+ "/onlyoffice/css/img/" + user.getId() + ".png" : null);
usersData.add(data);
}
}
Expand Down

0 comments on commit f7aeed5

Please sign in to comment.