Beispiel #1
0
  @RequestMapping(value = "/{needId}/listConnections", method = RequestMethod.GET)
  public String listConnections(@PathVariable String needId, Model model) {

    List<Need> needs = needRepository.findById(Long.valueOf(needId));
    if (needs.isEmpty()) return "noNeedFound";
    Need need = needs.get(0);
    List<Connection> connections = connectionRepository.findByNeedURI(need.getNeedURI());
    model.addAttribute("connections", connections);

    // create an URI iterator from the matches and fetch the linked data descriptions for the needs.
    final Iterator<Connection> connectionIterator = connections.iterator();
    Iterator<Dataset> datasetIterator =
        WonLinkedDataUtils.getModelForURIs(
            new ProjectingIterator<Connection, URI>(connectionIterator) {
              @Override
              public URI next() {
                return connectionIterator.next().getRemoteNeedURI();
              }
            },
            this.linkedDataSource);
    Iterator<NeedPojo> needPojoIterator = WonOwnerWebappUtils.toNeedPojos(datasetIterator);

    // create a list of models and add all the descriptions:
    List<NeedPojo> remoteNeeds = new ArrayList<NeedPojo>(connections.size());
    while (connectionIterator.hasNext()) {
      remoteNeeds.add(needPojoIterator.next());
    }
    model.addAttribute("remoteNeeds", remoteNeeds);
    return "listConnections";
  }
  /**
   * Creates a new Connection object. Expects <> won:hasFacet [FACET] in the RDF content, will throw
   * exception if it's not there.
   *
   * @param needURI
   * @param otherNeedURI
   * @param otherConnectionURI
   * @param content
   * @param connectionState
   * @param connectionEventType
   * @return
   * @throws NoSuchNeedException
   * @throws IllegalMessageForNeedStateException
   * @throws ConnectionAlreadyExistsException
   */
  public Connection createConnection(
      final URI needURI,
      final URI otherNeedURI,
      final URI otherConnectionURI,
      final Model content,
      final ConnectionState connectionState,
      final ConnectionEventType connectionEventType)
      throws NoSuchNeedException, IllegalMessageForNeedStateException,
          ConnectionAlreadyExistsException {

    if (needURI == null) throw new IllegalArgumentException("needURI is not set");
    if (otherNeedURI == null) throw new IllegalArgumentException("otherNeedURI is not set");
    if (needURI.equals(otherNeedURI))
      throw new IllegalArgumentException("needURI and otherNeedURI are the same");

    // Load need (throws exception if not found)
    Need need = DataAccessUtils.loadNeed(needRepository, needURI);
    if (!isNeedActive(need))
      throw new IllegalMessageForNeedStateException(
          needURI, connectionEventType.name(), need.getState());

    URI facetURI = getFacet(content);
    if (facetURI == null)
      throw new IllegalArgumentException(
          "at least one RDF node must be of type won:" + WON.HAS_FACET.getLocalName());
    // TODO: create a proper exception if a facet is not supported by a need
    if (facetRepository.findByNeedURIAndTypeURI(needURI, facetURI).isEmpty())
      throw new RuntimeException("Facet is not supported by Need: " + facetURI);

    List<Connection> connections =
        connectionRepository.findByNeedURIAndRemoteNeedURI(needURI, otherNeedURI);
    Connection con = getConnection(connections, facetURI, connectionEventType);

    if (con == null) {
      /* Create connection */
      con = new Connection();
      con.setNeedURI(needURI);
      con.setState(connectionState);
      con.setRemoteNeedURI(otherNeedURI);
      con.setRemoteConnectionURI(otherConnectionURI);
      con.setTypeURI(facetURI);
      // save connection (this creates a new id)
      con = connectionRepository.saveAndFlush(con);
      // create and set new uri
      con.setConnectionURI(URIService.createConnectionURI(con));
      con = connectionRepository.saveAndFlush(con);

      // TODO: do we save the connection content? where? as a chat content?
    }

    return con;
  }
