/**
   * @param firstAction The first Action in the session's action chain
   *     <p>return Returns the KickstartSession.
   */
  protected KickstartSession setupKickstartSession(Action firstAction) {
    kickstartSession = new KickstartSession();
    Boolean deployConfig = this.getKsdata().getKickstartDefaults().getCfgManagementFlag();

    // TODO: Proxy logic

    // Setup the KickstartSession
    kickstartSession.setPackageFetchCount(new Long(0));
    kickstartSession.setKickstartMode(KickstartSession.MODE_ONETIME);
    kickstartSession.setDeployConfigs(deployConfig);
    kickstartSession.setAction(firstAction);
    kickstartSession.setKsdata(this.getKsdata());
    kickstartSession.setKstree(this.getKsdata().getTree());
    kickstartSession.setVirtualizationType(
        this.getKsdata().getKickstartDefaults().getVirtualizationType());
    kickstartSession.setLastAction(new Date());
    kickstartSession.setNewServer(this.getTargetServer());
    kickstartSession.setOldServer(this.getTargetServer());
    kickstartSession.setHostServer(this.getHostServer());
    kickstartSession.setUser(this.getUser());
    kickstartSession.setState(KickstartFactory.SESSION_STATE_CREATED);
    kickstartSession.setOrg(this.getUser().getOrg());
    kickstartSession.setSystemRhnHost(this.getProxyHost());
    kickstartSession.setVirtualizationType(
        this.getKsdata().getKickstartDefaults().getVirtualizationType());
    log.debug("** Saving new KickstartSession: " + kickstartSession.getId());
    KickstartFactory.saveKickstartSession(kickstartSession);
    log.debug("** Saved new KickstartSession: " + kickstartSession.getId());

    return kickstartSession;
  }
  public void testLookupBySession() throws Exception {
    // Still have that weird error creating a test server
    // sometimes in hosted.
    ActivationKey k = createTestActivationKey(user);
    KickstartData ksdata = KickstartDataTest.createKickstartWithOptions(k.getOrg());
    KickstartFactory.saveKickstartData(ksdata);
    KickstartSession sess = KickstartSessionTest.createKickstartSession(ksdata, k.getCreator());
    KickstartFactory.saveKickstartSession(sess);
    k.setKickstartSession(sess);
    ActivationKeyFactory.save(k);
    k = (ActivationKey) reload(k);

    ActivationKey lookedUp = ActivationKeyFactory.lookupByKickstartSession(sess);
    assertNotNull(lookedUp);
  }
  public void testKsSessionPathparse() throws Exception {
    user.addPermanentRole(RoleFactory.ORG_ADMIN);
    KickstartSession session = KickstartSessionTest.createKickstartSession(ksdata, user);
    KickstartFactory.saveKickstartSession(session);
    session = (KickstartSession) reload(session);
    assertNotSame(session.getState(), KickstartFactory.SESSION_STATE_CONFIG_ACCESSED);

    String encodedSession = SessionSwap.encodeData(session.getId().toString());
    // URL: /kickstart/ks/session/2xb7d56e8958b0425e762cc74e8705d8e7
    String url = "http://rhn.redhat.com/session/ks/session/" + encodedSession;
    request.setAttribute(RequestContext.REQUESTED_URI, url);
    Map<String, Object> options = helper.parseKickstartUrl(url);
    assertNotNull(options);
    assertNotNull(options.get("org_id"));
    assertNotNull(options.get("ksdata"));
    assertNotNull(options.get("session"));
    assertNotNull(options.get("host"));
    assertEquals(session.getState(), KickstartFactory.SESSION_STATE_CONFIG_ACCESSED);
  }
  private Profile processProfileType(String profileTypeIn) {
    log.debug("PROFILE_TYPE=" + profileTypeIn);

    if (profileTypeIn == null
        || profileTypeIn.length() == 0
        || // TODO: fix this hack
        profileTypeIn.equals(TARGET_PROFILE_TYPE_NONE)) {
      return null;
    }
    Profile retval = null;
    // Profile of this existing system's packages
    String pname =
        LocalizationService.getInstance()
            .getMessage("kickstart.session.newprofile", this.kickstartSession.getId().toString());
    if (profileTypeIn.equals(TARGET_PROFILE_TYPE_EXISTING)) {
      log.debug("    TARGET_PROFILE_TYPE_EXISTING");
      // "Profile for kickstart session "
      retval =
          ProfileManager.createProfile(
              ProfileFactory.TYPE_SYNC_PROFILE,
              this.user,
              getTargetServer().getBaseChannel(),
              pname,
              pname);
      ProfileManager.copyFrom(this.server, retval);
    }
    // Profile of 'stored profile'
    else if (profileTypeIn.equals(TARGET_PROFILE_TYPE_PACKAGE)) {
      log.debug("    TARGET_PROFILE_TYPE_PACKAGE");
      if (this.profileId == null) {
        throw new UnsupportedOperationException(
            "You specified a target profile type"
                + TARGET_PROFILE_TYPE_PACKAGE
                + " but this.profileId is null");
      }
      retval = ProfileManager.lookupByIdAndOrg(this.profileId, this.user.getOrg());
    }
    // Some other system's profile
    else if (profileTypeIn.equals(TARGET_PROFILE_TYPE_SYSTEM)) {
      Server otherServer = ServerFactory.lookupById(this.serverProfileId);
      log.debug("    TARGET_PROFILE_TYPE_SYSTEM");
      log.debug("    this.serverProfileId      : " + this.serverProfileId);
      log.debug("    otherServer               : " + otherServer);
      if (otherServer != null) {
        log.debug("otherServer.Id            : " + otherServer.getId());
        log.debug("otherServer.getBaseChannel: " + otherServer.getBaseChannel());
      }

      retval =
          ProfileManager.createProfile(
              ProfileFactory.TYPE_SYNC_PROFILE,
              this.user,
              otherServer.getBaseChannel(),
              pname,
              pname);
      ProfileManager.copyFrom(otherServer, retval);
    }
    this.kickstartSession.setServerProfile(retval);
    KickstartFactory.saveKickstartSession(this.kickstartSession);

    if (getTargetServer() != null) {
      HibernateFactory.getSession().refresh(getTargetServer());
    }

    // TODO: Compute missing packages and forward user to the missing page

    return retval;
  }