Skip to content

Commit

Permalink
search codetemplatelibrary usage from MC client
Browse files Browse the repository at this point in the history
  • Loading branch information
kayyagari committed Apr 7, 2021
1 parent de3d287 commit b5ab119
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
Expand All @@ -28,24 +30,37 @@
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import org.jdesktop.swingx.decorator.HighlighterFactory;
import org.jdesktop.swingx.treetable.AbstractMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableNode;

import com.kayyagari.ctrefs.shared.CodeTemplateUsageServletInterface;
import com.mirth.connect.client.core.ClientException;
import com.mirth.connect.client.ui.AbstractChannelTableNode;
import com.mirth.connect.client.ui.ChannelGroupStatus;
import com.mirth.connect.client.ui.ChannelTableColumnFactory;
import com.mirth.connect.client.ui.ChannelTableNode;
import com.mirth.connect.client.ui.ChannelTableNodeFactory;
import com.mirth.connect.client.ui.ChannelTreeTableModel;
import com.mirth.connect.client.ui.Frame;
import com.mirth.connect.client.ui.Mirth;
import com.mirth.connect.client.ui.PlatformUI;
import com.mirth.connect.client.ui.UIConstants;
import com.mirth.connect.client.ui.codetemplate.CodeTemplateLibraryTreeTableNode;
import com.mirth.connect.client.ui.codetemplate.CodeTemplatePanel;
import com.mirth.connect.client.ui.codetemplate.CodeTemplateRootTreeTableNode;
import com.mirth.connect.client.ui.codetemplate.CodeTemplateTableColumnFactory;
import com.mirth.connect.client.ui.codetemplate.CodeTemplateTreeTableModel;
import com.mirth.connect.client.ui.codetemplate.CodeTemplateTreeTableNode;
import com.mirth.connect.client.ui.components.MirthTreeTable;
import com.mirth.connect.model.Channel;
import com.mirth.connect.model.ChannelGroup;
import com.mirth.connect.model.ChannelStatus;
import com.mirth.connect.model.ChannelSummary;
import com.mirth.connect.model.codetemplates.BasicCodeTemplateProperties;
import com.mirth.connect.model.codetemplates.CodeTemplate;
import com.mirth.connect.model.codetemplates.CodeTemplateLibrary;
Expand All @@ -60,9 +75,11 @@ public class CodeTemplateUsagePanel extends JPanel {
private MirthTreeTable channelTable;
private JScrollPane templateTreeTableScrollPane;
private JScrollPane channelScrollPane;
private CodeTemplateUsageServletInterface ctUsageServlet;

public CodeTemplateUsagePanel() {
initComponents();
ctUsageServlet = PlatformUI.MIRTH_FRAME.mirthClient.getServlet(CodeTemplateUsageServletInterface.class);
}

public void search() throws Exception {
Expand All @@ -89,6 +106,38 @@ private void addCodeTemplates(List<CodeTemplateLibrary> lst) {
model.sort();
}

private void fetchUsageInfo() {
System.out.println("fetching usage info");
int selectedRow = templateTreeTable.getSelectedRow();
if (selectedRow >= 0) {
TreePath selectedPath = templateTreeTable.getPathForRow(selectedRow);
if (selectedPath != null) {
TreeTableNode selectedNode = (TreeTableNode) selectedPath.getLastPathComponent();
if (selectedNode instanceof CodeTemplateTreeTableNode) {
selectedNode = selectedNode.getParent();
}
String id = (String) selectedNode.getValueAt(CodeTemplatePanel.TEMPLATE_ID_COLUMN);

try {
List<ChannelSummary> lst = ctUsageServlet.findUsageOfCodeTemplateLib(id);

List<ChannelStatus> channelStatusLst = new ArrayList<>();
for(ChannelSummary cs : lst) {
channelStatusLst.add(cs.getChannelStatus());
}
ChannelGroup cg = new ChannelGroup();
ChannelGroupStatus cgs = new ChannelGroupStatus(cg, channelStatusLst);
ChannelTreeTableModel model = (ChannelTreeTableModel) channelTable.getTreeTableModel();

model.update(Collections.singletonList(cgs));
}
catch(ClientException e) {
PlatformUI.MIRTH_FRAME.alertThrowable(this, e);
}
}
}
}

private void initComponents() {
setLayout(new MigLayout("insets 8, novisualpadding, hidemode 3, fill"));
setBackground(UIConstants.BACKGROUND_COLOR);
Expand Down Expand Up @@ -136,6 +185,17 @@ private void initComponents() {

templateTreeTableScrollPane = new JScrollPane(templateTreeTable);
templateTreeTableScrollPane.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(0x6E6E6E)));

templateTreeTable.addMouseListener(new MouseAdapter() {

@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() > 1) {
fetchUsageInfo();
}
}

});

