Search in sources :

Example 16 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class AbstractBAFacet method connectFromNeed.

/**
 * This function is invoked when an won node sends an connect message to another won node and usually executes registered facet specific code.
 * The connection is identified by the connection object con. A rdf graph can be sent along with the request.
 *
 * @param con the connection object
 * @param content a rdf graph describing properties of the event. The null releative URI ('<>') inside that graph,
 *                as well as the base URI of the graph will be attached to the resource identifying the event.
 * @throws NoSuchConnectionException if connectionURI does not refer to an existing connection
 * @throws IllegalMessageForConnectionStateException if the message is not allowed in the current state of the connection
 */
@Override
public void connectFromNeed(final Connection con, final Model content, final WonMessage wonMessage) throws NoSuchNeedException, IllegalMessageForNeedStateException, ConnectionAlreadyExistsException {
    final Connection connectionForRunnable = con;
    executorService.execute(new Runnable() {

        @Override
        public void run() {
        // try {
        // ownerProtocolOwnerService.connect(
        // con.getNeedURI(),
        // con.getRemoteNeedURI(),
        // connectionForRunnable.getConnectionURI(),
        // content,
        // wonMessage);
        // } catch (WonProtocolException e) {
        // // we can't connect the connection. we send a deny back to the owner
        // // TODO should we introduce a new protocol method connectionFailed (because it's not an owner deny but some protocol-level error)?
        // // For now, we call the close method as if it had been called from the owner side
        // // TODO: even with this workaround, it would be good to send a content along with the close (so we can explain what happened).
        // logger.warn("could not connectFromNeed, sending close back. Exception was: ",e);
        // //          try {
        // //            // ToDo (FS): in this case a close wonMessage should be created and send instead!!
        // //            ownerFacingConnectionCommunicationService.close(
        // //                    connectionForRunnable.getConnectionURI(),
        // //                    content,
        // //                    wonMessage);
        // //          } catch (Exception e1) {
        // //            logger.warn("caught Exception sending close back from connectFromNeed:", e1);
        // //          }
        // }
        }
    });
}
Also used : Connection(won.protocol.model.Connection)

Example 17 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class AbstractFacet method connectFromNeed.

/**
 * This function is invoked when an won node sends an connect message to another won node and usually executes registered facet specific code.
 * The connection is identified by the connection object con. A rdf graph can be sent along with the request.
 *
 * @param con the connection object
 * @param content a rdf graph describing properties of the event. The null releative URI ('<>') inside that graph,
 *                as well as the base URI of the graph will be attached to the resource identifying the event.
 * @throws NoSuchConnectionException if connectionURI does not refer to an existing connection
 * @throws IllegalMessageForConnectionStateException if the message is not allowed in the current state of the connection
 */
@Override
public void connectFromNeed(final Connection con, final Model content, final WonMessage wonMessage) throws NoSuchNeedException, IllegalMessageForNeedStateException, ConnectionAlreadyExistsException {
    final Connection connectionForRunnable = con;
    executorService.execute(new Runnable() {

        @Override
        public void run() {
        // try {
        // ownerProtocolOwnerService.connect(con.getNeedURI(), con.getRemoteNeedURI(),
        // connectionForRunnable.getConnectionURI(), content, wonMessage);
        // } catch (WonProtocolException e) {
        // // we can't connect the connection. we send a deny back to the owner
        // // TODO should we introduce a new protocol method connectionFailed (because it's not an owner deny but some protocol-level error)?
        // // For now, we call the close method as if it had been called from the owner side
        // // TODO: even with this workaround, it would be good to send a content along with the close (so we can explain what happened).
        // logger.warn("could not connectFromNeed, sending close back. Exception was: ",e);
        // //          try {
        // //            // ToDo
        // //            ownerFacingConnectionCommunicationService.close(
        // //                    connectionForRunnable.getConnectionURI(), content, wonMessage);
        // //          } catch (Exception e1) {
        // //            logger.warn("caught Exception sending close back from connectFromNeed:", e1);
        // //          }
        // }
        }
    });
}
Also used : Connection(won.protocol.model.Connection)

Example 18 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class BAAtomicCCCoordinatorFacetImpl method isCoordinatorInFirstPhase.

private boolean isCoordinatorInFirstPhase(Connection con) {
    List<Connection> listOfCons = getAllCoordinatorParticipantConnections(con);
    URI ownerUri, needUri, currentStateURI;
    BACCState currentState;
    logger.debug("Checking if coordinator is in the first phase for coordinator " + con.getNeedURI() + "participant" + con.getRemoteNeedURI() + "and connection" + con.getConnectionURI());
    for (Connection c : listOfCons) {
        ownerUri = c.getNeedURI();
        needUri = c.getRemoteNeedURI();
        logger.debug("checking if coordinator is in first phase for coordinator {} and participant {}", ownerUri, needUri);
        currentStateURI = stateManager.getStateForNeedUri(ownerUri, needUri, getFacetType().getURI());
        logger.debug("Current coordinator state URI is {}: ", currentStateURI);
        if (currentStateURI == null) {
            // this happens when a connection is being opened in a parallel thread
            // and execution hasn't yet reached the point where its state is recorded
            // That other connection definitely hasn't caused phase 2 to start, so
            // we can safely say we're still in phase 1.
            // Therefore, continue with the next check.
            logger.debug("ignoring connection {} for phase one check as it" + " hasn't been processed by the facet logic yet", c.getConnectionURI());
            continue;
        }
        BACCState.Phase phase = BACCState.parsePhase(stateManager.getStatePhaseForNeedUri(ownerUri, needUri, getFacetType().getURI()).toString());
        if (// redundant, this or the previous condition can be omitted
        phase == null) {
            continue;
        }
        if (BACCState.getPhaseURI(phase).toString().equals(WON_TX.PHASE_SECOND.getURI().toString())) {
            return false;
        }
    }
    return true;
}
Also used : Connection(won.protocol.model.Connection) URI(java.net.URI) BACCState(won.node.facet.businessactivity.coordinatorcompletion.BACCState)

Example 19 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class BAAtomicPCCoordinatorFacetImpl method isCoordinatorInFirstPhase.

private boolean isCoordinatorInFirstPhase(Connection con) {
    List<Connection> listOfCons = getAllCoordinatorParticipantConnections(con);
    URI ownerUri, needUri, currentStateURI;
    BAPCState currentState;
    for (Connection c : listOfCons) {
        ownerUri = c.getNeedURI();
        needUri = c.getRemoteNeedURI();
        currentStateURI = stateManager.getStateForNeedUri(ownerUri, needUri, getFacetType().getURI());
        // logger.debug("Current state URI is {}: ", currentStateURI);
        if (currentStateURI == null) {
            // c.getConnectionURI());
            continue;
        }
        currentState = BAPCState.parseString(currentStateURI.toString());
        // logger.debug("phase is {}: ", currentState.getPhase());
        BAPCState.Phase phase = BAPCState.parsePhase(stateManager.getStatePhaseForNeedUri(ownerUri, needUri, getFacetType().getURI()).toString());
        if (// redundant, this or the previous condition can be omitted
        phase == null) {
            continue;
        }
        if (BAPCState.getPhaseURI(phase).toString().equals(WON_TX.PHASE_SECOND.getURI().toString())) {
            return false;
        }
    }
    return true;
}
Also used : Connection(won.protocol.model.Connection) URI(java.net.URI) BAPCState(won.node.facet.businessactivity.participantcompletion.BAPCState)

Example 20 with Connection

use of won.protocol.model.Connection in project webofneeds by researchstudio-sat.

the class LinkedDataServiceImpl method getConnectionDataset.

/**
 * ETag-aware method for obtaining connection data. Currently does not take into account new events, only changes
 * to the connection itself.
 * @param connectionUri
 * @param includeEventContainer
 * @param includeLatestEvent
 * @param etag
 * @return
 */
