2023-12-21 10:48:06 +01:00
|
|
|
package org.gcube.application.cms.plugins.events;
|
|
|
|
|
2024-03-21 15:53:09 +01:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2024-04-09 16:10:36 +02:00
|
|
|
import org.bson.Document;
|
2023-12-21 10:48:06 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.document.Project;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.accounting.Context;
|
|
|
|
import org.gcube.application.geoportal.common.model.document.accounting.User;
|
2024-01-17 16:09:48 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
|
2023-12-21 10:48:06 +01:00
|
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
import lombok.Setter;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
@Setter
|
|
|
|
@NoArgsConstructor
|
|
|
|
@AllArgsConstructor
|
|
|
|
@Slf4j
|
|
|
|
public class ItemObserved<T extends Project> implements ItemObservable {
|
2024-04-17 15:53:45 +02:00
|
|
|
|
2024-04-15 11:32:19 +02:00
|
|
|
private User userCaller;
|
|
|
|
private Context context;
|
|
|
|
private UseCaseDescriptor useCaseDescriptor;
|
|
|
|
private T project;
|
|
|
|
private EventManager.Event event;
|
|
|
|
private Document optional;
|
2023-12-21 10:48:06 +01:00
|
|
|
|
2024-04-17 15:53:45 +02:00
|
|
|
public static enum OPTIONAL_FIELD {
|
|
|
|
message, preview_url
|
|
|
|
}
|
|
|
|
|
2024-01-17 16:09:48 +01:00
|
|
|
public String getProjectId() {
|
|
|
|
log.debug("Called getProjectId");
|
2023-12-21 10:48:06 +01:00
|
|
|
if (project == null)
|
|
|
|
return null;
|
|
|
|
return project.getId();
|
|
|
|
}
|
2024-03-21 15:53:09 +01:00
|
|
|
|
2024-01-17 16:09:48 +01:00
|
|
|
@Override
|
|
|
|
public String getUCD_Id() {
|
|
|
|
log.debug("Called getUCD_Id");
|
|
|
|
if (useCaseDescriptor == null)
|
|
|
|
return null;
|
|
|
|
return useCaseDescriptor.getId();
|
|
|
|
}
|
2023-12-21 10:48:06 +01:00
|
|
|
|
2024-03-21 15:53:09 +01:00
|
|
|
public LinkedHashMap<String, Object> getDocumentEntries(int limit) {
|
|
|
|
|
|
|
|
LinkedHashMap<String, Object> documentAsMap = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Iterator<Entry<String, Object>> entrySetsIt = project.getTheDocument().entrySet().iterator();
|
|
|
|
int i = 0;
|
|
|
|
while (entrySetsIt.hasNext()) {
|
|
|
|
if (i + 1 > limit)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Entry<String, Object> entry = entrySetsIt.next();
|
|
|
|
documentAsMap.put(entry.getKey(), entry.getValue() != null ? entry.getValue().toString() : null);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
return documentAsMap;
|
|
|
|
}
|
2024-04-17 15:53:45 +02:00
|
|
|
|
|
|
|
public void setOptional(OPTIONAL_FIELD optionalField, String value) {
|
2024-04-09 16:10:36 +02:00
|
|
|
this.optional = new Document();
|
2024-04-17 15:53:45 +02:00
|
|
|
if (optional == null)
|
|
|
|
this.optional = new Document();
|
|
|
|
|
|
|
|
this.optional.put(optionalField.name(), value);
|
2024-04-09 16:10:36 +02:00
|
|
|
}
|
2024-04-17 15:53:45 +02:00
|
|
|
|
|
|
|
public String getOptionalValue(OPTIONAL_FIELD optionalField) {
|
|
|
|
if (this.optional != null) {
|
|
|
|
return this.optional.getString(optionalField.name());
|
2024-04-09 16:10:36 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2024-04-15 17:01:56 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
builder.append("ItemObserved [userCaller=");
|
|
|
|
builder.append(userCaller);
|
|
|
|
builder.append(", context=");
|
|
|
|
builder.append(context);
|
|
|
|
builder.append(", useCaseDescriptor id=");
|
2024-04-17 15:53:45 +02:00
|
|
|
builder.append(useCaseDescriptor != null ? useCaseDescriptor.getId() : null);
|
2024-04-15 17:01:56 +02:00
|
|
|
builder.append(", project id=");
|
2024-04-17 15:53:45 +02:00
|
|
|
builder.append(project != null ? project.getId() : null);
|
2024-04-15 17:01:56 +02:00
|
|
|
builder.append(", event=");
|
|
|
|
builder.append(event);
|
|
|
|
builder.append(", optional=");
|
|
|
|
builder.append(optional);
|
|
|
|
builder.append("]");
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
2023-12-21 10:48:06 +01:00
|
|
|
}
|