Beispiel #3
0
  @RequestMapping(value = "/{needId}", method = RequestMethod.GET)
  public String viewNeed(@PathVariable String needId, Model model) {

    model.addAttribute("needId", needId);

    List<Need> needs = needRepository.findById(Long.valueOf(needId));
    if (needs.isEmpty()) return "noNeedFound";

    Need need = needs.get(0);
    model.addAttribute("active", need.getState() != NeedState.ACTIVE ? "activate" : "deactivate");
    model.addAttribute("needURI", need.getNeedURI());
    List<Facet> facets = facetRepository.findByNeedURI(need.getNeedURI());

    NeedPojo needCommandPojo = new NeedPojo(facets);
    model.addAttribute("command", needCommandPojo);

    NeedPojo pojo =
        new NeedPojo(
            need.getNeedURI(),
            linkedDataSource.getDataForResource(need.getNeedURI()).getDefaultModel());
    pojo.setState(need.getState());
    model.addAttribute("pojo", pojo);

    // set facets on 'command': (needed for the dropdown list in the 'connect' control TODO:
    // deuglify
    needCommandPojo.setNeedFacetURIs(pojo.getNeedFacetURIs());

    return "viewNeed";
  }
Beispiel #4
0
 @RequestMapping(value = "/{needId}/toggle", method = RequestMethod.POST)
 public String toggleNeed(@PathVariable String needId, Model model)
     throws NoSuchConnectionFault, IllegalMessageForConnectionStateFault {
   List<Need> needs = needRepository.findById(Long.valueOf(needId));
   if (needs.isEmpty()) return "noNeedFound";
   Need need = needs.get(0);
   try {
     if (need.getState() == NeedState.ACTIVE) {
       ownerService.deactivate(need.getNeedURI(), null);
     } else {
       ownerService.activate(need.getNeedURI(), null);
     }
   } catch (NoSuchNeedException e) {
     logger.warn("caught NoSuchNeedException:", e);
   } catch (Exception e) {
     logger.warn("caught Exception", e);
   }
   return "redirect:/need/" + need.getId().toString();
   // return viewNeed(need.getId().toString(), model);
 }
Beispiel #5
0
  @RequestMapping(value = "/{needId}/connect", method = RequestMethod.POST)
  public String connect2Need(
      @PathVariable String needId, @ModelAttribute("SpringWeb") NeedPojo needPojo, Model model) {
    try {
      List<Need> needs = needRepository.findById(Long.valueOf(needId));
      if (needs.isEmpty()) return "noNeedFound";

      Need need1 = needs.get(0);

      com.hp.hpl.jena.rdf.model.Model facetModel =
          WonRdfUtils.FacetUtils.createFacetModelForHintOrConnect(
              URI.create(needPojo.getOwnFacetURI()), URI.create(needPojo.getRemoteFacetURI()));
      ownerService.connect(need1.getNeedURI(), new URI(needPojo.getNeedURI()), facetModel, null);
      return "redirect:/need/"
          + need1.getId().toString(); // viewNeed(need1.getId().toString(), model);
    } catch (URISyntaxException e) {
      logger.warn("caught URISyntaxException:", e);
    } catch (ConnectionAlreadyExistsException e) {
      logger.warn("caught ConnectionAlreadyExistsException:", e);
    } catch (IllegalMessageForNeedStateException e) {
      logger.warn("caught IllegalMessageForNeedStateException:", e);
    } catch (NoSuchNeedException e) {
      logger.warn("caught NoSuchNeedException:", e);
    } catch (InterruptedException e) {
      logger.warn("caught InterruptedException", e);
    } catch (ExecutionException e) {
      logger.warn("caught ExcutionException", e);
    } catch (CamelConfigurationFailedException e) {
      logger.warn("caught CameConfigurationException", e);
      logger.warn("caught CamelConfigurationFailedException", e);
    } catch (Exception e) {
      logger.warn("caught Exception", e);
    }

    return "noNeedFound";
  }
Beispiel #6
0
 @RequestMapping(value = "/import", method = RequestMethod.POST)
 public String importNeedPost(@RequestParam("needURI") URI needURI) {
   Need importedNeed = dataReloadService.importNeed(needURI);
   return "redirect:/need/" + importedNeed.getId();
 }
 private boolean isNeedActive(final Need need) {
   return NeedState.ACTIVE == need.getState();
 }