Search in sources :

Example 1 with TargetType

use of de.metas.ui.web.notification.UserNotification.TargetType in project metasfresh-webui-api by metasfresh.

the class JSONNotificationTarget method of.

/**
 * @param notification
 * @return JSON target or <code>null</code>
 */
static final JSONNotificationTarget of(final UserNotification notification) {
    final TargetType targetType = notification.getTargetType();
    switch(targetType) {
        case Window:
            final JSONNotificationTarget jsonTarget = new JSONNotificationTarget(targetType);
            jsonTarget.documentType = notification.getTargetDocumentType();
            jsonTarget.documentId = notification.getTargetDocumentId();
            return jsonTarget;
        case None:
            return null;
    }
    logger.warn("Unknown targetType={} for {}. Returning null.", targetType, notification);
    return null;
}
Also used : TargetType(de.metas.ui.web.notification.UserNotification.TargetType)

Example 2 with TargetType

use of de.metas.ui.web.notification.UserNotification.TargetType in project metasfresh-webui-api by metasfresh.

the class UserNotificationRepository method save.

private UserNotification save(final Event event, final int recipientUserId) {
    final I_AD_Note notificationPO = InterfaceWrapperHelper.newInstance(I_AD_Note.class);
    // 
    // Important
    final boolean important = DisplayType.toBoolean(event.getProperty(EVENT_PARAM_Important), false);
    notificationPO.setAD_User_ID(recipientUserId);
    notificationPO.setIsImportant(important);
    // 
    // detailADMessage -> AD_Message
    int adMessageId = -1;
    final String detailADMessage = event.getDetailADMessage();
    if (!Check.isEmpty(detailADMessage, true)) {
        adMessageId = Services.get(IADMessageDAO.class).retrieveIdByValue(detailADMessage);
    }
    if (adMessageId <= 0) {
        adMessageId = Services.get(IADMessageDAO.class).retrieveIdByValue(DEFAULT_AD_MESSAGE);
    }
    notificationPO.setAD_Message_ID(adMessageId);
    // 
    // detailADMessageParams
    final Map<String, Object> detailADMessageParams = event.getProperties();
    try {
        final String detailADMessageParamsJSON = jsonMapper.writeValueAsString(detailADMessageParams);
        notificationPO.setAD_Message_ParamsJSON(detailADMessageParamsJSON);
    } catch (final JsonProcessingException e) {
        throw new AdempiereException(e);
    }
    // 
    // detailPlain
    final String detailPlain = event.getDetailPlain();
    notificationPO.setTextMsg(detailPlain);
    // 
    // Target: window (from document record)
    final ITableRecordReference targetRecord = event.getRecord();
    final TargetType targetType;
    final String targetTableName;
    final int targetADTableId;
    final int targetRecordId;
    final int targetADWindowId;
    if (targetRecord != null) {
        targetType = TargetType.Window;
        final Object suggestedWindowIdObj = event.getProperty(Event.PROPERTY_SuggestedWindowId);
        final int suggestedWindowId = suggestedWindowIdObj instanceof Number ? ((Number) suggestedWindowIdObj).intValue() : -1;
        if (suggestedWindowId > 0) {
            targetADWindowId = suggestedWindowId;
            targetADTableId = targetRecord.getAD_Table_ID();
            targetTableName = targetRecord.getTableName();
            targetRecordId = targetRecord.getRecord_ID();
        } else {
            final RecordZoomWindowFinder recordWindowFinder = RecordZoomWindowFinder.newInstance(targetRecord);
            targetADWindowId = recordWindowFinder.findAD_Window_ID();
            targetADTableId = recordWindowFinder.getAD_Table_ID();
            targetTableName = recordWindowFinder.getTableName();
            targetRecordId = recordWindowFinder.getRecord_ID();
        }
    } else {
        targetType = TargetType.None;
        targetADWindowId = -1;
        targetTableName = null;
        targetADTableId = -1;
        targetRecordId = -1;
    }
    notificationPO.setAD_Table_ID(targetADTableId);
    notificationPO.setRecord_ID(targetRecordId);
    notificationPO.setAD_Window_ID(targetADWindowId);
    // 
    // 
    InterfaceWrapperHelper.save(notificationPO);
    return UserNotification.builder().id(String.valueOf(notificationPO.getAD_Note_ID())).timestamp(notificationPO.getCreated().getTime()).important(important).recipientUserId(recipientUserId).detailADMessage(detailADMessage).detailADMessageParams(detailADMessageParams).detailPlain(detailPlain).targetType(targetType).targetTableName(targetTableName).targetRecordId(targetRecordId).targetADWindowId(targetADWindowId).build();
}
Also used : ITableRecordReference(org.adempiere.util.lang.ITableRecordReference) AdempiereException(org.adempiere.exceptions.AdempiereException) TargetType(de.metas.ui.web.notification.UserNotification.TargetType) RecordZoomWindowFinder(org.adempiere.model.RecordZoomWindowFinder) I_AD_Note(org.compiere.model.I_AD_Note) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with TargetType

