Commit b198a8ea authored by Marco Descher's avatar Marco Descher 🏔

[16515] Refactor MessageParty

parent 58a36e17
Pipeline #11254 failed with stages
in 1 minute and 7 seconds
......@@ -18,6 +18,7 @@ import ch.elexis.core.jpa.model.adapter.AbstractIdModelAdapter;
import ch.elexis.core.model.message.MessageParty;
import ch.elexis.core.model.util.internal.ModelUtil;
import ch.elexis.core.services.INamedQuery;
import ch.elexis.core.services.holder.CoreModelServiceHolder;
public class Message extends AbstractIdDeleteModelAdapter<ch.elexis.core.jpa.entities.Message>
implements Identifiable, IMessage {
......@@ -32,17 +33,21 @@ public class Message extends AbstractIdDeleteModelAdapter<ch.elexis.core.jpa.ent
@Override
public IMessageParty getSender(){
Kontakt origin = getEntity().getOrigin();
return findFirstUserForContact(origin).map(e -> new MessageParty(e)).orElse(null);
return findFirstUserForContact(origin).map(e -> new MessageParty(e.getId(), 0))
.orElse(null);
}
@SuppressWarnings("unchecked")
@Override
public void setSender(IMessageParty value){
IUser user = value.getUser();
if(user != null) {
IContact assignedContact = user.getAssignedContact();
Kontakt entity = ((AbstractIdModelAdapter<Kontakt>) assignedContact).getEntity();
getEntityMarkDirty().setOrigin(entity);
String identifier = value.getIdentifier();
if (value.getType() == 0) {
Optional<IUser> user = CoreModelServiceHolder.get().load(identifier, IUser.class);
if (user.isPresent()) {
IContact assignedContact = user.get().getAssignedContact();
Kontakt entity = ((AbstractIdModelAdapter<Kontakt>) assignedContact).getEntity();
getEntityMarkDirty().setOrigin(entity);
}
}
// TODO support for station, silently ignored by now
}
......@@ -54,7 +59,7 @@ public class Message extends AbstractIdDeleteModelAdapter<ch.elexis.core.jpa.ent
*/
@Override
public void setSender(IUser user){
setSender(new MessageParty(user));
setSender(new MessageParty(user.getId(), 0));
}
@Override
......@@ -62,19 +67,26 @@ public class Message extends AbstractIdDeleteModelAdapter<ch.elexis.core.jpa.ent
// TODO support for station
Kontakt destination = getEntity().getDestination();
Optional<MessageParty> messageParty =
findFirstUserForContact(destination).map(e -> new MessageParty(e));
findFirstUserForContact(destination).map(e -> new MessageParty(e.getId(), 0));
return (messageParty.isPresent()) ? Collections.singletonList(messageParty.get())
: new ArrayList<>();
}
@SuppressWarnings("unchecked")
@Override
public void addReceiver(IMessageParty user){
public void addReceiver(IMessageParty messageParty){
List<IMessageParty> receiver = getReceiver();
receiver.add(user);
receiver.add(messageParty);
// TODO suppport multiple receivers
Kontakt contact = ((AbstractIdModelAdapter<Kontakt>) user.getUser().getAssignedContact()).getEntity();
getEntityMarkDirty().setDestination(contact);
String identifier = messageParty.getIdentifier();
if (messageParty.getType() == 0) {
Optional<IUser> user = CoreModelServiceHolder.get().load(identifier, IUser.class);
if (user.isPresent()) {
Kontakt contact =
((AbstractIdModelAdapter<Kontakt>) user.get().getAssignedContact()).getEntity();
getEntityMarkDirty().setDestination(contact);
}
}
}
@Override
......
......@@ -27,14 +27,15 @@ import org.slf4j.LoggerFactory;
import ch.elexis.core.model.IMessage;
import ch.elexis.core.model.IUser;
import ch.elexis.core.model.builder.IMessageBuilder;
import ch.elexis.core.model.message.MessageCode;
import ch.elexis.core.model.message.MessageParty;
import ch.elexis.core.model.message.MessageParty.MessagePartyType;
import ch.elexis.core.model.tasks.IIdentifiedRunnable;
import ch.elexis.core.model.tasks.IIdentifiedRunnable.ReturnParameter;
import ch.elexis.core.model.tasks.IIdentifiedRunnableFactory;
import ch.elexis.core.model.tasks.TaskException;
import ch.elexis.core.services.IContextService;
import ch.elexis.core.services.IMessageService;
import ch.elexis.core.services.IModelService;
import ch.elexis.core.services.IQuery;
import ch.elexis.core.services.IQuery.COMPARATOR;
......@@ -56,7 +57,6 @@ public class TaskServiceImpl implements ITaskService {
private Logger logger;
private IModelService taskModelService;
private IModelService coreModelService;
private ExecutorService parallelExecutorService;
private ExecutorService singletonExecutorService;
......@@ -73,16 +73,14 @@ public class TaskServiceImpl implements ITaskService {
@Reference
private IContextService contextService;
@Reference
private IMessageService messageService;
@Reference(target = "(" + IModelService.SERVICEMODELNAME + "=ch.elexis.core.tasks.model)")
private void setModelService(IModelService modelService){
taskModelService = modelService;
}
@Reference(target = "(" + IModelService.SERVICEMODELNAME + "=ch.elexis.core.model)")
private void setCoreModelService(IModelService modelService){
coreModelService = modelService;
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC, bind = "bindRunnableWithContextFactory", unbind = "unbindRunnableWithContextFactory")
private List<IIdentifiedRunnableFactory> runnableWithContextFactories;
......@@ -245,12 +243,12 @@ public class TaskServiceImpl implements ITaskService {
}
@Override
public boolean removeTaskDescriptor(ITaskDescriptor taskDescriptor) throws TaskException {
public boolean removeTaskDescriptor(ITaskDescriptor taskDescriptor) throws TaskException{
if (taskDescriptor == null) {
throw new TaskException(TaskException.PARAMETERS_MISSING);
}
setActive(taskDescriptor, false);
return taskModelService.remove(taskDescriptor);
}
......@@ -278,10 +276,10 @@ public class TaskServiceImpl implements ITaskService {
}
private void sendMessageToOwner(ITask task, IUser owner, TaskState state){
IMessage message = new IMessageBuilder(coreModelService,
new MessageParty(contextService.getRootContext().getStationIdentifier()), owner)
.build();
message.addMessageCode(MessageCode.Key.SenderSubId, "ch.elexis.core.tasks.taskservice");
IMessage message = messageService
.prepare(new MessageParty(contextService.getRootContext().getStationIdentifier(),
MessagePartyType.STATION), new MessageParty(owner.getId()));
message.addMessageCode(MessageCode.Key.SenderSubId, "tasks.taskservice");
message.setSenderAcceptsAnswer(false);
String resultText;
......@@ -301,7 +299,7 @@ public class TaskServiceImpl implements ITaskService {
}
message.setMessageText(sb.toString());
coreModelService.save(message);
messageService.send(message);
}
@Override
......@@ -378,7 +376,7 @@ public class TaskServiceImpl implements ITaskService {
@Override
public void setActive(ITaskDescriptor taskDescriptor, boolean active) throws TaskException{
if(taskDescriptor.isActive() == active) {
if (taskDescriptor.isActive() == active) {
return;
}
......@@ -411,7 +409,7 @@ public class TaskServiceImpl implements ITaskService {
// will be supplied by the other task invoking us (we don't know about the
// supplied parameters)
return;
}
}
if (TaskTriggerType.SYSTEM_EVENT == taskDescriptor.getTriggerType()) {
// we will not check activation here, no formal required parameters
......@@ -450,9 +448,10 @@ public class TaskServiceImpl implements ITaskService {
}
@Override
public Optional<ITask> findLatestExecution(ITaskDescriptor taskDescriptor) {
public Optional<ITask> findLatestExecution(ITaskDescriptor taskDescriptor){
IQuery<ITask> query = taskModelService.getQuery(ITask.class);
query.and(ModelPackage.Literals.ITASK__DESCRIPTOR_ID, COMPARATOR.EQUALS, taskDescriptor.getId());
query.and(ModelPackage.Literals.ITASK__DESCRIPTOR_ID, COMPARATOR.EQUALS,
taskDescriptor.getId());
query.orderBy("lastupdate", ORDER.DESC);
query.limit(1);
List<ITask> result = query.execute();
......
......@@ -941,11 +941,15 @@
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="IMessageParty" abstract="true" interface="true">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="documentation" value="A user or station that either sends or receives a message. XOR user or stationId."/>
<details key="documentation" value="A station, user or other type if message party."/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EReference" name="user" eType="#//IUser"
changeable="false"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="stationId" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"
<eStructuralFeatures xsi:type="ecore:EAttribute" name="identifier" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"
changeable="false"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="type" lowerBound="1" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
changeable="false" defaultValueLiteral="0">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="documentation" value="The message party type: 0=user (default), 1=station; more to follow on requirement"/>
</eAnnotations>
</eStructuralFeatures>
</eClassifiers>
</ecore:EPackage>
......@@ -630,8 +630,8 @@
</genOperations>
</genClasses>
<genClasses image="false" ecoreClass="model.ecore#//IMessageParty">
<genFeatures notify="false" createChild="false" propertySortChoices="true" ecoreFeature="ecore:EReference model.ecore#//IMessageParty/user"/>
<genFeatures createChild="false" ecoreFeature="ecore:EAttribute model.ecore#//IMessageParty/stationId"/>
<genFeatures property="Readonly" createChild="false" ecoreFeature="ecore:EAttribute model.ecore#//IMessageParty/identifier"/>
<genFeatures createChild="false" ecoreFeature="ecore:EAttribute model.ecore#//IMessageParty/type"/>
</genClasses>
</genPackages>
</genmodel:GenModel>
......@@ -17,15 +17,15 @@ package ch.elexis.core.model;
* <!-- end-user-doc -->
*
* <!-- begin-model-doc -->
* A user or station that either sends or receives a message. XOR user or stationId.
* A station, user or other type if message party.
* <!-- end-model-doc -->
*
* <p>
* The following features are supported:
* </p>
* <ul>
* <li>{@link ch.elexis.core.model.IMessageParty#getUser <em>User</em>}</li>
* <li>{@link ch.elexis.core.model.IMessageParty#getStationId <em>Station Id</em>}</li>
* <li>{@link ch.elexis.core.model.IMessageParty#getIdentifier <em>Identifier</em>}</li>
* <li>{@link ch.elexis.core.model.IMessageParty#getType <em>Type</em>}</li>
* </ul>
*
* @see ch.elexis.core.model.ModelPackage#getIMessageParty()
......@@ -34,33 +34,29 @@ package ch.elexis.core.model;
*/
public interface IMessageParty {
/**
* Returns the value of the '<em><b>User</b></em>' reference.
* Returns the value of the '<em><b>Identifier</b></em>' attribute.
* <!-- begin-user-doc -->
* <p>
* If the meaning of the '<em>User</em>' reference isn't clear,
* there really should be more of a description here...
* </p>
* <!-- end-user-doc -->
* @return the value of the '<em>User</em>' reference.
* @see ch.elexis.core.model.ModelPackage#getIMessageParty_User()
* @return the value of the '<em>Identifier</em>' attribute.
* @see ch.elexis.core.model.ModelPackage#getIMessageParty_Identifier()
* @model changeable="false"
* @generated
*/
IUser getUser();
String getIdentifier();
/**
* Returns the value of the '<em><b>Station Id</b></em>' attribute.
* Returns the value of the '<em><b>Type</b></em>' attribute.
* The default value is <code>"0"</code>.
* <!-- begin-user-doc -->
* <p>
* If the meaning of the '<em>Station Id</em>' attribute isn't clear,
* there really should be more of a description here...
* </p>
* <!-- end-user-doc -->
* @return the value of the '<em>Station Id</em>' attribute.
* @see ch.elexis.core.model.ModelPackage#getIMessageParty_StationId()
* @model changeable="false"
* <!-- begin-model-doc -->
* The message party type: 0=user (default), 1=station; more to follow on requirement
* <!-- end-model-doc -->
* @return the value of the '<em>Type</em>' attribute.
* @see ch.elexis.core.model.ModelPackage#getIMessageParty_Type()
* @model default="0" required="true" changeable="false"
* @generated
*/
String getStationId();
int getType();
} // IMessageParty
......@@ -6315,22 +6315,22 @@ public interface ModelPackage extends EPackage {
int IMESSAGE_PARTY = 63;
/**
* The feature id for the '<em><b>User</b></em>' reference.
* The feature id for the '<em><b>Identifier</b></em>' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @ordered
*/
int IMESSAGE_PARTY__USER = 0;
int IMESSAGE_PARTY__IDENTIFIER = 0;
/**
* The feature id for the '<em><b>Station Id</b></em>' attribute.
* The feature id for the '<em><b>Type</b></em>' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
* @ordered
*/
int IMESSAGE_PARTY__STATION_ID = 1;
int IMESSAGE_PARTY__TYPE = 1;
/**
* The number of structural features of the '<em>IMessage Party</em>' class.
......@@ -10305,26 +10305,26 @@ public interface ModelPackage extends EPackage {
EClass getIMessageParty();
/**
* Returns the meta object for the reference '{@link ch.elexis.core.model.IMessageParty#getUser <em>User</em>}'.
* Returns the meta object for the attribute '{@link ch.elexis.core.model.IMessageParty#getIdentifier <em>Identifier</em>}'.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the meta object for the reference '<em>User</em>'.
* @see ch.elexis.core.model.IMessageParty#getUser()
* @return the meta object for the attribute '<em>Identifier</em>'.
* @see ch.elexis.core.model.IMessageParty#getIdentifier()
* @see #getIMessageParty()
* @generated
*/
EReference getIMessageParty_User();
EAttribute getIMessageParty_Identifier();
/**
* Returns the meta object for the attribute '{@link ch.elexis.core.model.IMessageParty#getStationId <em>Station Id</em>}'.
* Returns the meta object for the attribute '{@link ch.elexis.core.model.IMessageParty#getType <em>Type</em>}'.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @return the meta object for the attribute '<em>Station Id</em>'.
* @see ch.elexis.core.model.IMessageParty#getStationId()
* @return the meta object for the attribute '<em>Type</em>'.
* @see ch.elexis.core.model.IMessageParty#getType()
* @see #getIMessageParty()
* @generated
*/
EAttribute getIMessageParty_StationId();
EAttribute getIMessageParty_Type();
/**
* Returns the meta object for class '{@link ch.elexis.core.model.IAppointment <em>IAppointment</em>}'.
......@@ -13567,20 +13567,20 @@ public interface ModelPackage extends EPackage {
EClass IMESSAGE_PARTY = eINSTANCE.getIMessageParty();
/**
* The meta object literal for the '<em><b>User</b></em>' reference feature.
* The meta object literal for the '<em><b>Identifier</b></em>' attribute feature.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
EReference IMESSAGE_PARTY__USER = eINSTANCE.getIMessageParty_User();
EAttribute IMESSAGE_PARTY__IDENTIFIER = eINSTANCE.getIMessageParty_Identifier();
/**
* The meta object literal for the '<em><b>Station Id</b></em>' attribute feature.
* The meta object literal for the '<em><b>Type</b></em>' attribute feature.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
EAttribute IMESSAGE_PARTY__STATION_ID = eINSTANCE.getIMessageParty_StationId();
EAttribute IMESSAGE_PARTY__TYPE = eINSTANCE.getIMessageParty_Type();
/**
* The meta object literal for the '{@link ch.elexis.core.model.IAppointment <em>IAppointment</em>}' class.
......
......@@ -4424,8 +4424,8 @@ public class ModelPackageImpl extends EPackageImpl implements ModelPackage {
* @generated
*/
@Override
public EReference getIMessageParty_User() {
return (EReference)iMessagePartyEClass.getEStructuralFeatures().get(0);
public EAttribute getIMessageParty_Identifier() {
return (EAttribute)iMessagePartyEClass.getEStructuralFeatures().get(0);
}
/**
......@@ -4434,7 +4434,7 @@ public class ModelPackageImpl extends EPackageImpl implements ModelPackage {
* @generated
*/
@Override
public EAttribute getIMessageParty_StationId() {
public EAttribute getIMessageParty_Type() {
return (EAttribute)iMessagePartyEClass.getEStructuralFeatures().get(1);
}
......@@ -4911,8 +4911,8 @@ public class ModelPackageImpl extends EPackageImpl implements ModelPackage {
createEAttribute(iMessageEClass, IMESSAGE__MESSAGE_PRIORITY);
iMessagePartyEClass = createEClass(IMESSAGE_PARTY);
createEReference(iMessagePartyEClass, IMESSAGE_PARTY__USER);
createEAttribute(iMessagePartyEClass, IMESSAGE_PARTY__STATION_ID);
createEAttribute(iMessagePartyEClass, IMESSAGE_PARTY__IDENTIFIER);
createEAttribute(iMessagePartyEClass, IMESSAGE_PARTY__TYPE);
}
/**
......@@ -5768,8 +5768,8 @@ public class ModelPackageImpl extends EPackageImpl implements ModelPackage {
addEParameter(op, ecorePackage.getEString(), "value", 0, 1, IS_UNIQUE, IS_ORDERED);
initEClass(iMessagePartyEClass, IMessageParty.class, "IMessageParty", IS_ABSTRACT, IS_INTERFACE, IS_GENERATED_INSTANCE_CLASS);
initEReference(getIMessageParty_User(), this.getIUser(), null, "user", null, 0, 1, IMessageParty.class, !IS_TRANSIENT, !IS_VOLATILE, !IS_CHANGEABLE, !IS_COMPOSITE, IS_RESOLVE_PROXIES, !IS_UNSETTABLE, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEAttribute(getIMessageParty_StationId(), ecorePackage.getEString(), "stationId", null, 0, 1, IMessageParty.class, !IS_TRANSIENT, !IS_VOLATILE, !IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEAttribute(getIMessageParty_Identifier(), ecorePackage.getEString(), "identifier", null, 0, 1, IMessageParty.class, !IS_TRANSIENT, !IS_VOLATILE, !IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
initEAttribute(getIMessageParty_Type(), ecorePackage.getEInt(), "type", "0", 1, 1, IMessageParty.class, !IS_TRANSIENT, !IS_VOLATILE, !IS_CHANGEABLE, !IS_UNSETTABLE, !IS_ID, IS_UNIQUE, !IS_DERIVED, IS_ORDERED);
// Create resource
createResource(eNS_URI);
......
......@@ -8,24 +8,35 @@ import ch.elexis.core.model.IUser;
import ch.elexis.core.model.message.MessageParty;
import ch.elexis.core.services.IModelService;
public class IMessageBuilder extends AbstractBuilder<IMessage>{
public IMessageBuilder(IModelService modelService, IUser sender, IUser receiver) {
this(modelService, new MessageParty(sender), new MessageParty(receiver));
public class IMessageBuilder extends AbstractBuilder<IMessage> {
public IMessageBuilder(IModelService modelService, IUser sender, IUser receiver){
this(modelService, new MessageParty(sender.getId(), 0),
new MessageParty(receiver.getId(), 0));
}
public IMessageBuilder(IModelService modelService, IMessageParty sender, IUser receiver){
this(modelService, sender, new MessageParty(receiver.getId(), 0));
}
public IMessageBuilder(IModelService modelService, IMessageParty sender, IUser receiver) {
this(modelService, sender, new MessageParty(receiver));
public IMessageBuilder(IModelService modelService, IMessageParty sender,
IMessageParty receiver){
this(modelService, sender, new IMessageParty[] {
receiver
});
}
public IMessageBuilder(IModelService modelService, IMessageParty sender, IMessageParty receiver){
public IMessageBuilder(IModelService modelService, IMessageParty sender,
IMessageParty... receiver){
super(modelService);
object = modelService.create(IMessage.class);
object.setSender(sender);
object.addReceiver(receiver);
object.setDeleted(false);
object.setCreateDateTime(LocalDateTime.now());
for (IMessageParty iMessageParty : receiver) {
object.addReceiver(iMessageParty);
}
}
}
package ch.elexis.core.model.message;
import ch.elexis.core.model.IMessageParty;
import ch.elexis.core.model.IUser;
public class MessageParty implements IMessageParty {
private final IUser user;
private final String stationIdentifier;
private final String identifier;
private final int type;
public MessageParty(IUser user){
this.user = user;
this.stationIdentifier = null;
public static class MessagePartyType {
public static final int USER = 0;
public static final int STATION = 1;
}
public MessageParty(String stationIdentifier){
this.stationIdentifier = stationIdentifier;
this.user = null;
public MessageParty(String identifier){
this(identifier, MessagePartyType.USER);
}
public MessageParty(String identifier, int type){
this.identifier = identifier;
this.type = type;
}
@Override
public IUser getUser(){
return user;
public String getIdentifier(){
return identifier;
}
@Override
public String getStationId(){
return stationIdentifier;
public int getType(){
return type;
}
}
\ No newline at end of file
......@@ -28,7 +28,7 @@ public class MessageTest extends AbstractTest {
IMessage message = coreModelService.create(IMessage.class);
message.setSender(user);
message.addReceiver(new MessageParty(user));
message.addReceiver(new MessageParty(user.getId()));
message.setCreateDateTime(LocalDateTime.now());
message.setMessageText(
"That makes me angry, and when Dr. Evil gets angry Mr. Bigglesworth gets upset. And when Mr. Bigglesworth gets upset, people DIE!");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment