    public void migrate()
        throws CvqException {
        for (DTO dto : (List<DTO>)HibernateUtil.getSession().createSQLQuery(
            "select r.requester_id as requesterId, r.home_folder_id as homeFolderId, r.creation_date as date, i.id as individualId from individual i, request r, history_entry he where r.id = he.request_id and i.id = he.object_id and property = 'homeFolder' and old_value is not null and new_value is null")
                .addScalar("requesterId").addScalar("homeFolderId").addScalar("date")
                .addScalar("individualId").setResultTransformer(Transformers.aliasToBean(DTO.class))
                .list()) {
            Individual i = userSearchService.getById(dto.individualId);
            HomeFolder homeFolder = userSearchService.getHomeFolderById(dto.homeFolderId);
            i.setHomeFolder(homeFolder);
            homeFolder.getIndividuals().add(i);
            i.setState(UserState.ARCHIVED);
            archived.add(i.getId());
            RequestAction fake = new RequestAction();
            fake.setDate(dto.date);
            fake.setAgentId(dto.requesterId);
            JsonObject payload = new JsonObject();
            payload.addProperty("state", UserState.ARCHIVED.toString());
            add(homeFolder, new UserAction(UserAction.Type.STATE_CHANGE, dto.individualId, payload), fake);
        }
        for (HomeFolder homeFolder : customDAO.all(HomeFolder.class)) {
            Set<Critere> criterias = new HashSet<Critere>();
            criterias.add(new Critere(Request.SEARCH_BY_HOME_FOLDER_ID, homeFolder.getId(), Critere.EQUALS));
            List<Request> requests = requestSearchService.get(criterias, null, null, 0, 0, true);
            Request first = requests.get(requests.size() - 1);
            if (homeFolder.isTemporary()) {
                List<RequestAction> actions = new ArrayList<RequestAction>(first.getActions());
                Collections.reverse(actions);
                for (RequestAction requestAction : actions) {
                    convert(requestAction);
                }
            } else {
                for (RequestAction requestAction :
                    (List<RequestAction>)HibernateUtil.getSession().createSQLQuery(
                        "select * from request_action where request_id in (select id from request where home_folder_id = :homeFolderId and specific_data_class='fr.cg95.cvq.business.request.ecitizen.HomeFolderModificationRequestData') or request_id = :firstId order by date asc")
                        .addEntity(RequestAction.class).setLong("homeFolderId", homeFolder.getId())
                        .setLong("firstId", first.getId()).list()) {
                    convert(requestAction);
                }
            }
        }
        HibernateUtil.getSession().flush();
        for (HomeFolder homeFolder : customDAO.all(HomeFolder.class)) {
            if (!customDAO.hasCreationAction(homeFolder.getId()))
                createFakeCreationAction(homeFolder, homeFolder.getId());
            for (Individual i : homeFolder.getIndividuals())
                if (!customDAO.hasCreationAction(i.getId()))
                    createFakeCreationAction(homeFolder, i.getId());
        }
        HibernateUtil.getSession().flush();
        for (Map.Entry<Long, Date> creationNotification : creationNotifications.entrySet()) {
            for (Long id : creationActions.get(creationNotification.getKey())) {
                UserAction action = (UserAction) customDAO.findById(UserAction.class, id);
                JsonObject payload = JSONUtils.deserialize(action.getData());
                payload.addProperty("notificationDate", creationNotification.getValue().toString());
                action.setData(new Gson().toJson(payload));
                customDAO.update(action);
            }
        }
        for (Long id : archived) {
            states.put(id, UserState.ARCHIVED);
        }
        for (Map.Entry<Long, UserState> state : states.entrySet()) {
            try {
                Individual i = userSearchService.getById(state.getKey());
                i.setState(state.getValue());
                customDAO.update(i);
            } catch (CvqObjectNotFoundException e) {
                HomeFolder homeFolder = userSearchService.getHomeFolderById(state.getKey());
                homeFolder.setState(state.getValue());
                customDAO.update(homeFolder);
            }
        }
        for (Adult external : customDAO.findBySimpleProperty(Adult.class, "homeFolder", null)) {
            HomeFolder homeFolder = null;
            for (IndividualRole role : (List<IndividualRole>) HibernateUtil.getSession()
                .createSQLQuery("select * from individual_role where owner_id = :id")
                .addEntity(IndividualRole.class).setLong("id", external.getId()).list()) {
                if (role.getHomeFolderId() != null) {
                    try {
                        homeFolder = userSearchService.getHomeFolderById(role.getHomeFolderId());
                    } catch (CvqObjectNotFoundException e) {
                    }
                } else if (role.getIndividualId() != null) {
                    try {
                        homeFolder = userSearchService.getById(role.getIndividualId()).getHomeFolder();
                    } catch (CvqObjectNotFoundException e) {
                    }
                }
                if (homeFolder != null) {
                    createFakeCreationAction(homeFolder, external.getId());
                    break;
                }
            }
        }
    }

    public void migrate()
        throws CvqException {
        for (DTO dto : (List<DTO>)HibernateUtil.getSession().createSQLQuery(
            "select r.requester_id as requesterId, r.home_folder_id as homeFolderId, r.creation_date as date, i.id as individualId from individual i, request r, history_entry he where r.id = he.request_id and i.id = he.object_id and property = 'homeFolder' and old_value is not null and new_value is null")
                .addScalar("requesterId").addScalar("homeFolderId").addScalar("date")
                .addScalar("individualId").setResultTransformer(Transformers.aliasToBean(DTO.class))
                .list()) {
            Individual i = userSearchService.getById(dto.individualId);
            HomeFolder homeFolder = userSearchService.getHomeFolderById(dto.homeFolderId);
            i.setHomeFolder(homeFolder);
            homeFolder.getIndividuals().add(i);
            i.setState(UserState.ARCHIVED);
            archived.add(i.getId());
            RequestAction fake = new RequestAction();
            fake.setDate(dto.date);
            fake.setAgentId(dto.requesterId);
            JsonObject payload = new JsonObject();
            payload.addProperty("state", UserState.ARCHIVED.toString());
            add(homeFolder, new UserAction(UserAction.Type.STATE_CHANGE, dto.individualId, payload), fake);
        }
        for (HomeFolder homeFolder : customDAO.all(HomeFolder.class)) {
            Set<Critere> criterias = new HashSet<Critere>();
            criterias.add(new Critere(Request.SEARCH_BY_HOME_FOLDER_ID, homeFolder.getId(), Critere.EQUALS));
            List<Request> requests = requestSearchService.get(criterias, Request.SEARCH_BY_CREATION_DATE, null, 0, 0, true);
            if (requests.isEmpty()) continue;
            Request first = requests.get(0);
            if (homeFolder.isTemporary()) {
                List<RequestAction> actions = new ArrayList<RequestAction>(first.getActions());
                Collections.reverse(actions);
                for (RequestAction requestAction : actions) {
                    convert(requestAction);
                }
            } else {
                for (RequestAction requestAction :
                    (List<RequestAction>)HibernateUtil.getSession().createSQLQuery(
                        "select * from request_action where request_id in (select id from request where home_folder_id = :homeFolderId and specific_data_class='fr.cg95.cvq.business.request.ecitizen.HomeFolderModificationRequestData') or request_id = :firstId order by date asc")
                        .addEntity(RequestAction.class).setLong("homeFolderId", homeFolder.getId())
                        .setLong("firstId", first.getId()).list()) {
                    convert(requestAction);
                }
            }
        }
        HibernateUtil.getSession().flush();
        for (HomeFolder homeFolder : customDAO.all(HomeFolder.class)) {
            if (!customDAO.hasCreationAction(homeFolder.getId()))
                createFakeCreationAction(homeFolder, homeFolder.getId());
            for (Individual i : homeFolder.getIndividuals())
                if (!customDAO.hasCreationAction(i.getId()))
                    createFakeCreationAction(homeFolder, i.getId());
        }
        HibernateUtil.getSession().flush();
        for (Map.Entry<Long, Date> creationNotification : creationNotifications.entrySet()) {
            for (Long id : creationActions.get(creationNotification.getKey())) {
                UserAction action = (UserAction) customDAO.findById(UserAction.class, id);
                JsonObject payload = JSONUtils.deserialize(action.getData());
                payload.addProperty("notificationDate", creationNotification.getValue().toString());
                action.setData(new Gson().toJson(payload));
                customDAO.update(action);
            }
        }
        for (Long id : archived) {
            states.put(id, UserState.ARCHIVED);
        }
        for (Map.Entry<Long, UserState> state : states.entrySet()) {
            try {
                Individual i = userSearchService.getById(state.getKey());
                i.setState(state.getValue());
                customDAO.update(i);
            } catch (CvqObjectNotFoundException e) {
                HomeFolder homeFolder = userSearchService.getHomeFolderById(state.getKey());
                homeFolder.setState(state.getValue());
                customDAO.update(homeFolder);
            }
        }
        for (Adult external : customDAO.findBySimpleProperty(Adult.class, "homeFolder", null)) {
            HomeFolder homeFolder = null;
            for (IndividualRole role : (List<IndividualRole>) HibernateUtil.getSession()
                .createSQLQuery("select * from individual_role where owner_id = :id")
                .addEntity(IndividualRole.class).setLong("id", external.getId()).list()) {
                if (role.getHomeFolderId() != null) {
                    try {
                        homeFolder = userSearchService.getHomeFolderById(role.getHomeFolderId());
                    } catch (CvqObjectNotFoundException e) {
                    }
                } else if (role.getIndividualId() != null) {
                    try {
                        homeFolder = userSearchService.getById(role.getIndividualId()).getHomeFolder();
                    } catch (CvqObjectNotFoundException e) {
                    }
                }
                if (homeFolder != null) {
                    createFakeCreationAction(homeFolder, external.getId());
                    break;
                }
            }
        }
    }
