Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/custom_async_init'
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Toth committed Feb 13, 2017
2 parents ca3616e + c25efe0 commit f2c5e8b
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.hpe.adm.octane.ideplugins.intellij.ui.treetable.EntityTreeView;
import com.hpe.adm.octane.ideplugins.intellij.util.NotificationUtil;
import com.hpe.adm.octane.ideplugins.services.EntityService;
import com.hpe.adm.octane.ideplugins.services.MetadataService;
import com.hpe.adm.octane.ideplugins.services.TestService;
import com.hpe.adm.octane.ideplugins.services.UserService;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettings;
Expand Down Expand Up @@ -113,6 +114,7 @@ protected void configure() {
});

//Services
bind(MetadataService.class).asEagerSingleton();
bind(TestService.class);
bind(SharedSpaceLevelRequestService.class);
bind(EntityService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
import com.hpe.adm.octane.ideplugins.intellij.PluginModule;
import com.hpe.adm.octane.ideplugins.intellij.settings.IdePluginPersistentState;
import com.hpe.adm.octane.ideplugins.intellij.ui.components.WelcomeViewComponent;
import com.hpe.adm.octane.ideplugins.intellij.ui.customcomponents.LoadingWidget;
import com.hpe.adm.octane.ideplugins.intellij.ui.main.MainPresenter;
import com.hpe.adm.octane.ideplugins.intellij.ui.util.UiUtil;
import com.hpe.adm.octane.ideplugins.services.MetadataService;
import com.hpe.adm.octane.ideplugins.services.TestService;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettings;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettingsProvider;
import com.hpe.adm.octane.ideplugins.services.exception.ServiceException;
import com.hpe.adm.octane.ideplugins.services.filtering.Entity;
import com.hpe.adm.octane.ideplugins.services.nonentity.SharedSpaceLevelRequestService;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

/**
* Entry point, will create the IntelliJ tool window
*/
Expand All @@ -29,6 +36,7 @@ public class EntryPoint implements ToolWindowFactory {
* This method can be called by multiple IntelliJ's running at the same time,
* it's important that the state of any one tool window that contains the IDE plugin
* does not affect the state of other potentially open tool windows
*
* @param project
* @param toolWindow
*/
Expand All @@ -45,53 +53,68 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
final ConnectionSettingsProvider connectionSettingsProvider = pluginModule.getInstance(ConnectionSettingsProvider.class);

Runnable mainToolWindowContentControl = () -> {
try{
ConnectionSettings connectionSettings = connectionSettingsProvider.getConnectionSettings();

if(connectionSettings == null || connectionSettings.isEmpty()){
throw new ServiceException("No connection settings configured");
}

TestService testService = pluginModule.getInstance(TestService.class);
testService.testConnection(connectionSettings);

// Make sure you only instantiate other services (including the ones in the Presenter hierarchy,
// after you tested the connection settings with the test service

//Add the workspace name to the ToolWindow content tab name
SharedSpaceLevelRequestService sharedSpaceLevelRequestService = pluginModule.getInstance(SharedSpaceLevelRequestService.class);
String workspaceDisplayName = " [" + sharedSpaceLevelRequestService.getCurrentWorkspaceName() + "]";

//Create the presenter hierarchy, DI will inject view instances
MainPresenter mainPresenter = pluginModule.getInstance(MainPresenter.class);
setContent(toolWindow, mainPresenter.getView(), workspaceDisplayName);
} catch (Exception ex){
pluginModule.getInstance(IdePluginPersistentState.class).clearState(IdePluginPersistentState.Key.ACTIVE_WORK_ITEM);
WelcomeViewComponent welcomeViewComponent;

// If there were previously configured connection settings
// show a slightly different message in the welcome view
if(!connectionSettingsProvider.getConnectionSettings().isEmpty()){
welcomeViewComponent = new WelcomeViewComponent(
project,
"Your previously saved connection settings do not seem to work",
"Please go to settings and test your connection to Octane");

//also show a notification with the exception
UiUtil.showWarningBalloon(project,
"Failed to connect to Octane",
"Your previously saved connection settings do not seem to work <br> Error: " + ex.getMessage(),
NotificationType.WARNING);
} else {
//In this case (probably), the plugin was never configured on this project before
welcomeViewComponent = new WelcomeViewComponent(project);
EntryPoint.this.setContent(toolWindow, () -> new LoadingWidget(), "");

Task.Backgroundable backgroundTask = new Task.Backgroundable(project, "Loading Workspace", false) {
public void run(@NotNull ProgressIndicator indicator) {
try {
ConnectionSettings connectionSettings = connectionSettingsProvider.getConnectionSettings();

if (connectionSettings == null || connectionSettings.isEmpty()) {
throw new ServiceException("No connection settings configured");
}

TestService testService = pluginModule.getInstance(TestService.class);
testService.testConnection(connectionSettings);

// Make sure you only instantiate other services (including the ones in the Presenter hierarchy,
// after you tested the connection settings with the test service

//Add the workspace name to the ToolWindow content tab name
SharedSpaceLevelRequestService sharedSpaceLevelRequestService = pluginModule.getInstance(SharedSpaceLevelRequestService.class);
String workspaceDisplayName = " [" + sharedSpaceLevelRequestService.getCurrentWorkspaceName() + "]";

//Eager init the metadata service, it's an eager singleton
MetadataService metadataService = pluginModule.getInstance(MetadataService.class);
metadataService.eagerInit(Entity.values());

SwingUtilities.invokeAndWait(() -> {
//Create the presenter hierarchy, DI will inject view instances
MainPresenter mainPresenter = pluginModule.getInstance(MainPresenter.class);
setContent(toolWindow, mainPresenter.getView(), workspaceDisplayName);
});

} catch (Exception ex) {
pluginModule.getInstance(IdePluginPersistentState.class).clearState(IdePluginPersistentState.Key.ACTIVE_WORK_ITEM);
WelcomeViewComponent welcomeViewComponent;

// If there were previously configured connection settings
// show a slightly different message in the welcome view
if (!connectionSettingsProvider.getConnectionSettings().isEmpty()) {
welcomeViewComponent = new WelcomeViewComponent(
project,
"Your previously saved connection settings do not seem to work",
"Please go to settings and test your connection to Octane");

//also show a notification with the exception
UiUtil.showWarningBalloon(project,
"Failed to connect to Octane",
"Your previously saved connection settings do not seem to work <br> Error: " + ex.getMessage(),
NotificationType.WARNING);
} else {
//In this case (probably), the plugin was never configured on this project before
welcomeViewComponent = new WelcomeViewComponent(project);
}

log.info("Showing welcome view, cause: " + ex);

//Show the welcome view
setContent(toolWindow, welcomeViewComponent, "");
}
}

log.info("Showing welcome view, cause: " + ex);

//Show the welcome view
setContent(toolWindow, welcomeViewComponent, "");
}
};
backgroundTask.queue();
};

//Run at the start of the application
Expand All @@ -100,9 +123,11 @@ public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindo
}

private void setContent(ToolWindow toolWindow, HasComponent hasComponent, String workspaceName) {
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
toolWindow.getContentManager().removeAllContents(true);
toolWindow.getContentManager().addContent(contentFactory.createContent(hasComponent.getComponent(), workspaceName, false));
SwingUtilities.invokeLater(() -> {
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
toolWindow.getContentManager().removeAllContents(true);
toolWindow.getContentManager().addContent(contentFactory.createContent(hasComponent.getComponent(), workspaceName, false));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

Expand Down Expand Up @@ -120,7 +119,7 @@ public void actionPerformed(AnActionEvent e) {
this.entityTreeView.addSeparatorToToolbar();

//eager init my work service support cache
Arrays.asList(Entity.values()).forEach(myWorkService::isFollowingEntitySupported);
//Arrays.asList(Entity.values()).forEach(myWorkService::isFollowingEntitySupported);
setContextMenuFactory(this.entityTreeView);

entityTreeView.setComponentWhenEmpty(() -> new NoSearchResultsPanel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.awt.event.MouseEvent;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -159,7 +158,7 @@ public void setView(@Named("myWorkEntityTreeView") EntityTreeView entityTreeView
this.entityTreeTableView = entityTreeView;

//eager init my work service support cache
Arrays.asList(Entity.values()).forEach(myWorkService::isFollowingEntitySupported);
//Arrays.asList(Entity.values()).forEach(myWorkService::isFollowingEntitySupported);
setContextMenuFactory(entityTreeView);

//start presenting
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.hpe.adm.octane.ideplugins.services;

import com.google.inject.Inject;
import com.hpe.adm.nga.sdk.Octane;
import com.hpe.adm.nga.sdk.metadata.FieldMetadata;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettingsProvider;
import com.hpe.adm.octane.ideplugins.services.connection.OctaneProvider;
import com.hpe.adm.octane.ideplugins.services.filtering.Entity;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class MetadataService {

@Inject
private OctaneProvider octaneProvider;

@Inject
private ConnectionSettingsProvider connectionSettingsProvider;

private Map<Entity, Collection<FieldMetadata>> cache;

public boolean hasFields(Entity entityType, String... fieldNames){

if(cache == null){
cache = new ConcurrentHashMap<>();
init();
}

Octane octane = octaneProvider.getOctane();

Collection<FieldMetadata> fields;

if(!cache.containsKey(entityType)){
fields = octane.metadata().fields(entityType.getEntityName()).execute();
cache.put(entityType, fields);
} else {
fields = cache.get(entityType);
}

List<String> responseFieldNames = fields.stream().map(FieldMetadata::getName).collect(Collectors.toList());

return Arrays.stream(fieldNames)
.allMatch(responseFieldNames::contains);
}

public void eagerInit(Entity... entities){
if(cache == null){
cache = new ConcurrentHashMap<>();
init();
}

Octane octane = octaneProvider.getOctane();

Arrays.stream(entities)
.parallel()
.forEach(entityType -> cache.put(entityType, octane.metadata().fields(entityType.getEntityName()).execute()));
}

private void init(){
cache = new ConcurrentHashMap<>();
connectionSettingsProvider.addChangeHandler(()-> cache.clear());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import com.google.inject.Inject;
import com.hpe.adm.nga.sdk.Octane;
import com.hpe.adm.nga.sdk.Query;
import com.hpe.adm.nga.sdk.metadata.FieldMetadata;
import com.hpe.adm.nga.sdk.model.EntityModel;
import com.hpe.adm.nga.sdk.model.MultiReferenceFieldModel;
import com.hpe.adm.octane.ideplugins.services.connection.ConnectionSettingsProvider;
import com.hpe.adm.octane.ideplugins.services.connection.OctaneProvider;
import com.hpe.adm.octane.ideplugins.services.exception.ServiceException;
import com.hpe.adm.octane.ideplugins.services.exception.ServiceRuntimeException;
Expand All @@ -30,7 +28,7 @@ public class MyWorkService {
private OctaneProvider octaneProvider;

@Inject
private ConnectionSettingsProvider connectionSettingsProvider;
private MetadataService metadataService;

public static final String FOLLOW_ITEMS_OWNER_FIELD = "my_follow_items_owner";
public static final String NEW_ITEMS_OWNER_FIELD = "my_new_items_owner";
Expand Down Expand Up @@ -87,7 +85,6 @@ public Collection<EntityModel> getMyWork(Map<Entity, Set<String>> fieldListMap)
.and(createPhaseQuery(TASK, "new", "inprogress"))
);


filterCriteria.put(MANUAL_TEST_RUN,
createCurrentUserQuery("run_by")
.and(MANUAL_TEST_RUN.createMatchSubtypeQueryBuilder())
Expand Down Expand Up @@ -221,30 +218,8 @@ private Query.QueryBuilder createCurrentUserQuery(String fieldName) {
}


private Map<Entity, Boolean> followingSupportEntityMap;

public boolean isFollowingEntitySupported(Entity entityType) {

//init cache map
if (followingSupportEntityMap == null) {
followingSupportEntityMap = new HashMap<>();
//Clear on settings changed
//connectionSettingsProvider.addChangeHandler(() -> followingSupportEntityMap.clear());
}

if (followingSupportEntityMap.containsKey(entityType)) {
return followingSupportEntityMap.get(entityType);
}

Octane octane = octaneProvider.getOctane();
Collection<FieldMetadata> fields = octane.metadata().fields(entityType.getEntityName()).execute();
boolean followFieldExits =
fields.stream().anyMatch(fieldMetadata -> FOLLOW_ITEMS_OWNER_FIELD.equals(fieldMetadata.getName()));
boolean newFieldExits =
fields.stream().anyMatch(fieldMetadata -> NEW_ITEMS_OWNER_FIELD.equals(fieldMetadata.getName()));

followingSupportEntityMap.put(entityType, followFieldExits && newFieldExits);
return followFieldExits && newFieldExits;
return metadataService.hasFields(entityType, FOLLOW_ITEMS_OWNER_FIELD, NEW_ITEMS_OWNER_FIELD);
}

public boolean isCurrentUserFollowing(EntityModel entityModel) {
Expand Down

0 comments on commit f2c5e8b

Please sign in to comment.