// Channel table
List<String> columns = Arrays.asList(new String[] { STATUS_COLUMN_NAME,
Expand All @@ -149,6 +209,18 @@ private void initComponents() {

ChannelTreeTableModel channelTreeModel = new ChannelTreeTableModel();
channelTreeModel.setColumnIdentifiers(columns);
channelTreeModel.setGroupModeEnabled(false);
channelTreeModel.setNodeFactory(new ChannelTableNodeFactory() {
@Override
public AbstractChannelTableNode createNode(ChannelGroupStatus groupStatus) {
return new ChannelTableNode(groupStatus);
}

@Override
public AbstractChannelTableNode createNode(ChannelStatus channelStatus) {
return new ChannelTableNode(channelStatus);
}
});
channelTable.setTreeTableModel(channelTreeModel);

channelTable.setDoubleBuffered(true);
Expand Down Expand Up @@ -208,12 +280,35 @@ private CodeTemplate createCT() {
return ct;
}

private void addTestChannel() {
ChannelGroup cg = new ChannelGroup();
List<ChannelStatus> lst = new ArrayList<>();
ChannelStatus cs = new ChannelStatus();
Channel ch = new Channel(UUID.randomUUID().toString());
ch.setDescription("test channel");
ch.setName("test channel");
ch.setNextMetaDataId(1);
ch.setRevision(1);

cs.setChannel(ch);
cs.setCodeTemplatesChanged(false);
cs.setDeployedDate(Calendar.getInstance());
cs.setDeployedRevisionDelta(1);
//cs.setLocalChannelId(ch.g);
lst.add(cs);
ChannelGroupStatus cgs = new ChannelGroupStatus(cg, lst);
ChannelTreeTableModel model = (ChannelTreeTableModel) channelTable.getTreeTableModel();

model.update(Collections.singletonList(cgs));
}

public static void main(String[] args) {
JFrame frame = new JFrame("CodeTemplate Usage");
frame.setBounds(100, 100, 400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CodeTemplateUsagePanel panel = new CodeTemplateUsagePanel();
panel.addTestCodeTemplates();
panel.addTestChannel();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(panel);
//frame.pack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.lang.reflect.Field;

import javax.swing.JDialog;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXTaskPane;

import com.kayyagari.ctrefs.shared.CodeTemplateUsageServletInterface;
Expand Down Expand Up @@ -34,7 +37,25 @@ public void start() {
}

public void showUsage() {
parent.alertInformation(parent, CodeTemplateUsageServletInterface.PLUGIN_NAME);
//parent.alertInformation(parent, CodeTemplateUsageServletInterface.PLUGIN_NAME);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
CodeTemplateUsagePanel panel = new CodeTemplateUsagePanel();
panel.search();
JDialog dl = new JDialog(parent);
dl.setTitle("CodeTemplate Usage Search");
dl.setBounds(parent.getBounds());
dl.getContentPane().add(panel);
dl.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dl.setVisible(true);
}
catch(Exception e) {
parent.alertThrowable(parent, e);
}
}
});
}

@Override
Expand Down
9 changes: 3 additions & 6 deletions package/resources/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
<url>mailto:kayyagari@apache.org</url>
<description>Plugin for detecting usages of a CodeTemplate in MirthConnect.</description>

<!-- <serverClasses>
<string>com.kayyagari.</string>
</serverClasses>
-->
<clientClasses>
<string>com.kayyagari.ctrefs.client.CodeTemplateUsagePlugin</string>
</clientClasses>
Expand All @@ -18,6 +14,7 @@
<library type="CLIENT" path="ct-refs-client-${project.version}.jar" />
<library type="SHARED" path="ct-refs-shared-${project.version}.jar" />

<!-- <apiProvider type="SERVLET_INTERFACE" name="" />
<apiProvider type="SERVER_CLASS" name="" /> -->
<apiProvider type="SERVLET_INTERFACE" name="com.kayyagari.ctrefs.shared.CodeTemplateUsageServletInterface" />

<apiProvider type="SERVER_CLASS" name="com.kayyagari.ctrefs.server.CodeTemplateUsageServlet" />
</pluginMetaData>
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.kayyagari.ctrefs.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.mirth.connect.client.core.ControllerException;
import com.mirth.connect.model.Channel;
import com.mirth.connect.model.ChannelHeader;
import com.mirth.connect.model.ChannelSummary;
import com.mirth.connect.model.codetemplates.CodeTemplate;
import com.mirth.connect.model.codetemplates.CodeTemplateLibrary;
import com.mirth.connect.server.controllers.ChannelController;
Expand All @@ -24,41 +28,34 @@ public CodeTemplateUsageFinder() {
ctController = ControllerFactory.getFactory().createCodeTemplateController();
}

public List<Channel> findUsagesOfCodeTemplate(String ctId) {
public List<ChannelSummary> findUsagesOfCodeTemplate(String ctId) throws ControllerException {
return findUsagesOf(ctId, false);
}

public List<Channel> findUsagesOfCodeTemplateLib(String ctLibId) {
public List<ChannelSummary> findUsagesOfCodeTemplateLib(String ctLibId) throws ControllerException {
return findUsagesOf(ctLibId, true);
}

private List<Channel> findUsagesOf(String id, boolean lib) {
List<Channel> includedIn = new ArrayList<>();
Set<String> cids = chController.getChannelIds();
List<Channel> channels = chController.getChannels(cids);
for(Channel ch : channels) {
List<CodeTemplateLibrary> ctLibs = ch.getExportData().getCodeTemplateLibraries();
if(ctLibs != null) {
outer:
for(CodeTemplateLibrary ctl : ctLibs) {
if(lib) {
if(id.equals(ctl.getId())) {
includedIn.add(ch);
break;
}
}
else {
for(CodeTemplate ct : ctl.getCodeTemplates()) {
if(id.equals(ct.getId())) {
includedIn.add(ch);
break outer;
}
}
}
}
}
}
private List<ChannelSummary> findUsagesOf(String id, boolean lib) throws ControllerException {
Map<String, ChannelHeader> includedIn = new HashMap<>();
//Set<String> cids = chController.getChannelIds();
//List<Channel> channels = chController.getChannels(cids);
ChannelHeader header = new ChannelHeader(0, null, false);

return includedIn;
List<CodeTemplateLibrary> codeTemplateLibraries = ctController.getLibraries(null, true);

for(CodeTemplateLibrary ctl : codeTemplateLibraries) {
if(id.equals(ctl.getId())) {
Set<String> chIds = ctl.getEnabledChannelIds();
if(chIds != null) {
chIds.stream().forEach(i -> {
includedIn.put(i, header);
});
}
break;
}
}

return chController.getChannelSummary(includedIn, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import com.kayyagari.ctrefs.shared.CodeTemplateUsageServletInterface;
import com.mirth.connect.client.core.ClientException;
import com.mirth.connect.client.core.ControllerException;
import com.mirth.connect.model.Channel;
import com.mirth.connect.model.ChannelSummary;
import com.mirth.connect.server.api.MirthServlet;

/**
Expand All @@ -25,12 +27,22 @@ public CodeTemplateUsageServlet(@Context HttpServletRequest request, @Context Se
}

@Override
public List<Channel> findUsageOfCodeTemplate(String ctId) throws ClientException {
return finder.findUsagesOfCodeTemplate(ctId);
public List<ChannelSummary> findUsageOfCodeTemplate(String ctId) throws ClientException {
try {
return finder.findUsagesOfCodeTemplate(ctId);
}
catch(ControllerException e) {
throw new ClientException(e);
}
}

@Override
public List<Channel> findUsageOfCodeTemplateLib(String ctLibId) throws ClientException {
return finder.findUsagesOfCodeTemplateLib(ctLibId);
public List<ChannelSummary> findUsageOfCodeTemplateLib(String ctLibId) throws ClientException {
try {
return finder.findUsagesOfCodeTemplateLib(ctLibId);
}
catch(ControllerException e) {
throw new ClientException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.mirth.connect.client.core.api.BaseServletInterface;
import com.mirth.connect.client.core.api.MirthOperation;
import com.mirth.connect.client.core.api.Param;
import com.mirth.connect.model.Channel;
import com.mirth.connect.model.ChannelSummary;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -34,13 +34,13 @@ public interface CodeTemplateUsageServletInterface extends BaseServletInterface

@GET
@Path("/usageOfCodeTemplate")
@Operation(summary = "Returns a List of all Channels that depend on the CodeTemplate identified by the given id")
@Operation(summary = "Returns a List of all ChannelSummary objects that depend on the CodeTemplate identified by the given id")
@MirthOperation(name = "findUsageOfCodeTemplate", display = "Find all the usages of the given CodeTemplate", permission = Permissions.CHANNELS_VIEW, type = ExecuteType.ASYNC, auditable = false)
public List<Channel> findUsageOfCodeTemplate(@Param("ctId") @Parameter(description = "Identifier of the CodeTemplate", required = true) @QueryParam("ctId") String ctId) throws ClientException;
public List<ChannelSummary> findUsageOfCodeTemplate(@Param("ctId") @Parameter(description = "Identifier of the CodeTemplate", required = true) @QueryParam("ctId") String ctId) throws ClientException;

@GET
@Path("/usageOfCodeTemplateLib")
@Operation(summary = "Returns a List of all Channels that depend on the CodeTemplateLibrary identified by the given id")
@Operation(summary = "Returns a List of all ChannelSummary objects that depend on the CodeTemplateLibrary identified by the given id")
@MirthOperation(name = "findUsageOfCodeTemplateLib", display = "Find all the usages of the given CodeTemplateLibrary", permission = Permissions.CHANNELS_VIEW, type = ExecuteType.ASYNC, auditable = false)
public List<Channel> findUsageOfCodeTemplateLib(@Param("ctLibId") @Parameter(description = "Identifier of the CodeTemplateLibrary", required = true) @QueryParam("ctLibId") String ctLibId) throws ClientException;
public List<ChannelSummary> findUsageOfCodeTemplateLib(@Param("ctLibId") @Parameter(description = "Identifier of the CodeTemplateLibrary", required = true) @QueryParam("ctLibId") String ctLibId) throws ClientException;
}

0 comments on commit b5ab119

Please sign in to comment.