profile.setValue(EndEntityProfile.DEFAULTCA,0,Integer.toString(caId)); profile.setRequired(EndEntityProfile.DEFAULTCA,0,true); profile.setValue(EndEntityProfile.AVAILCAS,0,Integer.toString(caId)); profile.setRequired(EndEntityProfile.AVAILCAS,0,true);
profile.upgrade();
    public boolean upgrade(Admin admin, String[] args) {        debug(">upgrade("+admin.toString()+")");        String dbtype = null;        if (args.length > 0) {            dbtype = args[0];            debug("Database type="+dbtype);        }        if (args.length > 1) {        	dataSource = args[1];        	debug("Datasource="+dataSource);        }        String caName = "MyCA";        if (args.length > 2) {            caName = args[2];            debug("CaName="+caName);        }        String keyStore = null;        if (args.length > 3) {            keyStore = args[3];            debug("KeyStore="+keyStore);        }        String pwd = null;        if (args.length > 4) {            pwd = args[4];            debug("Pwd="+pwd);        }        if (!preCheck()) {        	info("preCheck failed, no upgrade performed.");            return false;        }        // Read old keystore file in the beginning so we know it's good        byte[] keystorebytes = null;        try {            keystorebytes = FileTools.readFiletoBuffer(keyStore);        } catch (IOException ioe) {            error("IOException reading old keystore file: ", ioe);            return false;        }        info("Starting upgrade from ejbca2 to ejbca3.");        // Fetch the resource file with SQL to modify the database tables        InputStream in = this.getClass().getResourceAsStream("/upgrade/21_30/21_30-upgrade-"+dbtype+".sql");        if (in == null) {        	error("Can not read resource for database type '"+dbtype+"'");        	return false;        }        // TODO: export and import profiles?        // TODO: export and import key recovery keys?                // Migrate database tables to new columns etc        Connection con = null;        info("Start migration of database.");        try {            InputStreamReader inreader = new InputStreamReader(in);            con = getConnection();            SqlExecutor sqlex = new SqlExecutor(con, false);             sqlex.runCommands(inreader);        } catch (NamingException e) {            error("Error during database migration: ", e);            return false;        } catch (SQLException e) {            error("SQL error during database migration: ", e);            return false;        } catch (IOException e) {            error("IO error during database migration: ", e);            return false;        } finally {            JDBCUtil.close(con);        }        info("Finished migrating database.");        // Import CA from PKCS12 file        String privKeyAlias = "privateKey";        getCaAdminSession().upgradeFromOldCAKeyStore(admin, caName, keystorebytes, pwd.toCharArray(), pwd.toCharArray(), privKeyAlias);        // Change fields, i.e. CAId in database tables        CAInfo cainfo = getCaAdminSession().getCAInfo(admin, caName);        int caId = cainfo.getCAId();                // Fix all End Entity Profiles        HashMap profileidtonamemap = getRaAdminSession().getEndEntityProfileIdToNameMap(admin);        Iterator iter = profileidtonamemap.keySet().iterator();        while(iter.hasNext()){        	int next = ((Integer) iter.next()).intValue();        	// Only upgrade nonfixed profiles.        	if(next > SecConst.EMPTY_ENDENTITYPROFILE){        		EndEntityProfile profile = getRaAdminSession().getEndEntityProfile(admin,next);           		profile.setValue(EndEntityProfile.DEFAULTCA,0,Integer.toString(caId));        		profile.setRequired(EndEntityProfile.DEFAULTCA,0,true);        		profile.setValue(EndEntityProfile.AVAILCAS,0,Integer.toString(caId));        		profile.setRequired(EndEntityProfile.AVAILCAS,0,true);        		getRaAdminSession().changeEndEntityProfile(admin,(String) profileidtonamemap.get(new Integer(next)),profile);        	}        }                PreparedStatement ps1 = null;        PreparedStatement ps2 = null;        PreparedStatement ps3 = null;        try {            con = getConnection();            ps1 = con.prepareStatement("update admingroupdata set caId=?");            ps1.setInt(1, caId);            ps1.executeUpdate();                        ps2 = con.prepareStatement("update logentrydata set caId=?");            ps2.setInt(1, caId);            ps2.executeUpdate();            ps3 = con.prepareStatement("update userdata set caId=?");            ps3.setInt(1, caId);            ps3.executeUpdate();        } catch (SQLException sqle) {            error("Error updating caId: ", sqle);            return false;        } catch (NamingException ne) {            error("Error getting connection: ", ne);            return false;        } finally {            JDBCUtil.close(ps1);            JDBCUtil.close(ps2);            JDBCUtil.close(ps3);            JDBCUtil.close(con);        }        try {            getAuthorizationSession().initialize(admin, caId);        } catch (AdminGroupExistsException e) {            error("Error initializing admin group: ", e);            return false;        }        debug("<upgrade()");        return true;    }