updated ItemObserverd. Completed the Notification-Plugin

This commit is contained in:
Francesco Mangiacrapa 2024-04-17 15:53:45 +02:00
parent 6202bb71fe
commit 93576e5b19
7 changed files with 98 additions and 38 deletions

View File

@ -14,7 +14,6 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
@Getter
@ -22,9 +21,8 @@ import lombok.extern.slf4j.Slf4j;
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
@ToString
public class ItemObserved<T extends Project> implements ItemObservable {
private User userCaller;
private Context context;
private UseCaseDescriptor useCaseDescriptor;
@ -32,6 +30,10 @@ public class ItemObserved<T extends Project> implements ItemObservable {
private EventManager.Event event;
private Document optional;
public static enum OPTIONAL_FIELD {
message, preview_url
}
public String getProjectId() {
log.debug("Called getProjectId");
if (project == null)
@ -70,15 +72,18 @@ public class ItemObserved<T extends Project> implements ItemObservable {
return documentAsMap;
}
public void setOptionalMessage(String message){
public void setOptional(OPTIONAL_FIELD optionalField, String value) {
this.optional = new Document();
this.optional.put("message", message);
if (optional == null)
this.optional = new Document();
this.optional.put(optionalField.name(), value);
}
public String getOptionalMessage(){
if(this.optional!=null) {
return (String) this.optional.getOrDefault("message", null);
public String getOptionalValue(OPTIONAL_FIELD optionalField) {
if (this.optional != null) {
return this.optional.getString(optionalField.name());
}
return null;
}
@ -91,9 +96,9 @@ public class ItemObserved<T extends Project> implements ItemObservable {
builder.append(", context=");
builder.append(context);
builder.append(", useCaseDescriptor id=");
builder.append(useCaseDescriptor!=null?useCaseDescriptor.getId():null);
builder.append(useCaseDescriptor != null ? useCaseDescriptor.getId() : null);
builder.append(", project id=");
builder.append(project!=null?project.getId():null);
builder.append(project != null ? project.getId() : null);
builder.append(", event=");
builder.append(event);
builder.append(", optional=");

View File

@ -14,7 +14,7 @@ public class SimpleLifecycleTests {
// public void testFullCycle(){
// SimpleLifeCycleManager manager = plugins.get(SimpleLifeCycleManager.PLUGIN_ID);
//
// StepExecutionRequest request = new StepExecutionRequest();
// PerformStepRequest request = new PerformStepRequest();
//
// Project p = null;
//

View File

@ -24,6 +24,7 @@ import org.gcube.application.cms.implementations.utils.UserUtils;
import org.gcube.application.cms.plugins.events.EventManager;
import org.gcube.application.cms.plugins.events.EventManager.Event;
import org.gcube.application.cms.plugins.events.ItemObserved;
import org.gcube.application.cms.plugins.events.ItemObserved.OPTIONAL_FIELD;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.geoportal.common.model.configuration.Configuration;
import org.gcube.application.geoportal.common.model.document.Project;
@ -131,7 +132,7 @@ public class ProfiledDocuments {
AccountingInfo user = UserUtils.getCurrent().asInfo();
item.setUserCaller(user.getUser());
item.setContext(user.getContext());
item.setOptionalMessage(null);
item.setOptional(null);
item.setEvent(event);
item.setProject(theNewProject);
item.setUseCaseDescriptor(manager.getUseCaseDescriptor());
@ -171,7 +172,7 @@ public class ProfiledDocuments {
AccountingInfo user = UserUtils.getCurrent().asInfo();
item.setUserCaller(user.getUser());
item.setContext(user.getContext());
item.setOptionalMessage(null);
item.setOptional(null);
item.setEvent(event);
item.setProject(theUpdatedProject);
item.setUseCaseDescriptor(manager.getUseCaseDescriptor());
@ -332,7 +333,7 @@ public class ProfiledDocuments {
item.setUserCaller(user.getUser());
item.setContext(user.getContext());
item.setEvent(event);
item.setOptionalMessage(performStepRequest.getMessage());
item.setOptional(OPTIONAL_FIELD.message, performStepRequest.getMessage());
item.setUseCaseDescriptor(manager.getUseCaseDescriptor());
item.setProject(theProject);
log.info("By notifying event ({}, ID {})", event, item.getProjectId());

View File

@ -70,6 +70,19 @@
<artifactId>commons-text</artifactId>
<version>1.11.0</version>
</dependency>
<!-- Fix the issue with JUnit tests: java.lang.NoClassDefFoundError : javax/xml/soap/SOAPException -->
<!-- <dependency> -->
<!-- <groupId>javax.xml.soap</groupId> -->
<!-- <artifactId>javax.xml.soap-api</artifactId> -->
<!-- <version>1.4.0</version> -->
<!-- </dependency> -->
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.application.cms</groupId>
<artifactId>cms-test-commons</artifactId>

View File

@ -11,6 +11,7 @@ import org.gcube.application.cms.notifications.config.serviceaccount.GeoportalSe
import org.gcube.application.cms.notifications.social.SocialClients;
import org.gcube.application.cms.notifications.substitutor.NMessagesPlaceholdersSubstitutorUtil;
import org.gcube.application.cms.plugins.events.ItemObserved;
import org.gcube.application.cms.plugins.events.ItemObserved.OPTIONAL_FIELD;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.accounting.User;
import org.gcube.common.authorization.utils.manager.SecretManager;
@ -61,8 +62,9 @@ public class NotifyToSocial {
subject = nMPlaceholdersSUtil.replacePlaceholder(itemObserved, notify.getPlaceholder_title());
body = nMPlaceholdersSUtil.replacePlaceholder(itemObserved, notify.getPlaceholder_msg());
if (itemObserved.getOptionalMessage() != null) {
body += "\n" + itemObserved.getOptionalMessage();
String optionalMessage = itemObserved.getOptionalValue(OPTIONAL_FIELD.message);
if (optionalMessage != null) {
body += "\n" + optionalMessage;
}
log.info("Going to send notification to users: {}", recipientUsers);
@ -78,12 +80,24 @@ public class NotifyToSocial {
PostInputBean toWrite = new PostInputBean();
String title = nMPlaceholdersSUtil.replacePlaceholder(itemObserved, notify.getPlaceholder_title());
String body = nMPlaceholdersSUtil.replacePlaceholder(itemObserved, notify.getPlaceholder_msg());
if (itemObserved.getOptionalMessage() != null) {
body += "\n" + itemObserved.getOptionalMessage();
}
toWrite.setText(body);
if (title != null)
if (title != null) {
toWrite.setPreviewtitle(title);
}
String optionalMessage = itemObserved.getOptionalValue(OPTIONAL_FIELD.message);
if (optionalMessage != null) {
body += "\n" + optionalMessage;
}
toWrite.setText(body);
String previewURL = itemObserved.getOptionalValue(OPTIONAL_FIELD.preview_url);
System.out.println("previewURL: "+previewURL);
if (previewURL != null) {
toWrite.setPreviewurl(previewURL);
}
vrePost(toWrite, notify);
}
@ -106,14 +120,14 @@ public class NotifyToSocial {
secretManager.startSession(geoportalSecret);
log.debug("{} is going to send vrePost {}", SecretManagerProvider.instance.get().getUser().getUsername(),
toWrite);
// String bodyWithTag = toWrite.getText() + "\n #" + geoportalSecret.getUser().getUsername();
// log.debug("body with tag: {}", bodyWithTag);
// toWrite.setText(bodyWithTag);
String bodyWithTag = toWrite.getText() + "\n#" + geoportalSecret.getUser().getUsername();
log.debug("body with tag: {}", bodyWithTag);
toWrite.setText(bodyWithTag);
toWrite.setEnablenotification(true);
log.info("Calling social - going to write user post: {}", toWrite);
Post thePost = socialClients.writeUserPost(toWrite);
log.info("Calling social - going to write application post: {}", toWrite);
Post thePost = socialClients.writeApplication(toWrite);
log.info("{} post created: {} ", notify.getType(), thePost);
} catch (Exception e) {

View File

@ -49,7 +49,7 @@ public class SocialClients {
} catch (Exception e) {
log.error("Error on instacing {}: ", UserClient.class.getSimpleName(), e);
}
try {
this.notificationClient = new NotificationClient();
} catch (Exception e) {
@ -99,22 +99,48 @@ public class SocialClients {
return postClient.writeUserPost(toWrite);
}
public static org.slf4j.Logger getLog() {
return log;
/**
* Write application.
*
* @param toWrite the to write
* @return the post
*/
public Post writeApplication(PostInputBean toWrite) {
return postClient.writeApplicationPost(toWrite);
}
/**
* Gets the post client.
*
* @return the post client
*/
public PostClient getPostClient() {
return postClient;
}
/**
* Gets the messages client.
*
* @return the messages client
*/
public MessageClient getMessagesClient() {
return messagesClient;
}
/**
* Gets the user client.
*
* @return the user client
*/
public UserClient getUserClient() {
return userClient;
}
/**
* Gets the notification client.
*
* @return the notification client
*/
public NotificationClient getNotificationClient() {
return notificationClient;
}

View File

@ -18,6 +18,7 @@ import org.gcube.application.cms.notifications.manage.ManageDoActionNotification
import org.gcube.application.cms.plugins.events.EventManager;
import org.gcube.application.cms.plugins.events.EventManager.Event;
import org.gcube.application.cms.plugins.events.ItemObserved;
import org.gcube.application.cms.plugins.events.ItemObserved.OPTIONAL_FIELD;
import org.gcube.application.cms.plugins.faults.InitializationException;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.cms.tests.TestProfiles;
@ -48,7 +49,7 @@ public class NotificationsPluginTest extends BasicPluginTest {
/**
* Check plugin.
*/
@Before
//@Before
public void checkPlugin() {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
plugin = (NotificationsPlugin) plugins.get(NotificationsPlugin.DESCRIPTOR.getId());
@ -171,7 +172,7 @@ public class NotificationsPluginTest extends BasicPluginTest {
/**
* Check the VRE POST
*/
@Test
//@Test
public void checkNotifications_LIFECYCLE_STEP_PERFORMED_TO_APPROVE_SUBMITTED() {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
UseCaseDescriptor descriptor = TestProfiles.profiles.get("profiledConcessioni");
@ -192,7 +193,7 @@ public class NotificationsPluginTest extends BasicPluginTest {
//Setting creator
User creator = new User();
creator.setUsername("massimiliano.assante");
creator.setUsername("francesco.mangiacrapa");
item.getProject().getInfo().getCreationInfo().setUser(creator);
EventManager.Event event = Event.LIFECYCLE_STEP_PERFORMED;
@ -279,7 +280,7 @@ public class NotificationsPluginTest extends BasicPluginTest {
item.setProject(testProject);
item.setOptionalMessage("My great message");
item.setOptional(OPTIONAL_FIELD.message, "Il mio grande progetto");
return item;
}