/**
   * creates a social entity
   *
   * @param resource resource to bind with new social entity
   * @param user user owner of the resource
   * @return the id of the new entity
   * @throws WebApiException exception thrown by social engine
   */
  public Long createEntity(Resource resource, User user) throws WebApiException {
    Entity entity =
        SemanticHelper.createEntity(
            socialClient,
            Long.valueOf(user.getSocialId()),
            "computer file",
            resource.getName(),
            resource.getId(),
            null,
            null);

    return entity.getId();
  }
 private static long createUser(SCWebApiClient client, EntityBase eb) throws WebApiException {
   System.out.println("Creating an entity...");
   EntityType person = client.readEntityType("person", eb.getKbLabel());
   Entity entity = new Entity();
   entity.setEntityBase(eb);
   entity.setEtype(person);
   Long eid = client.create(entity);
   System.out.println("Created entity with ID " + eid);
   System.out.println("Creating a user...");
   User user = new User();
   user.setName("Test user " + System.currentTimeMillis());
   user.setEntityBaseId(eb.getId());
   user.setPersonEntityId(eid);
   long id = client.create(user);
   System.out.println("New user's ID: " + id);
   return id;
 }
 /**
  * checks if social entity is owned by the given user
  *
  * @param user user owner of entity
  * @param entityId social entity id
  * @return true if entity is owned by the user, false otherwise
  */
 public boolean isOwnedBy(User user, String entityId) {
   try {
     Entity entity = socialClient.readEntity(Long.decode(entityId), null);
     it.unitn.disi.sweb.webapi.model.smartcampus.social.User socialUser =
         socialClient.readUser(Long.valueOf(user.getSocialId()));
     return entity.getOwnerId().equals(socialUser.getEntityBaseId());
   } catch (NumberFormatException e) {
     logger.error(String.format("Exception converting in long entityId %s", entityId));
     return false;
   } catch (WebApiException e) {
     logger.error("Exception invoking socialEngineClient", e);
     return false;
   } catch (Exception e) {
     logger.error("A general exception is occurred", e);
     return false;
   }
 }
  public static void main(String[] args) throws WebApiException, JSONException {
    SCWebApiClient client =
        SCWebApiClient.getInstance(Locale.ENGLISH, "sweb.smartcampuslab.it", 8080);
    // SCWebApiClient client = SCWebApiClient.getInstance(Locale.ENGLISH,
    // "127.0.0.1", 8080);
    if (!client.ping()) {
      throw new RuntimeException("Ping failed");
    }
    // user 1 and its EB
    System.out.println("Creating an entity base 1...");
    EntityBase eb1 = new EntityBase();
    eb1.setLabel("TEST_SC_EB_1_" + System.currentTimeMillis());
    Long ebId1 = client.create(eb1);
    // Re-read to get the ID of the default KB
    eb1 = client.readEntityBase(ebId1);
    System.out.println("Created an entity base 1 " + eb1.getLabel() + " with ID " + ebId1);
    long uid1 = createUser(client, eb1);

    // user 2 and its EB
    System.out.println("Creating an entity base 2...");
    EntityBase eb2 = new EntityBase();
    eb2.setLabel("TEST_SC_EB_2_" + System.currentTimeMillis());
    Long ebId2 = client.create(eb2);
    // Re-read to get the ID of the default KB
    eb2 = client.readEntityBase(ebId2);
    System.out.println("Created an entity base 1 " + eb2.getLabel() + " with ID " + ebId2);
    long uid2 = createUser(client, eb2);

    // compute shared entities with user 2
    LiveTopic filter = new LiveTopic();
    LiveTopicSource filterSource = new LiveTopicSource();
    filter.setActorId(uid2); // <-- mandatory
    filterSource.setUserIds(new HashSet<Long>(Arrays.asList(new Long[] {uid1})));
    filter.setSource(filterSource);
    LiveTopicContentType type = new LiveTopicContentType();
    type.setAllTypes(true);
    filter.setType(type); // <-- mandatory
    filter.setStatus(LiveTopicStatus.ACTIVE); // <-- mandatory
    LiveTopicSubject lts = new LiveTopicSubject();
    lts.setAllSubjects(true);
    Set<LiveTopicSubject> subjects = new HashSet<LiveTopicSubject>();
    subjects.add(lts);
    filter.setSubjects(subjects);

    List<Long> sharedId = client.computeEntitiesForLiveTopic(filter, null, null);

    System.out.println(
        "Shared entities IDs (should be empty even if user 1 has an entity): " + sharedId);

    // entity of user 1
    EntityType et = client.readEntityType("entity", eb1.getKbLabel());
    Entity entity = new Entity();
    entity.setEntityBase(eb1);
    entity.setEtype(et);

    long entityId = client.create(entity);

    // share entity with user 2
    LiveTopicSource assignments = client.readAssignments(entityId, Operation.READ, uid1);
    if (!assignments.getUserIds().contains(uid2)) {
      assignments.getUserIds().add(uid2);
    }
    client.updateAssignments(entityId, Operation.READ, uid1, assignments);

    // compute shared entities
    sharedId = client.computeEntitiesForLiveTopic(filter, null, null);

    System.out.println("Shared entities IDs (should be one shared entity): " + sharedId);

    // check for public entities
    filter.setActorId(null);
    sharedId = client.computeEntitiesForLiveTopic(filter, null, null);
    System.out.println(
        "Shared entities IDs (should be empty as there are no public entities): " + sharedId);

    System.out.println("Public sharing");
  }