@Override
@Transactional
public DataWithEtag<Dataset> getConnectionDataset(final URI connectionUri, final boolean includeEventContainer, final boolean includeLatestEvent, final String etag) {
    DataWithEtag<Connection> data = null;
    Connection connection = null;
    data = needInformationService.readConnection(connectionUri, etag);
    if (data.isNotFound()) {
        return DataWithEtag.dataNotFound();
    }
    if (!data.isChanged()) {
        return DataWithEtag.dataNotChanged(data);
    }
    connection = data.getData();
    String newEtag = data.getEtag();
    // load the model from storage
    Model model = connectionModelMapper.toModel(connection);
    Model additionalData = connection.getDatasetHolder() == null ? null : connection.getDatasetHolder().getDataset().getDefaultModel();
    setNsPrefixes(model);
    if (additionalData != null) {
        model.add(additionalData);
    }
    // model.setNsPrefix("", connection.getConnectionURI().toString());
    // create connection member
    Resource connectionResource = model.getResource(connection.getConnectionURI().toString());
    // add WON node link
    connectionResource.addProperty(WON.HAS_WON_NODE, model.createResource(this.resourceURIPrefix));
    Dataset eventDataset = null;
    if (includeEventContainer) {
        // create event container and attach it to the member
        Resource eventContainer = model.createResource(connection.getConnectionURI().toString() + "/events");
        connectionResource.addProperty(WON.HAS_EVENT_CONTAINER, eventContainer);
        eventContainer.addProperty(RDF.type, WON.EVENT_CONTAINER);
        if (includeLatestEvent) {
            // we add the latest event in the connection
            Slice<MessageEventPlaceholder> latestEvents = messageEventRepository.findByParentURIFetchDatasetEagerly(connectionUri, new PageRequest(0, 1, new Sort(Sort.Direction.DESC, "creationDate")));
            if (latestEvents.hasContent()) {
                MessageEventPlaceholder event = latestEvents.getContent().get(0);
                // add the event's dataset
                eventDataset = setDefaults(event.getDatasetHolder().getDataset());
                // connect the event to its container
                eventContainer.addProperty(RDFS.member, model.getResource(event.getMessageURI().toString()));
            }
        }
        DatasetHolder datasetHolder = connection.getDatasetHolder();
        if (datasetHolder != null) {
            addAdditionalData(model, datasetHolder.getDataset().getDefaultModel(), connectionResource);
        }
    }
    Dataset connectionDataset = addBaseUriAndDefaultPrefixes(newDatasetWithNamedModel(createDataGraphUriFromResource(connectionResource), model));
    RdfUtils.addDatasetToDataset(connectionDataset, eventDataset);
    return new DataWithEtag<>(connectionDataset, newEtag, etag);
}
Also used : DatasetHolder(won.protocol.model.DatasetHolder) PageRequest(org.springframework.data.domain.PageRequest) Dataset(org.apache.jena.query.Dataset) Connection(won.protocol.model.Connection) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) Sort(org.springframework.data.domain.Sort) DataWithEtag(won.protocol.model.DataWithEtag) MessageEventPlaceholder(won.protocol.model.MessageEventPlaceholder) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Connection (won.protocol.model.Connection)50 URI (java.net.URI)30 WonMessage (won.protocol.message.WonMessage)20 Message (org.apache.camel.Message)12 Model (org.apache.jena.rdf.model.Model)12 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)9 Dataset (org.apache.jena.query.Dataset)6 WonURI (won.bot.framework.eventbot.action.impl.mail.model.WonURI)6 Resource (org.apache.jena.rdf.model.Resource)5 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)5 MissingMessagePropertyException (won.protocol.message.processor.exception.MissingMessagePropertyException)5 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)4 EventBus (won.bot.framework.eventbot.bus.EventBus)4 Event (won.bot.framework.eventbot.event.Event)4 CloseCommandEvent (won.bot.framework.eventbot.event.impl.command.close.CloseCommandEvent)4 EventListener (won.bot.framework.eventbot.listener.EventListener)4 WonRdfUtils (won.protocol.util.WonRdfUtils)4 Message (org.telegram.telegrambots.api.objects.Message)3 TelegramBotContextWrapper (won.bot.framework.bot.context.TelegramBotContextWrapper)3 BaseNeedAndConnectionSpecificEvent (won.bot.framework.eventbot.event.BaseNeedAndConnectionSpecificEvent)3