diff --git a/src/prerna/collaboration/BrainTopicBrief.java b/src/prerna/collaboration/BrainTopicBrief.java
index 5f815cefcd..cfaca617c0 100644
--- a/src/prerna/collaboration/BrainTopicBrief.java
+++ b/src/prerna/collaboration/BrainTopicBrief.java
@@ -179,6 +179,7 @@ private static String topic(User user, String ownerId, String ownerType, String
classLogger.warn("Could not count the threads of topic {} for the brief", topicId, e);
}
out.append(BrainTopicFiles.brief(ownerId, ownerType, topicId));
+ out.append(linkedFoldersBrief(ownerId, ownerType, topicId));
// the topic's work items, each with the owner's next step or who they wait on
List
*
*
@@ -880,9 +983,9 @@ public boolean alreadyTold() {
*
* @param accessToken Microsoft Graph access token for the user
* @param copy the local copy to upload
- * @param informed whether the owner asked for this save themselves, which
- * is what lets it write over a change they were already
- * told about; a save nobody asked for never does
+ * @param informed whether the owner asked for this save themselves, which is
+ * what lets it write over a change they were already told
+ * about; a save nobody asked for never does
* @return the file as OneDrive now holds it
* @throws SaveBackRefused if the file is not a recorded copy, or its original
* is gone or has changed
@@ -1238,8 +1341,8 @@ private static long downloadBytes(String accessToken, Map item,
*
*
* @param url what to fetch
- * @param headers the request headers, or null for a url that is pre-authenticated,
- * which a failure then does not quote
+ * @param headers the request headers, or null for a url that is
+ * pre-authenticated, which a failure then does not quote
* @param file where the bytes go, in a folder that exists
* @param name what a refusal calls the file
* @return how many bytes were written; the file is left as it was when there
@@ -1370,11 +1473,11 @@ private static String sessionUpload(String accessToken, String driveId, String i
/**
* Sends a file to an upload session in pieces, for whichever helper opened the
- * session. A session whose upload fails is cancelled: while one is open OneDrive
- * answers every other upload of the same file with a 409, "currently being
- * uploaded", for a quarter of an hour or so, which is how one refusal of a file
- * open in Office became many. The failure does not quote the session's url,
- * which is what signs its requests.
+ * session. A session whose upload fails is cancelled: while one is open
+ * OneDrive answers every other upload of the same file with a 409, "currently
+ * being uploaded", for a quarter of an hour or so, which is how one refusal of
+ * a file open in Office became many. The failure does not quote the session's
+ * url, which is what signs its requests.
*
* @param uploadUrl the url the session answered with
* @param file the file to send
@@ -1413,7 +1516,8 @@ public static String sendToUploadSession(String uploadUrl, File file, String fil
try {
HttpHelperUtility.deleteRequestStringBody(uploadUrl, null, null, null, null);
} catch (Exception notCancelled) {
- // the url is what signs the request, so the failure, which quotes it, is not logged
+ // the url is what signs the request, so the failure, which quotes it, is not
+ // logged
classLogger.warn("Could not cancel the upload session of {}; Graph refuses another upload of that "
+ "file until the session expires", fileName);
}
@@ -1432,8 +1536,10 @@ public static String sendToUploadSession(String uploadUrl, File file, String fil
* @return the failure itself when it does not quote the url, or one that says
* the same without it
*/
- // shortcut: HttpHelperUtility logs the url itself when the connection drops, before it throws;
- // have it leave query strings out of its own lines once that has to be closed too
+ // shortcut: HttpHelperUtility logs the url itself when the connection drops,
+ // before it throws;
+ // have it leave query strings out of its own lines once that has to be closed
+ // too
private static Exception withoutUrl(Exception e, String url, String what) {
String why = e.getMessage();
if (why == null || !why.contains(url)) {
@@ -1510,11 +1616,11 @@ private static void remember(File copy, String driveId, String itemId, String ve
* @param itemId the folder the upload names, or null
* @param folderPath the folder path the upload names, or null
* @param fileName the name the upload gives the file
- * @return true when the local file is a recorded copy and the upload names
- * its original, or names nowhere else to go
+ * @return true when the local file is a recorded copy and the upload names its
+ * original, or names nowhere else to go
*/
- private static boolean isOriginal(String accessToken, File copy, String driveId, String itemId,
- String folderPath, String fileName) throws Exception {
+ private static boolean isOriginal(String accessToken, File copy, String driveId, String itemId, String folderPath,
+ String fileName) throws Exception {
MicrosoftOneDriveSources.Source source = MicrosoftOneDriveSources.read(copy);
if (source == null) {
return false;
@@ -1892,4 +1998,48 @@ private static void requireNoPathSeparator(String fileName) {
}
}
-}
+ public static List> listFilesFixture(String driveId, String itemId, String path, int limit)
+ throws Exception {
+ String fixturePath = fixturePath();
+ if (fixturePath == null) {
+ throw new IllegalStateException("Fixture mode is not enabled. Set " + FIXTURE_SETTING
+ + " environment variable or DI property and ensure onedrive-m365.json exists next to mail.json.");
+ }
+ OneDriveFixtureSource source = OneDriveFixtureSource.of(fixturePath);
+ List> items = new ArrayList<>();
+ for (Map item : source.listFiles(driveId, itemId, path, limit)) {
+ items.add(MicrosoftOneDriveItemMapper.toDriveItem(item, driveId));
+ }
+ return items;
+ }
+
+ public static Map getItemFixture(String driveId, String itemId) throws Exception {
+ String fixturePath = fixturePath();
+ if (fixturePath == null) {
+ throw new IllegalStateException("Fixture mode is not enabled. Set " + FIXTURE_SETTING
+ + " environment variable or DI property and ensure onedrive-m365.json exists next to mail.json.");
+ }
+ OneDriveFixtureSource source = OneDriveFixtureSource.of(fixturePath);
+ Map item = source.getItem(driveId, itemId);
+ if (item == null) {
+ throw new IllegalStateException("Microsoft Graph returned no OneDrive item for drive = " + driveId
+ + ", item = " + itemId + " in fixture data");
+ }
+ return MicrosoftOneDriveItemMapper.toDriveItem(item, driveId);
+ }
+
+ public static String getFileContentFixture(String driveId, String itemId) throws Exception {
+ String fixturePath = fixturePath();
+ if (fixturePath == null) {
+ throw new IllegalStateException("Fixture mode is not enabled. Set " + FIXTURE_SETTING
+ + " environment variable or DI property and ensure onedrive-m365.json exists next to mail.json.");
+ }
+ OneDriveFixtureSource source = OneDriveFixtureSource.of(fixturePath);
+ String content = source.getFileContent(driveId, itemId);
+ if (content == null) {
+ throw new IllegalStateException(
+ "No content found for item in fixture data for drive = " + driveId + ", item = " + itemId);
+ }
+ return content;
+ }
+}
\ No newline at end of file
diff --git a/src/prerna/io/connector/ms/onedrive/MicrosoftOneDriveItemMapper.java b/src/prerna/io/connector/ms/onedrive/MicrosoftOneDriveItemMapper.java
index e951c19e00..a4e6b7f2bf 100644
--- a/src/prerna/io/connector/ms/onedrive/MicrosoftOneDriveItemMapper.java
+++ b/src/prerna/io/connector/ms/onedrive/MicrosoftOneDriveItemMapper.java
@@ -101,7 +101,9 @@ public static Map toDriveItem(Map item, String f
Map described = effectiveItem(item);
Map output = new LinkedHashMap<>();
- output.put(ID, described.get(ID));
+ Object itemId = described.get(ID);
+ output.put("id", itemId);
+ output.put("itemId", itemId);
putIfPresent(output, NAME, described.get(NAME));
putIfPresent(output, DRIVE_ID, driveIdOf(item, fallbackDriveId));
output.put("isFolder", described.get(FOLDER) != null);
@@ -221,6 +223,11 @@ public static String driveIdOf(Map item, String fallback) {
if (driveId == null) {
driveId = ValueUtils.toNonBlankString(parentField(item, DRIVE_ID));
}
+ // For fixture data, check if driveId is at the root level
+ if (driveId == null) {
+ Object rootDriveId = item.get(DRIVE_ID);
+ driveId = ValueUtils.toNonBlankString(rootDriveId == null ? null : rootDriveId.toString());
+ }
return driveId == null ? fallback : driveId;
}
diff --git a/src/prerna/io/connector/ms/onedrive/MicrosoftOneDriveListFilesReactor.java b/src/prerna/io/connector/ms/onedrive/MicrosoftOneDriveListFilesReactor.java
index e70bac6291..035ce93bb2 100644
--- a/src/prerna/io/connector/ms/onedrive/MicrosoftOneDriveListFilesReactor.java
+++ b/src/prerna/io/connector/ms/onedrive/MicrosoftOneDriveListFilesReactor.java
@@ -88,6 +88,16 @@ protected NounMetadata executeAuthenticated() {
int limit = positiveInt(ReactorKeysEnum.LIMIT.getKey(), DEFAULT_LIMIT, MAX_LIMIT);
try {
+ String fixturePath = MicrosoftOneDriveHelper.fixturePath();
+ if (fixturePath != null) {
+ List> files = MicrosoftOneDriveHelper.listFilesFixture(driveId, itemId, path,
+ limit);
+ Map output = new LinkedHashMap<>();
+ output.put("count", files.size());
+ output.put("files", files);
+ return new NounMetadata(output, PixelDataType.CUSTOM_DATA_STRUCTURE);
+ }
+
User user = this.insight.getUser();
String accessToken = MicrosoftLoginUtils.getValidAccessToken(user);
List> files = MicrosoftOneDriveHelper.listFiles(accessToken, driveId, itemId, path,
diff --git a/src/prerna/io/connector/ms/teams/MicrosoftTeamsHelper.java b/src/prerna/io/connector/ms/teams/MicrosoftTeamsHelper.java
index 944cbe6fa5..df3cb477d4 100644
--- a/src/prerna/io/connector/ms/teams/MicrosoftTeamsHelper.java
+++ b/src/prerna/io/connector/ms/teams/MicrosoftTeamsHelper.java
@@ -28,9 +28,11 @@
package prerna.io.connector.ms.teams;
import java.io.File;
+import java.io.Reader;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
@@ -38,6 +40,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
import org.apache.hc.core5.http.ContentType;
import org.apache.logging.log4j.LogManager;
@@ -53,6 +56,7 @@
import prerna.io.connector.ms.MicrosoftTokenFiller;
import prerna.io.connector.ms.onedrive.MicrosoftOneDriveHelper;
import prerna.security.HttpHelperUtility;
+import prerna.util.Utility;
/**
* Helper utility for Microsoft Teams connector operations.
@@ -101,6 +105,8 @@ public class MicrosoftTeamsHelper {
private static final List CONFLICT_BEHAVIORS = Arrays.asList(CONFLICT_FAIL, CONFLICT_RENAME,
CONFLICT_REPLACE);
+ private static final String FIXTURE_SETTING = "COLLABORATION_FIXTURE_MAIL";
+
/**
* Graph recommends a resumable upload session for anything larger than 4MB.
*/
@@ -113,6 +119,101 @@ private MicrosoftTeamsHelper() {
}
+ static String fixturePath() {
+ try {
+ String fixture = Utility.getDIHelperProperty(FIXTURE_SETTING);
+ if (fixture == null || fixture.isBlank()) {
+ fixture = System.getenv(FIXTURE_SETTING);
+ }
+ if (fixture == null || fixture.isBlank()) {
+ classLogger.debug("No fixture setting found for COLLABORATION_FIXTURE_MAIL");
+ return null;
+ }
+ fixture = fixture.trim();
+ classLogger.debug("Found fixture mail path: {}", fixture);
+ Path mailPath = Path.of(fixture);
+ Path teamsM365Path = mailPath.resolveSibling("teams-m365.json");
+ classLogger.debug("Looking for Teams M365 fixture at: {}", teamsM365Path);
+ if (Files.exists(teamsM365Path)) {
+ classLogger.debug("Teams M365 fixture found, using fixture mode");
+ return teamsM365Path.toString();
+ } else {
+ classLogger.debug("Teams M365 fixture not found at: {}", teamsM365Path);
+ return null;
+ }
+ } catch (Exception e) {
+ classLogger.error("Error checking for Teams fixture", e);
+ return null;
+ }
+ }
+
+ static class TeamsFixtureSource {
+ private static final Map LOADED = new ConcurrentHashMap<>();
+ private final List> teams = new ArrayList<>();
+ private final List> channels = new ArrayList<>();
+
+ @SuppressWarnings("unchecked")
+ private TeamsFixtureSource(String path) {
+ try (Reader reader = Files.newBufferedReader(Path.of(path), StandardCharsets.UTF_8)) {
+ Map fixture = new Gson().fromJson(reader, Map.class);
+ if (fixture.get("teams") instanceof List> teamsList) {
+ for (Object item : teamsList) {
+ teams.add((Map) item);
+ }
+ }
+ if (fixture.get("channels") instanceof List> channelsList) {
+ for (Object item : channelsList) {
+ channels.add((Map) item);
+ }
+ }
+ } catch (Exception e) {
+ throw new IllegalStateException("Cannot read the " + FIXTURE_SETTING + " file at " + path, e);
+ }
+ }
+
+ static TeamsFixtureSource of(String path) {
+ return (TeamsFixtureSource) LOADED.computeIfAbsent(path, p -> new TeamsFixtureSource(p));
+ }
+
+ List> listTeams(int limit) {
+ List> result = new ArrayList<>();
+ for (Map team : teams) {
+ result.add(team);
+ if (limit > 0 && result.size() >= limit) {
+ break;
+ }
+ }
+ return result;
+ }
+
+ List> listChannels(String teamId, int limit) {
+ List> result = new ArrayList<>();
+ for (Map channel : channels) {
+ if (teamId.equals(channel.get("teamId"))) {
+ result.add(channel);
+ if (limit > 0 && result.size() >= limit) {
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
+ Map getChannelFilesFolder(String teamId, String channelId) {
+ for (Map channel : channels) {
+ if (teamId.equals(channel.get("teamId")) && channelId.equals(channel.get("id"))) {
+ Map folder = new HashMap<>();
+ folder.put(DRIVE_ID, "drive-" + teamId);
+ folder.put(FOLDER_ID, "folder-" + channelId);
+ folder.put(NAME, channel.get(DISPLAY_NAME));
+ folder.put(WEB_URL, channel.get(WEB_URL));
+ return folder;
+ }
+ }
+ return null;
+ }
+ }
+
/**
* Lists the Microsoft Teams the current user has joined.
*
@@ -624,4 +725,56 @@ private static void requireValue(String value, String message) {
}
}
+ static List> listTeamsFixture(int limit) throws Exception {
+ String fixturePath = fixturePath();
+ if (fixturePath == null) {
+ throw new IllegalStateException("Fixture mode is not enabled. Set " + FIXTURE_SETTING + " environment variable or DI property and ensure teams-m365.json exists next to mail.json.");
+ }
+ TeamsFixtureSource source = TeamsFixtureSource.of(fixturePath);
+ return source.listTeams(limit);
+ }
+
+ static List> listChannelsFixture(String teamId, int limit) throws Exception {
+ String fixturePath = fixturePath();
+ if (fixturePath == null) {
+ throw new IllegalStateException("Fixture mode is not enabled. Set " + FIXTURE_SETTING + " environment variable or DI property and ensure teams-m365.json exists next to mail.json.");
+ }
+ TeamsFixtureSource source = TeamsFixtureSource.of(fixturePath);
+ return source.listChannels(teamId, limit);
+ }
+
+ public static Map getChannelFilesFolderFixture(String teamId, String channelId) throws Exception {
+ String fixturePath = fixturePath();
+ if (fixturePath == null) {
+ throw new IllegalStateException("Fixture mode is not enabled. Set " + FIXTURE_SETTING + " environment variable or DI property and ensure teams-m365.json exists next to mail.json.");
+ }
+ TeamsFixtureSource source = TeamsFixtureSource.of(fixturePath);
+ Map folder = source.getChannelFilesFolder(teamId, channelId);
+ if (folder == null) {
+ throw new IllegalStateException(
+ "Microsoft Graph returned no folder for channel id = " + channelId + " in team " + teamId + " in fixture data");
+ }
+ return folder;
+ }
+
+ public static List> listChannelFilesFixture(String teamId, String channelId,
+ String folderPath, int limit) throws Exception {
+ String fixturePath = fixturePath();
+ if (fixturePath == null) {
+ throw new IllegalStateException("Fixture mode is not enabled. Set " + FIXTURE_SETTING + " environment variable or DI property and ensure teams-m365.json exists next to mail.json.");
+ }
+ Map folder = getChannelFilesFolderFixture(teamId, channelId);
+ String driveId = folder.get(DRIVE_ID).toString();
+ List> items = new ArrayList<>();
+ try {
+ List> files = MicrosoftOneDriveHelper.listFilesFixture(driveId, folder.get(FOLDER_ID).toString(), folderPath, limit);
+ for (Map file : files) {
+ items.add(toDriveItemMap(file, driveId));
+ }
+ } catch (Exception e) {
+ classLogger.debug("Channel files not found in OneDrive fixture, returning empty list", e);
+ }
+ return items;
+ }
+
}
diff --git a/src/prerna/io/connector/ms/teams/MicrosoftTeamsListChannelsReactor.java b/src/prerna/io/connector/ms/teams/MicrosoftTeamsListChannelsReactor.java
index bcdcd03c07..015ef24f50 100644
--- a/src/prerna/io/connector/ms/teams/MicrosoftTeamsListChannelsReactor.java
+++ b/src/prerna/io/connector/ms/teams/MicrosoftTeamsListChannelsReactor.java
@@ -27,6 +27,7 @@
*******************************************************************************/
package prerna.io.connector.ms.teams;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -90,10 +91,22 @@ protected NounMetadata executeAuthenticated() {
}
try {
+ String fixturePath = MicrosoftTeamsHelper.fixturePath();
+ if (fixturePath != null) {
+ List> channels = MicrosoftTeamsHelper.listChannelsFixture(teamId, limit);
+ Map output = new LinkedHashMap<>();
+ output.put("count", channels.size());
+ output.put("channels", channels);
+ return new NounMetadata(output, PixelDataType.CUSTOM_DATA_STRUCTURE);
+ }
+
User user = this.insight.getUser();
String accessToken = MicrosoftLoginUtils.getValidAccessToken(user);
- List> result = MicrosoftTeamsHelper.listChannels(accessToken, teamId, limit);
- return new NounMetadata(result, PixelDataType.CUSTOM_DATA_STRUCTURE);
+ List> channels = MicrosoftTeamsHelper.listChannels(accessToken, teamId, limit);
+ Map output = new LinkedHashMap<>();
+ output.put("count", channels.size());
+ output.put("channels", channels);
+ return new NounMetadata(output, PixelDataType.CUSTOM_DATA_STRUCTURE);
} catch (SemossPixelException e) {
classLogger.error("Error while listing Microsoft Teams channels", e);
throw e;
diff --git a/src/prerna/io/connector/ms/teams/MicrosoftTeamsListFilesReactor.java b/src/prerna/io/connector/ms/teams/MicrosoftTeamsListFilesReactor.java
index 647e484c39..51c465cae4 100644
--- a/src/prerna/io/connector/ms/teams/MicrosoftTeamsListFilesReactor.java
+++ b/src/prerna/io/connector/ms/teams/MicrosoftTeamsListFilesReactor.java
@@ -27,6 +27,7 @@
*******************************************************************************/
package prerna.io.connector.ms.teams;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -96,11 +97,24 @@ protected NounMetadata executeAuthenticated() {
}
try {
+ String fixturePath = MicrosoftTeamsHelper.fixturePath();
+ if (fixturePath != null) {
+ List> files = MicrosoftTeamsHelper.listChannelFilesFixture(teamId, channelId,
+ folderPath, limit);
+ Map output = new LinkedHashMap<>();
+ output.put("count", files.size());
+ output.put("files", files);
+ return new NounMetadata(output, PixelDataType.CUSTOM_DATA_STRUCTURE);
+ }
+
User user = this.insight.getUser();
String accessToken = MicrosoftLoginUtils.getValidAccessToken(user);
- List> result = MicrosoftTeamsHelper.listChannelFiles(accessToken, teamId, channelId,
+ List> files = MicrosoftTeamsHelper.listChannelFiles(accessToken, teamId, channelId,
folderPath, limit);
- return new NounMetadata(result, PixelDataType.CUSTOM_DATA_STRUCTURE);
+ Map output = new LinkedHashMap<>();
+ output.put("count", files.size());
+ output.put("files", files);
+ return new NounMetadata(output, PixelDataType.CUSTOM_DATA_STRUCTURE);
} catch (SemossPixelException e) {
classLogger.error("Error while listing Microsoft Teams channel files", e);
throw e;
diff --git a/src/prerna/io/connector/ms/teams/MicrosoftTeamsListTeamsReactor.java b/src/prerna/io/connector/ms/teams/MicrosoftTeamsListTeamsReactor.java
index 76c9c467f5..048ba613e1 100644
--- a/src/prerna/io/connector/ms/teams/MicrosoftTeamsListTeamsReactor.java
+++ b/src/prerna/io/connector/ms/teams/MicrosoftTeamsListTeamsReactor.java
@@ -27,6 +27,7 @@
*******************************************************************************/
package prerna.io.connector.ms.teams;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -83,10 +84,22 @@ protected NounMetadata executeAuthenticated() {
}
try {
+ String fixturePath = MicrosoftTeamsHelper.fixturePath();
+ if (fixturePath != null) {
+ List> teams = MicrosoftTeamsHelper.listTeamsFixture(limit);
+ Map output = new LinkedHashMap<>();
+ output.put("count", teams.size());
+ output.put("teams", teams);
+ return new NounMetadata(output, PixelDataType.CUSTOM_DATA_STRUCTURE);
+ }
+
User user = this.insight.getUser();
String accessToken = MicrosoftLoginUtils.getValidAccessToken(user);
- List> result = MicrosoftTeamsHelper.listTeams(accessToken, limit);
- return new NounMetadata(result, PixelDataType.CUSTOM_DATA_STRUCTURE);
+ List> teams = MicrosoftTeamsHelper.listTeams(accessToken, limit);
+ Map output = new LinkedHashMap<>();
+ output.put("count", teams.size());
+ output.put("teams", teams);
+ return new NounMetadata(output, PixelDataType.CUSTOM_DATA_STRUCTURE);
} catch (SemossPixelException e) {
classLogger.error("Error while listing Microsoft Teams", e);
throw e;
diff --git a/src/prerna/reactor/collaboration/BrainLinkTopicM365FolderReactor.java b/src/prerna/reactor/collaboration/BrainLinkTopicM365FolderReactor.java
new file mode 100644
index 0000000000..43e9dc664a
--- /dev/null
+++ b/src/prerna/reactor/collaboration/BrainLinkTopicM365FolderReactor.java
@@ -0,0 +1,164 @@
+/*******************************************************************************
+ * Copyright 2015 Defense Health Agency (DHA)
+ *
+ * If your use of this software does not include any GPLv2 components:
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ----------------------------------------------------------------------------
+ * If your use of this software includes any GPLv2 components:
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *******************************************************************************/
+package prerna.reactor.collaboration;
+
+import java.util.Map;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import prerna.auth.User;
+import prerna.collaboration.BrainTopicLinks;
+import prerna.collaboration.BrainTopicLinks.FolderPointer;
+import prerna.io.connector.ms.MicrosoftLoginUtils;
+import prerna.io.connector.ms.onedrive.MicrosoftOneDriveHelper;
+import prerna.io.connector.ms.onedrive.MicrosoftOneDriveItemMapper;
+import prerna.io.connector.ms.teams.MicrosoftTeamsHelper;
+import prerna.reactor.ReactorDefinition;
+import prerna.sablecc2.om.execptions.SemossPixelException;
+import prerna.sablecc2.om.nounmeta.NounMetadata;
+import prerna.util.ValueUtils;
+
+// BrainLinkTopicM365Folder(topicId=["..."], kind=["drive_folder"], driveId=["..."], itemId=["..."]);
+public class BrainLinkTopicM365FolderReactor extends AbstractCollaborationReactor {
+
+ private static final Logger classLogger = LogManager.getLogger(BrainLinkTopicM365FolderReactor.class);
+
+ private static final String TOPIC_ID = "topicId";
+ private static final String KIND = "kind";
+ private static final String DRIVE_ID = "driveId";
+ private static final String ITEM_ID = "itemId";
+ private static final String TEAM_ID = "teamId";
+ private static final String CHANNEL_ID = "channelId";
+ private static final String LINK_ID = "linkId";
+ private static final String REMOVE = "remove";
+
+ public BrainLinkTopicM365FolderReactor() {
+ this.keysToGet = new String[] { TOPIC_ID, KIND, DRIVE_ID, ITEM_ID, TEAM_ID, CHANNEL_ID, LINK_ID, REMOVE };
+ this.keyRequired = new int[] { 1, 0, 0, 0, 0, 0, 0, 0 };
+ }
+
+ @Override
+ public NounMetadata execute() {
+ User user = getUser();
+ String topicId = getStringFromKeyOrCurRow(TOPIC_ID);
+ if (Boolean.TRUE.equals(getBoolean(REMOVE))) {
+ return mapResult(BrainTopicLinks.unlink(user, topicId, getString(LINK_ID)));
+ }
+
+ String kind = ValueUtils.trimToNull(getString(KIND));
+ if (kind == null) {
+ throw new IllegalArgumentException("A folder kind is required");
+ }
+ try {
+ String accessToken = null;
+ String fixturePath = MicrosoftOneDriveHelper.fixturePath();
+ if (fixturePath == null) {
+ accessToken = MicrosoftLoginUtils.getValidAccessToken(user);
+ }
+ FolderPointer folder = resolveFolder(accessToken, kind);
+ return mapResult(BrainTopicLinks.link(user, topicId, folder));
+ } catch (SemossPixelException e) {
+ classLogger.error("Could not get a Microsoft token to link a topic folder", e);
+ throw e;
+ } catch (IllegalArgumentException e) {
+ classLogger.error("Invalid input while linking a Microsoft folder to a topic", e);
+ throw e;
+ } catch (Exception e) {
+ classLogger.error("Could not verify a Microsoft folder for a topic link", e);
+ throw new SemossPixelException("Could not verify folder access in Microsoft 365: " + e.getMessage());
+ }
+ }
+
+ private FolderPointer resolveFolder(String accessToken, String kind) throws Exception {
+ if (BrainTopicLinks.DRIVE_FOLDER.equals(kind)) {
+ String driveId = ValueUtils.trimToNull(getString(DRIVE_ID));
+ String itemId = ValueUtils.trimToNull(getString(ITEM_ID));
+ if (driveId == null || itemId == null) {
+ throw new IllegalArgumentException("A drive id and item id are required for a OneDrive folder");
+ }
+ Map item;
+ String fixturePath = MicrosoftOneDriveHelper.fixturePath();
+ if (fixturePath != null) {
+ item = MicrosoftOneDriveHelper.getItemFixture(driveId, itemId);
+ } else {
+ item = MicrosoftOneDriveHelper.getItem(accessToken, driveId, itemId, null);
+ }
+ if (!Boolean.TRUE.equals(item.get("isFolder"))) {
+ throw new IllegalArgumentException("The selected OneDrive item is not a folder");
+ }
+ String resolvedDriveId = ValueUtils.toNonBlankString(item.get("driveId"));
+ if (resolvedDriveId == null) {
+ resolvedDriveId = driveId;
+ }
+ String resolvedItemId = MicrosoftOneDriveItemMapper.itemIdOf(item);
+ return new FolderPointer(kind, resolvedDriveId, resolvedItemId, null, null, null,
+ MicrosoftOneDriveItemMapper.nameOf(item), string(item, "webUrl"));
+ }
+ if (BrainTopicLinks.CHANNEL_FILES.equals(kind)) {
+ String teamId = ValueUtils.trimToNull(getString(TEAM_ID));
+ String channelId = ValueUtils.trimToNull(getString(CHANNEL_ID));
+ if (teamId == null || channelId == null) {
+ throw new IllegalArgumentException("A team id and channel id are required for Teams channel files");
+ }
+ Map folder;
+ String fixturePath = MicrosoftOneDriveHelper.fixturePath();
+ if (fixturePath != null) {
+ folder = MicrosoftTeamsHelper.getChannelFilesFolderFixture(teamId, channelId);
+ } else {
+ folder = MicrosoftTeamsHelper.getChannelFilesFolder(accessToken, teamId, channelId);
+ }
+ return new FolderPointer(kind, string(folder, "driveId"), string(folder, "folderId"), teamId, channelId,
+ null, string(folder, "name"), string(folder, "webUrl"));
+ }
+ if (BrainTopicLinks.SHAREPOINT_FOLDER.equals(kind)) {
+ throw new IllegalArgumentException("SharePoint folder linking is not available in this operation");
+ }
+ throw new IllegalArgumentException("Unsupported folder kind");
+ }
+
+ private static String string(Map values, String key) {
+ return ValueUtils.toNonBlankString(values.get(key));
+ }
+
+ @Override
+ public ReactorDefinition getReactorDefinition() {
+ return super.getReactorDefinition()
+ .description("Links a OneDrive folder or Teams channel's Files folder to a topic after checking it "
+ + "with the owner's Microsoft account; set remove to true with linkId to unlink it.")
+ .key(TOPIC_ID, "Topic id")
+ .key(KIND, "Folder kind: drive_folder for OneDrive or channel_files for Teams")
+ .key(DRIVE_ID, "Microsoft drive id of a OneDrive folder")
+ .key(ITEM_ID, "Microsoft item id of a OneDrive folder")
+ .key(TEAM_ID, "Microsoft Teams team id that owns the channel")
+ .key(CHANNEL_ID, "Microsoft Teams channel id whose Files folder should be linked")
+ .key(LINK_ID, "Linked folder id, required when removing a link")
+ .key(REMOVE, "true removes the existing link instead of creating one")
+ .output(Map.class, "The topic id and its linked OneDrive and Teams folders");
+ }
+}
diff --git a/src/prerna/reactor/collaboration/BrainListTopicM365FoldersReactor.java b/src/prerna/reactor/collaboration/BrainListTopicM365FoldersReactor.java
new file mode 100644
index 0000000000..f1c81fa91b
--- /dev/null
+++ b/src/prerna/reactor/collaboration/BrainListTopicM365FoldersReactor.java
@@ -0,0 +1,61 @@
+/*******************************************************************************
+ * Copyright 2015 Defense Health Agency (DHA)
+ *
+ * If your use of this software does not include any GPLv2 components:
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ----------------------------------------------------------------------------
+ * If your use of this software includes any GPLv2 components:
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *******************************************************************************/
+package prerna.reactor.collaboration;
+
+import java.util.Map;
+
+import prerna.auth.User;
+import prerna.collaboration.BrainTopicLinks;
+import prerna.reactor.ReactorDefinition;
+import prerna.sablecc2.om.nounmeta.NounMetadata;
+
+// BrainListTopicM365Folders(topicId=["..."]);
+public class BrainListTopicM365FoldersReactor extends AbstractCollaborationReactor {
+
+ private static final String TOPIC_ID = "topicId";
+
+ public BrainListTopicM365FoldersReactor() {
+ this.keysToGet = new String[] { TOPIC_ID };
+ this.keyRequired = new int[] { 1 };
+ }
+
+ @Override
+ public NounMetadata execute() {
+ User user = getUser();
+ String topicId = getStringFromKeyOrCurRow(TOPIC_ID);
+ return mapResult(BrainTopicLinks.list(user, topicId));
+ }
+
+ @Override
+ public ReactorDefinition getReactorDefinition() {
+ return super.getReactorDefinition()
+ .description("Lists the OneDrive and Teams folders linked to a topic, including their access state.")
+ .key(TOPIC_ID, "Topic id")
+ .output(Map.class, "The topic id and its linked folders");
+ }
+}