use of de.metas.ui.web.notification.UserNotification.TargetType in project metasfresh-webui-api by metasfresh.

the class DebugRestController method postEvent.

@RequestMapping(value = "/eventBus/postEvent", method = RequestMethod.GET)
public void postEvent(// 
@RequestParam(name = "topicName", defaultValue = "de.metas.event.GeneralNotifications") final String topicName, // 
@RequestParam(name = "message", defaultValue = "test message") final String message, // 
@RequestParam(name = "toUserId", defaultValue = "-1") final int toUserId, // 
@RequestParam(name = "important", defaultValue = "false") final boolean important, // 
@RequestParam(name = "targetType", required = false) final String targetTypeStr, // 
@RequestParam(name = "targetDocumentType", required = false, defaultValue = "143") final String targetDocumentType, // 
@RequestParam(name = "targetDocumentId", required = false) final String targetDocumentId) {
    final Topic topic = Topic.builder().name(topicName).type(Type.LOCAL).build();
    final Builder eventBuilder = Event.builder().setSummary("summary").setDetailPlain(message).putProperty(UserNotificationRepository.EVENT_PARAM_Important, important);
    if (toUserId > 0) {
        eventBuilder.addRecipient_User_ID(toUserId);
    }
    final TargetType targetType = Check.isEmpty(targetTypeStr) ? null : TargetType.forJsonValue(targetTypeStr);
    if (targetType == TargetType.Window) {
        final String targetTableName = documentCollection.getDocumentDescriptorFactory().getDocumentDescriptor(WindowId.fromJson(targetDocumentType)).getEntityDescriptor().getTableName();
        final TableRecordReference targetRecord = TableRecordReference.of(targetTableName, Integer.parseInt(targetDocumentId));
        eventBuilder.setRecord(targetRecord);
    }
    final Event event = eventBuilder.build();
    Services.get(IEventBusFactory.class).getEventBus(topic).postEvent(event);
}
Also used : TableRecordReference(org.adempiere.util.lang.impl.TableRecordReference) Builder(de.metas.event.Event.Builder) TargetType(de.metas.ui.web.notification.UserNotification.TargetType) Event(de.metas.event.Event) Topic(de.metas.event.Topic) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

TargetType (de.metas.ui.web.notification.UserNotification.TargetType)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Event (de.metas.event.Event)1 Builder (de.metas.event.Event.Builder)1 Topic (de.metas.event.Topic)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 RecordZoomWindowFinder (org.adempiere.model.RecordZoomWindowFinder)1 ITableRecordReference (org.adempiere.util.lang.ITableRecordReference)1 TableRecordReference (org.adempiere.util.lang.impl.TableRecordReference)1 I_AD_Note (org.compiere.model.I_AD_Note)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1