Exemplo n.º 1
0
  public static ClientUser buildUserFromProfile(NativeDocument profile, String username) {
    NativeElement root = profile.getDocumentElement();
    ClientUser currentUser = new ClientUser();
    currentUser.setUsername(username);

    // backwards compatibility for the moment...
    if (root.getElementsByTagName("creds").getLength() != 0) {
      Debug.println("Profile detected Old version...");
      currentUser.setFirstName(root.getElementByTagName("first").getTextContent());
      currentUser.setLastName(root.getElementByTagName("last").getTextContent());
      currentUser.setAffiliation(root.getElementByTagName("affiliation").getTextContent());
    } else {
      Debug.println("Profile detected New shiny version!");
      NativeNodeList nodes = root.getChildNodes();
      for (int i = 0; i < nodes.getLength(); i++) {
        NativeNode curNode = nodes.item(i);
        if (curNode.getNodeType() != NativeNode.ELEMENT_NODE) continue;
        String name = curNode.getNodeName();
        if (name.equals(NewAccountPanel.EMAIL_KEY)) currentUser.setEmail(curNode.getTextContent());
        else if (name.equals(NewAccountPanel.FIRSTNAME_KEY))
          currentUser.setFirstName(curNode.getTextContent());
        else if (name.equals(NewAccountPanel.LASTNAME_KEY))
          currentUser.setLastName(curNode.getTextContent());
        else if (name.equals(NewAccountPanel.AFFILIATION_KEY))
          currentUser.setAffiliation(curNode.getTextContent());
        else currentUser.setProperty(name, curNode.getTextContent());
      }
      if (root.getElementByTagName("quickGroup") == null)
        currentUser.setProperty("quickGroup", "rlu");
    }

    return currentUser;
  }
  /**
   * Assumes that: - Users have already been converted - Working Sets have already been converted -
   * Working Set IDs have remained the same.
   */
  protected void run() throws Exception {
    userIO = new UserIO(session);
    relationshipIO = new RelationshipIO(session);
    taxonIO = new TaxonIO(session);

    if (isTestMode()) print("### RUNNING IN TEST MODE ###");

    final WorkingSetIO wsIO = new WorkingSetIO(session);

    for (VFSPathToken token : data.getOldVFS().list(new VFSPath("/users"))) {
      final User user = userIO.getUserFromUsername(token.toString());
      if (user == null) printf("## No user exists for data directory %s ==", token);
      else {
        Hibernate.initialize(user.getSubscribedWorkingSets());

        final VFSPath workingSetPath =
            new VFSPath("/users/" + user.getUsername() + "/workingSet.xml");
        final NativeDocument document = new JavaNativeDocument();
        try {
          document.parse(data.getOldVFS().getString(workingSetPath));
        } catch (Exception e) {
          continue;
        }

        printf("== Finding working sets for %s (%s) ==", user.getUsername(), user.getId());

        final NativeNodeList nodes = document.getDocumentElement().getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
          NativeNode node = nodes.item(i);
          if ("public".equals(node.getNodeName())) {
            NativeNodeList children = node.getChildNodes();
            for (int k = 0; k < children.getLength(); k++) {
              NativeNode child = children.item(k);
              if ("workingSet".equals(child.getNodeName())) {
                NativeElement el = (NativeElement) child;
                String id = el.getAttribute("id");
                String creator = el.getAttribute("creator");

                if (!user.getUsername().equals(creator)) {
                  boolean success = wsIO.subscribeToWorkingSet(Integer.valueOf(id), user);
                  if (!success) printf("# Failed to subscribe to ws %s", id);
                  else {
                    printf("Subscribed to ws %s", id);
                    /*if (count.incrementAndGet() % 50 == 0) {
                    	printf("Converted %s working set subscriptions...", count.get());
                    	commitAndStartTransaction();
                    }*/
                  }
                }
              }
            }
          } else if ("private".equals(node.getNodeName())) {
            NativeNodeList children = node.getChildNodes();
            for (int k = 0; k < children.getLength(); k++) {
              NativeNode child = children.item(k);
              if ("workingSet".equals(child.getNodeName())) {
                WorkingSetData data =
                    new WorkingSetParser().parseSingleWorkingSet((NativeElement) node);
                if ("".equals(data.getId())) {
                  continue;
                }
                WorkingSet privateWS =
                    WorkingSetConverter.convertWorkingSetData(
                        data, session, relationshipIO, userIO, taxonIO, user);
                if (privateWS != null) {
                  printf("Created private working set %s", privateWS.getName());
                  session.save(privateWS);
                }
              }
            }
          }
        }
      }
      commitAndStartTransaction();
    }

    // commitAndStartTransaction();
  }