org.openmeetings.servlet.outputhandler.BackupExport.java Source code

Java tutorial

Introduction

Here is the source code for org.openmeetings.servlet.outputhandler.BackupExport.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License") +  you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.openmeetings.servlet.outputhandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.openmeetings.app.OpenmeetingsVariables;
import org.openmeetings.app.data.basic.AuthLevelmanagement;
import org.openmeetings.app.data.basic.Configurationmanagement;
import org.openmeetings.app.data.basic.Sessionmanagement;
import org.openmeetings.app.data.basic.dao.LdapConfigDaoImpl;
import org.openmeetings.app.data.calendar.daos.AppointmentDaoImpl;
import org.openmeetings.app.data.calendar.daos.MeetingMemberDaoImpl;
import org.openmeetings.app.data.conference.PollManagement;
import org.openmeetings.app.data.conference.Roommanagement;
import org.openmeetings.app.data.conference.dao.RoomModeratorsDaoImpl;
import org.openmeetings.app.data.file.dao.FileExplorerItemDaoImpl;
import org.openmeetings.app.data.flvrecord.FlvRecordingDaoImpl;
import org.openmeetings.app.data.flvrecord.FlvRecordingMetaDataDaoImpl;
import org.openmeetings.app.data.user.Organisationmanagement;
import org.openmeetings.app.data.user.Usermanagement;
import org.openmeetings.app.data.user.dao.PrivateMessageFolderDaoImpl;
import org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl;
import org.openmeetings.app.data.user.dao.UserContactsDaoImpl;
import org.openmeetings.app.data.user.dao.UsersDaoImpl;
import org.openmeetings.app.persistence.beans.basic.Configuration;
import org.openmeetings.app.persistence.beans.basic.LdapConfig;
import org.openmeetings.app.persistence.beans.calendar.Appointment;
import org.openmeetings.app.persistence.beans.calendar.MeetingMember;
import org.openmeetings.app.persistence.beans.domain.Organisation;
import org.openmeetings.app.persistence.beans.domain.Organisation_Users;
import org.openmeetings.app.persistence.beans.files.FileExplorerItem;
import org.openmeetings.app.persistence.beans.flvrecord.FlvRecording;
import org.openmeetings.app.persistence.beans.flvrecord.FlvRecordingMetaData;
import org.openmeetings.app.persistence.beans.poll.RoomPoll;
import org.openmeetings.app.persistence.beans.poll.RoomPollAnswers;
import org.openmeetings.app.persistence.beans.rooms.RoomModerators;
import org.openmeetings.app.persistence.beans.rooms.Rooms;
import org.openmeetings.app.persistence.beans.rooms.Rooms_Organisation;
import org.openmeetings.app.persistence.beans.sip.asterisk.AsteriskSipUsers;
import org.openmeetings.app.persistence.beans.sip.asterisk.Extensions;
import org.openmeetings.app.persistence.beans.sip.asterisk.MeetMe;
import org.openmeetings.app.persistence.beans.user.PrivateMessageFolder;
import org.openmeetings.app.persistence.beans.user.PrivateMessages;
import org.openmeetings.app.persistence.beans.user.UserContacts;
import org.openmeetings.app.persistence.beans.user.Users;
import org.openmeetings.app.sip.api.impl.asterisk.dao.AsteriskDAOImpl;
import org.openmeetings.utils.math.CalendarPatterns;
import org.red5.logging.Red5LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * @author sebastianwagner
 * 
 */
public class BackupExport {

    private static final Logger log = Red5LoggerFactory.getLogger(BackupExport.class,
            OpenmeetingsVariables.webAppRootKey);
    private static final String BACKUP_COMMENT = "###############################################\n"
            + "This File is auto-generated by the Backup Tool \n"
            + "you should use the BackupPanel to modify or change this file \n"
            + "see http://incubator.apache.org/openmeetings/Upgrade.html for Details \n"
            + "###############################################";

    @Autowired
    private AppointmentDaoImpl appointmentDao;
    @Autowired
    private Sessionmanagement sessionManagement;
    @Autowired
    private Usermanagement userManagement;
    @Autowired
    private Organisationmanagement organisationmanagement;
    @Autowired
    private Roommanagement roommanagement;
    @Autowired
    private FileExplorerItemDaoImpl fileExplorerItemDao;
    @Autowired
    private FlvRecordingDaoImpl flvRecordingDao;
    @Autowired
    private FlvRecordingMetaDataDaoImpl flvRecordingMetaDataDao;
    @Autowired
    private RoomModeratorsDaoImpl roomModeratorsDao;
    @Autowired
    private UsersDaoImpl usersDao;
    @Autowired
    private MeetingMemberDaoImpl meetingMemberDao;
    @Autowired
    private LdapConfigDaoImpl ldapConfigDao;
    @Autowired
    private PrivateMessagesDaoImpl privateMessagesDao;
    @Autowired
    private PrivateMessageFolderDaoImpl privateMessageFolderDao;
    @Autowired
    private UserContactsDaoImpl userContactsDao;
    @Autowired
    private AuthLevelmanagement authLevelManagement;
    @Autowired
    private PollManagement pollManagement;
    @Autowired
    private Configurationmanagement cfgManagement;
    @Autowired
    private AsteriskDAOImpl asteriskDAOImpl;

    public void performExport(String filePath, File backup_dir, boolean includeFiles, String omFilesDir)
            throws Exception {

        if (!backup_dir.exists()) {
            backup_dir.mkdirs();
        }

        /*
         * ##################### Backup Organizations
         */
        List<Organisation> orgList = organisationmanagement.getOrganisations(3L);

        if (orgList != null) {
            Document doc = this.createOrgDocument(orgList);

            File orgListXML = new File(backup_dir, "organizations.xml");

            FileOutputStream fos = new FileOutputStream(orgListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Backup Users
         */

        List<Users> uList = usersDao.getAllUsersDeleted();

        if (uList != null) {
            log.debug("Number of Users to be deleted " + uList.size());

            Document doc = this.createDocument(uList);

            File userListXML = new File(backup_dir, "users.xml");

            FileOutputStream fos = new FileOutputStream(userListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Backup Room
         */
        List<Rooms> roomList = roommanagement.getBackupRooms();

        if (roomList != null) {
            Document doc = this.createRoomsDocument(roomList);

            File roomListXML = new File(backup_dir, "rooms.xml");

            FileOutputStream fos = new FileOutputStream(roomListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Backup Room Organizations
         */
        List<Rooms_Organisation> roomOrgList = roommanagement.getRoomsOrganisations();

        if (roomOrgList != null) {
            Document doc = this.createOrgRoomsDocument(roomOrgList);

            File roomListXML = new File(backup_dir, "rooms_organisation.xml");

            FileOutputStream fos = new FileOutputStream(roomListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Backup Appointements
         */
        List<Appointment> aList = appointmentDao.getAppointments();

        if (aList != null) {
            Document doc = this.createAppointementDocument(aList);

            File aListXML = new File(backup_dir, "appointements.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);

        }

        /*
         * ##################### Backup Meeting Members
         */
        List<MeetingMember> membersList = meetingMemberDao.getMeetingMembers();

        if (membersList != null) {
            Document doc = this.createMeetingMemberDocument(membersList);

            File aListXML = new File(backup_dir, "meetingmembers.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);

        }

        /*
         * ##################### LDAP Configs
         */
        List<LdapConfig> ldapConfigList = ldapConfigDao.getLdapConfigs();

        if (ldapConfigList != null) {
            Document doc = this.createLdapConfigDocument(ldapConfigList);

            File aListXML = new File(backup_dir, "ldapconfigs.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Private Messages
         */
        List<PrivateMessages> privateMessages = privateMessagesDao.getPrivateMessages();

        if (privateMessages != null) {
            Document doc = this.createPrivateMessagesDocument(privateMessages);

            File aListXML = new File(backup_dir, "privateMessages.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Private Message Folders
         */
        List<PrivateMessageFolder> privateMessageFolders = privateMessageFolderDao.getPrivateMessageFolders();

        if (privateMessageFolders != null) {
            Document doc = this.createPrivateMessageFolderDocument(privateMessageFolders);

            File aListXML = new File(backup_dir, "privateMessageFolder.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### User Contacts
         */
        List<UserContacts> userContacts = userContactsDao.getUserContacts();

        if (privateMessageFolders != null) {
            Document doc = this.createUserContactsDocument(userContacts);

            File aListXML = new File(backup_dir, "userContacts.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### File-Explorer
         */
        List<FileExplorerItem> fileExplorerList = fileExplorerItemDao.getFileExplorerItems();

        if (fileExplorerList != null) {
            Document doc = this.createFileExplorerItemDocument(fileExplorerList);

            File aListXML = new File(backup_dir, "fileExplorerItems.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Recordings
         */
        List<FlvRecording> flvRecordings = flvRecordingDao.getAllFlvRecordings();

        for (FlvRecording flvRecording : flvRecordings) {
            flvRecording.setFlvRecordingMetaData(
                    flvRecordingMetaDataDao.getFlvRecordingMetaDataByRecording(flvRecording.getFlvRecordingId()));
        }

        if (privateMessageFolders != null) {
            Document doc = this.createFlvRecordingDocument(flvRecordings);

            File aListXML = new File(backup_dir, "flvRecordings.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Polls
         */
        List<RoomPoll> roomPolls = pollManagement.getPollListBackup();

        if (roomPolls != null) {
            Document doc = this.createRoomPollDocument(roomPolls);

            File aListXML = new File(backup_dir, "roompolls.xml");

            FileOutputStream fos = new FileOutputStream(aListXML);

            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Config
         */
        List<Configuration> configs = cfgManagement.getConfigurations(0, Integer.MAX_VALUE, "conf_key", true);
        if (configs != null) {
            Document doc = createConfigDocument(configs);
            File aListXML = new File(backup_dir, "configs.xml");
            FileOutputStream fos = new FileOutputStream(aListXML);
            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Asterisk SIP Data
         */
        List<AsteriskSipUsers> asteriskSipUsers = asteriskDAOImpl.getAsteriskSipUsers();
        if (asteriskSipUsers != null) {
            Document doc = createAsteriskSipUsersDocument(asteriskSipUsers);
            File aListXML = new File(backup_dir, "asterisksipusers.xml");
            FileOutputStream fos = new FileOutputStream(aListXML);
            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Asterisk SIP Extensions
         */
        List<Extensions> extensions = asteriskDAOImpl.getExtensions();
        if (extensions != null) {
            Document doc = createExtensionsDocument(extensions);
            File aListXML = new File(backup_dir, "extensions.xml");
            FileOutputStream fos = new FileOutputStream(aListXML);
            this.serializetoXML(fos, "UTF-8", doc);
        }

        /*
         * ##################### Asterisk SIP Meetme
         */
        List<MeetMe> members = asteriskDAOImpl.getMembers();
        if (members != null) {
            Document doc = createMembersDocument(members);
            File aListXML = new File(backup_dir, "members.xml");
            FileOutputStream fos = new FileOutputStream(aListXML);
            this.serializetoXML(fos, "UTF-8", doc);
        }

        if (includeFiles) {
            /*
             * ##################### Backup Room Files
             */
            File targetDir = new File(backup_dir, "roomFiles");

            if (!targetDir.exists()) {
                targetDir.mkdir();
            }

            File sourceDir = new File(omFilesDir, OpenmeetingsVariables.UPLOAD_DIR);

            File[] files = sourceDir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    if (!file.getName().equals("backup") && !file.getName().equals("import")) {

                        targetDir = new File(backup_dir, "roomFiles" + File.separatorChar + file.getName());

                        log.debug("### " + file.getName());

                        copyDirectory(file, targetDir);
                    }
                }
            }

            /*
             * ##################### Backup Recording Files
             */
            File targetDirRec = new File(backup_dir, "recordingFiles");

            if (!targetDirRec.exists()) {
                targetDirRec.mkdir();
            }

            File sourceDirRec = new File(omFilesDir,
                    OpenmeetingsVariables.STREAMS_DIR + File.separatorChar + "hibernate" + File.separatorChar);

            copyDirectory(sourceDirRec, targetDirRec);
        }

        List<File> fileList = new ArrayList<File>();
        log.debug("---Getting references to all files in: " + backup_dir.getCanonicalPath());
        getAllFiles(backup_dir, fileList);
        log.debug("---Creating zip file");
        writeZipFile(backup_dir, fileList, new FileOutputStream(filePath));
        log.debug("---Done");
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
     * , javax.servlet.http.HttpServletResponse)
     */
    public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
            ServletContext servletCtx) throws ServletException, IOException {

        String sid = httpServletRequest.getParameter("sid");
        if (sid == null) {
            sid = "default";
        }
        log.debug("sid: " + sid);

        Long users_id = sessionManagement.checkSession(sid);
        Long user_level = userManagement.getUserLevelByID(users_id);

        log.debug("users_id: " + users_id);
        log.debug("user_level: " + user_level);

        if (authLevelManagement.checkAdminLevel(user_level)) {
            // if (true) {

            String includeFileOption = httpServletRequest.getParameter("includeFileOption");
            boolean includeFiles = includeFileOption == null || "yes".equals(includeFileOption);

            String moduleName = httpServletRequest.getParameter("moduleName");
            if (moduleName == null) {
                moduleName = "moduleName";
            }
            log.debug("moduleName: " + moduleName);

            if (moduleName.equals("backup")) {

                /*
                 * ##################### Create Base Folder structure
                 */

                String current_dir = servletCtx.getRealPath("/");
                File working_dir = new File(new File(current_dir, OpenmeetingsVariables.UPLOAD_DIR), "backup");

                if (!working_dir.exists()) {
                    working_dir.mkdir();
                }

                String dateString = "backup_" + CalendarPatterns.getTimeForStreamId(new Date());

                File backup_dir = new File(working_dir, dateString);
                String requestedFile = dateString + ".zip";
                File backupFile = new File(backup_dir, requestedFile);

                String full_path = backupFile.getAbsolutePath();
                try {
                    performExport(full_path, backup_dir, includeFiles, current_dir);

                    RandomAccessFile rf = new RandomAccessFile(full_path, "r");

                    httpServletResponse.reset();
                    httpServletResponse.resetBuffer();
                    httpServletResponse.setContentType("APPLICATION/OCTET-STREAM");
                    httpServletResponse.setHeader("Content-Disposition",
                            "attachment; filename=\"" + requestedFile + "\"");
                    httpServletResponse.setHeader("Content-Length", "" + rf.length());

                    OutputStream out = httpServletResponse.getOutputStream();

                    byte[] buffer = new byte[1024];
                    int readed = -1;

                    while ((readed = rf.read(buffer, 0, buffer.length)) > -1) {
                        out.write(buffer, 0, readed);
                    }

                    rf.close();

                    out.flush();
                    out.close();
                } catch (Exception er) {
                    log.error("Error exporting: ", er);
                }

                if (backupFile.exists()) {
                    // log.debug("DELETE :1: "+backupFile.getAbsolutePath());
                    backupFile.delete();
                }

                deleteDirectory(backup_dir);

            }
        } else {
            log.debug("ERROR LangExport: not authorized FileDownload " + (new Date()));
        }
    }

    private Document createRoomPollDocument(List<RoomPoll> roomPollList) {

        Document document = getDocument();

        Element root = document.addElement("root");

        Element roompolls = root.addElement("roompolls");

        for (RoomPoll roomPollItem : roomPollList) {

            Element roompoll = roompolls.addElement("roompoll");

            roompoll.addElement("pollname").addCDATA(formatString("" + roomPollItem.getPollName()));
            roompoll.addElement("pollquestion").addCDATA(formatString("" + roomPollItem.getPollQuestion()));
            roompoll.addElement("archived").addCDATA(formatString("" + roomPollItem.isArchived()));
            roompoll.addElement("created")
                    .addCDATA(formatString("" + CalendarPatterns.getExportDate(roomPollItem.getCreated())));
            if (roomPollItem.getCreatedBy() != null) {
                roompoll.addElement("createdbyuserid")
                        .addCDATA(formatString("" + roomPollItem.getCreatedBy().getUser_id()));
            } else {
                roompoll.addElement("createdbyuserid").addCDATA(formatString(""));
            }
            if (roomPollItem.getPollType() != null) {
                roompoll.addElement("polltypeid")
                        .addCDATA(formatString("" + roomPollItem.getPollType().getPollTypesId()));
            } else {
                roompoll.addElement("polltypeid").addCDATA(formatString(""));
            }
            if (roomPollItem.getRoom() != null) {
                roompoll.addElement("roomid").addCDATA(formatString("" + roomPollItem.getRoom().getRooms_id()));
            } else {
                roompoll.addElement("roomid").addCDATA(formatString(""));
            }

            if (roomPollItem.getRoomPollAnswerList() == null) {
                continue;
            }

            Element roompollanswers = roompoll.addElement("roompollanswers");

            for (RoomPollAnswers roomPollAnswersItem : roomPollItem.getRoomPollAnswerList()) {

                Element roompollanswer = roompollanswers.addElement("roompollanswer");

                roompollanswer.addElement("pointlist")
                        .addCDATA(formatString("" + roomPollAnswersItem.getPointList()));
                roompollanswer.addElement("answer").addCDATA(formatString("" + roomPollAnswersItem.getAnswer()));
                roompollanswer.addElement("votedate").addCDATA(
                        formatString("" + CalendarPatterns.getExportDate(roomPollAnswersItem.getVoteDate())));
                if (roomPollAnswersItem.getVotedUser() != null) {
                    roompollanswer.addElement("voteduserid")
                            .addCDATA(formatString("" + roomPollAnswersItem.getVotedUser().getUser_id()));
                } else {
                    roompollanswer.addElement("voteduserid").addCDATA(formatString(""));
                }

            }

        }

        return document;
    }

    public boolean deleteDirectory(File path) throws IOException {

        // log.debug("deleteDirectory :: "+path);

        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }

        // log.debug("DELETE :3: "+path.getAbsolutePath());

        return (path.delete());
    }

    public void getAllFiles(File dir, List<File> fileList) throws IOException {
        try {
            File[] files = dir.listFiles();
            for (File file : files) {
                fileList.add(file);
                if (file.isDirectory()) {
                    // log.debug("directory:" + file.getCanonicalPath());
                    getAllFiles(file, fileList);
                } else {
                    // log.debug("     file:" + file.getCanonicalPath());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void writeZipFile(File directoryToZip, List<File> fileList, FileOutputStream fos) {

        try {
            ZipOutputStream zos = new ZipOutputStream(fos);

            for (File file : fileList) {
                if (!file.isDirectory()) { // we only zip files, not directories
                    addToZip(directoryToZip, file, zos);
                }
            }

            zos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void copyDirectory(File sourceLocation, File targetLocation) throws IOException {

        // log.debug("^^^^ "+sourceLocation.getName()+" || "+targetLocation.getName());

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
            }
        } else {

            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
    }

    public void addToZip(File directoryToZip, File file, ZipOutputStream zos)
            throws FileNotFoundException, IOException {

        FileInputStream fis = new FileInputStream(file);

        // we want the zipEntry's path to be a relative path that is relative
        // to the directory being zipped, so chop off the rest of the path
        String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
                file.getCanonicalPath().length());
        log.debug("Writing '" + zipFilePath + "' to zip file");
        ZipEntry zipEntry = new ZipEntry(zipFilePath);
        zos.putNextEntry(zipEntry);

        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }

        zos.closeEntry();
        fis.close();
    }

    public Document createAppointementDocument(List<Appointment> aList) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element appointments = root.addElement("appointments");

        for (Iterator<Appointment> it = aList.iterator(); it.hasNext();) {
            Appointment a = it.next();

            Element appointment = appointments.addElement("appointment");

            appointment.addElement("appointmentId").addCDATA("" + a.getAppointmentId());
            appointment.addElement("appointmentName").addCDATA("" + a.getAppointmentName());
            appointment.addElement("appointmentLocation").addCDATA("" + a.getAppointmentLocation());
            appointment.addElement("appointmentDescription").addCDATA("" + a.getAppointmentDescription());
            appointment.addElement("appointmentStarttime")
                    .addCDATA(CalendarPatterns.getExportDate(a.getAppointmentStarttime()));
            appointment.addElement("appointmentEndtime")
                    .addCDATA(CalendarPatterns.getExportDate(a.getAppointmentEndtime()));
            if (a.getAppointmentCategory() != null) {
                appointment.addElement("categoryId").addCDATA("" + a.getAppointmentCategory().getCategoryId());
            } else {
                appointment.addElement("categoryId").addCDATA("" + 0);
            }
            if (a.getUserId() != null) {
                appointment.addElement("users_id").addCDATA("" + a.getUserId().getUser_id());
            } else {
                appointment.addElement("users_id").addCDATA("" + 0);
            }
            appointment.addElement("deleted").addCDATA("" + a.getDeleted());
            if (a.getRemind() != null) {
                appointment.addElement("typId").addCDATA("" + a.getRemind().getTypId());
            } else {
                appointment.addElement("typId").addCDATA("" + 0);
            }
            appointment.addElement("isDaily").addCDATA("" + a.getIsDaily());
            appointment.addElement("isWeekly").addCDATA("" + a.getIsWeekly());
            appointment.addElement("isMonthly").addCDATA("" + a.getIsMonthly());
            appointment.addElement("isYearly").addCDATA("" + a.getIsYearly());
            if (a.getRoom() != null) {
                appointment.addElement("room_id").addCDATA("" + a.getRoom().getRooms_id());
            } else {
                appointment.addElement("room_id").addCDATA("" + 0);
            }
            appointment.addElement("icalId").addCDATA("" + a.getIcalId());
            appointment.addElement("language_id").addCDATA("" + a.getLanguage_id());
            appointment.addElement("isPasswordProtected").addCDATA("" + a.getIsPasswordProtected());
            appointment.addElement("password").addCDATA("" + a.getPassword());
        }

        return document;
    }

    public Document createRoomsDocument(List<Rooms> roomList) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element rooms = root.addElement("rooms");

        for (Iterator<Rooms> it = roomList.iterator(); it.hasNext();) {
            Rooms r = it.next();

            Element room = rooms.addElement("room");

            room.addElement("name").addCDATA("" + r.getName());
            room.addElement("rooms_id").addCDATA("" + r.getRooms_id());
            room.addElement("deleted").addCDATA("" + r.getDeleted());
            room.addElement("comment").addCDATA("" + r.getComment());
            room.addElement("numberOfPartizipants").addCDATA("" + r.getNumberOfPartizipants());
            room.addElement("appointment").addCDATA("" + r.getAppointment());
            room.addElement("externalRoomId").addCDATA("" + r.getExternalRoomId());
            room.addElement("externalRoomType").addCDATA("" + r.getExternalRoomType());
            if (r.getRoomtype() != null) {
                room.addElement("roomtypeId").addCDATA("" + r.getRoomtype().getRoomtypes_id());
            } else {
                room.addElement("roomtypeId").addCDATA("" + 0);
            }
            if (r.getOwnerId() != null) {
                room.addElement("ownerid").addCDATA("" + r.getOwnerId());
            } else {
                room.addElement("ownerid").addCDATA("");
            }
            if (r.getWaitForRecording() != null) {
                room.addElement("waitForRecording").addCDATA("" + r.getWaitForRecording());
            } else {
                room.addElement("waitForRecording").addCDATA("");
            }
            if (r.getHideTopBar() != null) {
                room.addElement("hideTopBar").addCDATA("" + r.getHideTopBar());
            } else {
                room.addElement("hideTopBar").addCDATA("");
            }
            if (r.getAllowRecording() != null) {
                room.addElement("allowRecording").addCDATA("" + r.getAllowRecording());
            } else {
                room.addElement("allowRecording").addCDATA("");
            }
            room.addElement("isDemoRoom").addCDATA("" + r.getIsDemoRoom());
            room.addElement("demoTime").addCDATA("" + r.getDemoTime());
            room.addElement("isModeratedRoom").addCDATA("" + r.getIsModeratedRoom());
            room.addElement("allowUserQuestions").addCDATA("" + r.getAllowUserQuestions());
            room.addElement("isAudioOnly").addCDATA("" + r.getIsAudioOnly());
            room.addElement("sipNumber").addCDATA("" + r.getSipNumber());
            room.addElement("conferencePin").addCDATA("" + r.getConferencePin());
            if (r.getIspublic() != null) {
                room.addElement("ispublic").addCDATA("" + r.getIspublic());
            } else {
                r.setIspublic(false);
                room.addElement("ispublic").addCDATA("" + r.getIspublic());
            }

            if (r.getShowMicrophoneStatus() != null) {
                room.addElement("showMicrophoneStatus").addCDATA("" + r.getShowMicrophoneStatus());
            } else {
                r.setShowMicrophoneStatus(false);
                room.addElement("showMicrophoneStatus").addCDATA("" + r.getShowMicrophoneStatus());
            }

            if (r.getHideActionsMenu() != null) {
                room.addElement("hideActionsMenu").addCDATA("" + r.getHideActionsMenu());
            } else {
                room.addElement("hideActionsMenu").addCDATA("");
            }

            if (r.getHideActivitiesAndActions() != null) {
                room.addElement("hideActivitiesAndActions").addCDATA("" + r.getHideActivitiesAndActions());
            } else {
                room.addElement("hideActivitiesAndActions").addCDATA("");
            }

            if (r.getHideChat() != null) {
                room.addElement("hideChat").addCDATA("" + r.getHideChat());
            } else {
                room.addElement("hideChat").addCDATA("");
            }

            if (r.getHideFilesExplorer() != null) {
                room.addElement("hideFilesExplorer").addCDATA("" + r.getHideFilesExplorer());
            } else {
                room.addElement("hideFilesExplorer").addCDATA("");
            }

            if (r.getHideScreenSharing() != null) {
                room.addElement("hideScreenSharing").addCDATA("" + r.getHideScreenSharing());
            } else {
                room.addElement("hideScreenSharing").addCDATA("");
            }

            if (r.getHideWhiteboard() != null) {
                room.addElement("hideWhiteboard").addCDATA("" + r.getHideWhiteboard());
            } else {
                room.addElement("hideWhiteboard").addCDATA("");
            }

            room.addElement("isClosed").addCDATA("" + r.getIsClosed());
            room.addElement("redirectURL").addCDATA("" + r.getRedirectURL());

            List<RoomModerators> roomModeratorsList = roomModeratorsDao.getRoomModeratorByRoomId(r.getRooms_id());

            Element room_moderators = room.addElement("room_moderators");

            for (RoomModerators roomModerator : roomModeratorsList) {

                Element room_moderator = room_moderators.addElement("room_moderator");

                if (roomModerator.getUser() != null) {
                    room_moderator.addElement("user_id").addCDATA("" + roomModerator.getUser().getUser_id());
                } else {
                    room_moderator.addElement("user_id").addCDATA("0");
                }

                room_moderator.addElement("is_supermoderator").addCDATA("" + roomModerator.getIsSuperModerator());
            }

        }

        return document;
    }

    public Document createOrgDocument(List<Organisation> orgList) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element organisations = root.addElement("organisations");

        for (Iterator<Organisation> it = orgList.iterator(); it.hasNext();) {
            Organisation org = it.next();

            Element organisation = organisations.addElement("organisation");

            organisation.addElement("name").addCDATA(formatString("" + org.getName()));
            organisation.addElement("organisation_id").addCDATA(formatString("" + org.getOrganisation_id()));
            organisation.addElement("deleted").addCDATA(formatString("" + org.getDeleted()));

        }

        return document;
    }

    private String formatString(String str) {
        // Todo fix if there is sth. we can do to prevent unicode replacement
        // with invalid chars
        // This is the place if you need to do special conversion on the strings
        return str;
    }

    private Document createPrivateMessagesDocument(List<PrivateMessages> privateMessages) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element privatemessages = root.addElement("privatemessages");

        for (Iterator<PrivateMessages> it = privateMessages.iterator(); it.hasNext();) {
            PrivateMessages pm = it.next();

            Element privateMessage = privatemessages.addElement("privatemessage");

            privateMessage.addElement("privateMessageId").addCDATA(formatString("" + pm.getPrivateMessageId()));
            privateMessage.addElement("message").addCDATA(formatString("" + pm.getMessage()));
            privateMessage.addElement("subject").addCDATA(formatString("" + pm.getSubject()));
            privateMessage.addElement("privateMessageFolderId")
                    .addCDATA(formatString("" + pm.getPrivateMessageFolderId()));
            privateMessage.addElement("userContactId").addCDATA(formatString("" + pm.getUserContactId()));
            privateMessage.addElement("parentMessage").addCDATA(formatString("" + pm.getParentMessage()));
            privateMessage.addElement("bookedRoom").addCDATA(formatString("" + pm.getBookedRoom()));
            if (pm.getFrom() != null) {
                privateMessage.addElement("from").addCDATA(formatString("" + pm.getFrom().getUser_id()));
            } else {
                privateMessage.addElement("from").addCDATA("0");
            }
            if (pm.getTo() != null) {
                privateMessage.addElement("to").addCDATA(formatString("" + pm.getTo().getUser_id()));
            } else {
                privateMessage.addElement("to").addCDATA("0");
            }
            privateMessage.addElement("inserted")
                    .addCDATA(formatString("" + CalendarPatterns.getExportDate(pm.getInserted())));
            privateMessage.addElement("isContactRequest").addCDATA(formatString("" + pm.getIsContactRequest()));
            privateMessage.addElement("isRead").addCDATA(formatString("" + pm.getIsRead()));
            privateMessage.addElement("isTrash").addCDATA(formatString("" + pm.getIsTrash()));
            if (pm.getOwner() != null) {
                privateMessage.addElement("owner").addCDATA(formatString("" + pm.getOwner().getUser_id()));
            } else {
                privateMessage.addElement("owner").addCDATA("0");
            }

            if (pm.getRoom() != null) {
                privateMessage.addElement("room").addCDATA(formatString("" + pm.getRoom().getRooms_id()));
            } else {
                privateMessage.addElement("room").addCDATA("0");
            }

        }

        return document;
    }

    private Document createFileExplorerItemDocument(List<FileExplorerItem> fileExplorerItems) throws Exception {

        Document document = getDocument();

        Element root = document.addElement("root");

        Element fileExplorerItemsElement = root.addElement("fileExplorerItems");

        for (Iterator<FileExplorerItem> it = fileExplorerItems.iterator(); it.hasNext();) {
            FileExplorerItem fileExplorerItem = it.next();

            Element fileExplorerItemElement = fileExplorerItemsElement.addElement("fileExplorerItem");

            fileExplorerItemElement.addElement("fileExplorerItemId")
                    .addCDATA(formatString("" + fileExplorerItem.getFileExplorerItemId()));
            fileExplorerItemElement.addElement("fileName")
                    .addCDATA(formatString("" + fileExplorerItem.getFileName()));
            fileExplorerItemElement.addElement("fileHash")
                    .addCDATA(formatString("" + fileExplorerItem.getFileHash()));
            fileExplorerItemElement.addElement("parentFileExplorerItemId")
                    .addCDATA(formatString("" + fileExplorerItem.getParentFileExplorerItemId()));
            fileExplorerItemElement.addElement("room_id")
                    .addCDATA(formatString("" + fileExplorerItem.getRoom_id()));
            fileExplorerItemElement.addElement("ownerId")
                    .addCDATA(formatString("" + fileExplorerItem.getOwnerId()));
            fileExplorerItemElement.addElement("isFolder")
                    .addCDATA(formatString("" + fileExplorerItem.getIsFolder()));
            fileExplorerItemElement.addElement("isImage")
                    .addCDATA(formatString("" + fileExplorerItem.getIsImage()));
            fileExplorerItemElement.addElement("isPresentation")
                    .addCDATA(formatString("" + fileExplorerItem.getIsPresentation()));
            fileExplorerItemElement.addElement("isVideo")
                    .addCDATA(formatString("" + fileExplorerItem.getIsVideo()));
            fileExplorerItemElement.addElement("insertedBy")
                    .addCDATA(formatString("" + fileExplorerItem.getInsertedBy()));
            fileExplorerItemElement.addElement("inserted")
                    .addCDATA(formatString("" + CalendarPatterns.getExportDate(fileExplorerItem.getInserted())));
            fileExplorerItemElement.addElement("updated")
                    .addCDATA(formatString("" + CalendarPatterns.getExportDate(fileExplorerItem.getUpdated())));
            fileExplorerItemElement.addElement("deleted")
                    .addCDATA(formatString("" + fileExplorerItem.getDeleted()));
            fileExplorerItemElement.addElement("fileSize")
                    .addCDATA(formatString("" + fileExplorerItem.getFileSize()));
            fileExplorerItemElement.addElement("flvWidth")
                    .addCDATA(formatString("" + fileExplorerItem.getFlvWidth()));
            fileExplorerItemElement.addElement("flvHeight")
                    .addCDATA(formatString("" + fileExplorerItem.getFlvHeight()));
            fileExplorerItemElement.addElement("previewImage")
                    .addCDATA(formatString("" + fileExplorerItem.getPreviewImage()));
            fileExplorerItemElement.addElement("wmlFilePath")
                    .addCDATA(formatString("" + fileExplorerItem.getWmlFilePath()));
            fileExplorerItemElement.addElement("isStoredWmlFile")
                    .addCDATA(formatString("" + fileExplorerItem.getIsStoredWmlFile()));
            fileExplorerItemElement.addElement("isChart")
                    .addCDATA(formatString("" + fileExplorerItem.getIsChart()));

        }

        return document;

    }

    private Document createFlvRecordingDocument(List<FlvRecording> flvRecordings) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element flvrecordings = root.addElement("flvrecordings");

        for (Iterator<FlvRecording> it = flvRecordings.iterator(); it.hasNext();) {
            FlvRecording flvRec = it.next();

            Element flvrecording = flvrecordings.addElement("flvrecording");

            flvrecording.addElement("alternateDownload").addCDATA(formatString("" + flvRec.getAlternateDownload()));
            flvrecording.addElement("comment").addCDATA(formatString("" + flvRec.getComment()));
            flvrecording.addElement("deleted").addCDATA(formatString("" + flvRec.getDeleted()));
            flvrecording.addElement("fileHash").addCDATA(formatString("" + flvRec.getFileHash()));
            flvrecording.addElement("fileName").addCDATA(formatString("" + flvRec.getFileName()));
            flvrecording.addElement("flvRecordingId").addCDATA(formatString("" + flvRec.getFlvRecordingId()));
            flvrecording.addElement("previewImage").addCDATA(formatString("" + flvRec.getPreviewImage()));
            flvrecording.addElement("recorderStreamId").addCDATA(formatString("" + flvRec.getRecorderStreamId()));
            flvrecording.addElement("fileSize").addCDATA(formatString("" + flvRec.getFileSize()));
            flvrecording.addElement("flvHeight").addCDATA(formatString("" + flvRec.getFlvHeight()));
            flvrecording.addElement("flvWidth").addCDATA(formatString("" + flvRec.getFlvWidth()));
            flvrecording.addElement("height").addCDATA(formatString("" + flvRec.getHeight()));
            flvrecording.addElement("width").addCDATA(formatString("" + flvRec.getWidth()));
            flvrecording.addElement("insertedBy").addCDATA(formatString("" + flvRec.getInsertedBy()));
            flvrecording.addElement("organization_id").addCDATA(formatString("" + flvRec.getOrganization_id()));
            flvrecording.addElement("ownerId").addCDATA(formatString("" + flvRec.getOwnerId()));
            flvrecording.addElement("parentFileExplorerItemId")
                    .addCDATA(formatString("" + flvRec.getParentFileExplorerItemId()));
            flvrecording.addElement("progressPostProcessing")
                    .addCDATA(formatString("" + flvRec.getProgressPostProcessing()));
            flvrecording.addElement("room_id").addCDATA(formatString("" + flvRec.getRoom_id()));
            flvrecording.addElement("inserted")
                    .addCDATA(formatString("" + CalendarPatterns.getExportDate(flvRec.getInserted())));
            flvrecording.addElement("isFolder").addCDATA(formatString("" + flvRec.getIsFolder()));
            flvrecording.addElement("isImage").addCDATA(formatString("" + flvRec.getIsImage()));
            flvrecording.addElement("isInterview").addCDATA(formatString("" + flvRec.getIsInterview()));
            flvrecording.addElement("isPresentation").addCDATA(formatString("" + flvRec.getIsPresentation()));
            flvrecording.addElement("isRecording").addCDATA(formatString("" + flvRec.getIsRecording()));
            flvrecording.addElement("recordEnd")
                    .addCDATA(formatString("" + CalendarPatterns.getExportDate(flvRec.getRecordEnd())));
            flvrecording.addElement("recordStart")
                    .addCDATA(formatString("" + CalendarPatterns.getExportDate(flvRec.getRecordStart())));

            Element flvrecordingmetadatas = flvrecording.addElement("flvrecordingmetadatas");

            for (Iterator<FlvRecordingMetaData> itMeta = flvRec.getFlvRecordingMetaData().iterator(); itMeta
                    .hasNext();) {
                FlvRecordingMetaData flvMeta = itMeta.next();

                Element flvrecordingmetadata = flvrecordingmetadatas.addElement("flvrecordingmetadata");

                flvrecordingmetadata.addElement("flvRecordingMetaDataId")
                        .addCDATA(formatString("" + flvMeta.getFlvRecordingMetaDataId()));
                flvrecordingmetadata.addElement("freeTextUserName")
                        .addCDATA(formatString("" + flvMeta.getFreeTextUserName()));
                flvrecordingmetadata.addElement("fullWavAudioData")
                        .addCDATA(formatString("" + flvMeta.getFullWavAudioData()));
                flvrecordingmetadata.addElement("streamName").addCDATA(formatString("" + flvMeta.getStreamName()));
                flvrecordingmetadata.addElement("wavAudioData")
                        .addCDATA(formatString("" + flvMeta.getWavAudioData()));
                flvrecordingmetadata.addElement("initialGapSeconds")
                        .addCDATA(formatString("" + flvMeta.getInitialGapSeconds()));
                flvrecordingmetadata.addElement("insertedBy").addCDATA(formatString("" + flvMeta.getInsertedBy()));
                flvrecordingmetadata.addElement("interiewPodId")
                        .addCDATA(formatString("" + flvMeta.getInteriewPodId()));
                flvrecordingmetadata.addElement("audioIsValid")
                        .addCDATA(formatString("" + flvMeta.getAudioIsValid()));
                flvrecordingmetadata.addElement("inserted")
                        .addCDATA(formatString("" + CalendarPatterns.getExportDate(flvMeta.getInserted())));
                flvrecordingmetadata.addElement("isAudioOnly")
                        .addCDATA(formatString("" + flvMeta.getIsAudioOnly()));
                flvrecordingmetadata.addElement("isScreenData")
                        .addCDATA(formatString("" + flvMeta.getIsScreenData()));
                flvrecordingmetadata.addElement("isVideoOnly")
                        .addCDATA(formatString("" + flvMeta.getIsVideoOnly()));
                flvrecordingmetadata.addElement("recordEnd")
                        .addCDATA(formatString("" + CalendarPatterns.getExportDate(flvMeta.getRecordEnd())));
                flvrecordingmetadata.addElement("recordStart")
                        .addCDATA(formatString("" + CalendarPatterns.getExportDate(flvMeta.getRecordStart())));
                flvrecordingmetadata.addElement("updated")
                        .addCDATA(formatString("" + CalendarPatterns.getExportDate(flvMeta.getUpdated())));

            }

        }

        return document;
    }

    private Document createPrivateMessageFolderDocument(List<PrivateMessageFolder> privateMessageFolders)
            throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element privatemessagefolders = root.addElement("privatemessagefolders");

        for (Iterator<PrivateMessageFolder> it = privateMessageFolders.iterator(); it.hasNext();) {
            PrivateMessageFolder pmf = it.next();

            Element privateMessageFolder = privatemessagefolders.addElement("privatemessagefolder");

            privateMessageFolder.addElement("privateMessageFolderId")
                    .addCDATA(formatString("" + pmf.getPrivateMessageFolderId()));
            privateMessageFolder.addElement("folderName").addCDATA(formatString("" + pmf.getFolderName()));
            privateMessageFolder.addElement("userId").addCDATA(formatString("" + pmf.getUserId()));

        }

        return document;
    }

    private Document createUserContactsDocument(List<UserContacts> userContacts) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element usercontacts = root.addElement("usercontacts");

        for (Iterator<UserContacts> it = userContacts.iterator(); it.hasNext();) {
            UserContacts uc = it.next();

            Element usercontact = usercontacts.addElement("usercontact");

            usercontact.addElement("userContactId").addCDATA(formatString("" + uc.getUserContactId()));
            usercontact.addElement("hash").addCDATA(formatString("" + uc.getHash()));
            if (uc.getContact() != null) {
                usercontact.addElement("contact").addCDATA(formatString("" + uc.getContact().getUser_id()));
            } else {
                usercontact.addElement("contact").addCDATA("0");
            }
            if (uc.getOwner() != null) {
                usercontact.addElement("owner").addCDATA(formatString("" + uc.getOwner().getUser_id()));
            } else {
                usercontact.addElement("owner").addCDATA("0");
            }
            usercontact.addElement("pending").addCDATA(formatString("" + uc.getPending()));
            usercontact.addElement("shareCalendar").addCDATA(formatString("" + uc.getShareCalendar()));

        }

        return document;
    }

    private Document createOrgRoomsDocument(List<Rooms_Organisation> roomOrgList) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element organisations = root.addElement("room_organisations");

        for (Iterator<Rooms_Organisation> it = roomOrgList.iterator(); it.hasNext();) {
            Rooms_Organisation roomOrg = it.next();

            Element room_organisation = organisations.addElement("room_organisation");

            room_organisation.addElement("rooms_organisation_id")
                    .addCDATA(formatString("" + roomOrg.getRooms_organisation_id()));
            room_organisation.addElement("organisation_id").addCDATA("true".equals(roomOrg.getDeleted()) ? "0"
                    : formatString("" + roomOrg.getOrganisation().getOrganisation_id()));
            if (roomOrg.getRoom() != null) {
                room_organisation.addElement("rooms_id")
                        .addCDATA(formatString("" + roomOrg.getRoom().getRooms_id()));
            } else {
                room_organisation.addElement("rooms_id").addCDATA("0");
            }
            room_organisation.addElement("deleted").addCDATA(formatString("" + roomOrg.getDeleted()));

        }

        return document;
    }

    private Document createMeetingMemberDocument(List<MeetingMember> memberList) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element meetingmembers = root.addElement("meetingmembers");

        for (Iterator<MeetingMember> it = memberList.iterator(); it.hasNext();) {
            MeetingMember meetMember = it.next();

            Element meetingmember = meetingmembers.addElement("meetingmember");

            meetingmember.addElement("meetingMemberId")
                    .addCDATA(formatString("" + meetMember.getMeetingMemberId()));
            if (meetMember.getUserid() != null) {
                meetingmember.addElement("userid").addCDATA(formatString("" + meetMember.getUserid().getUser_id()));
            } else {
                meetingmember.addElement("userid").addCDATA("0");
            }
            if (meetMember.getAppointment() != null) {
                meetingmember.addElement("appointment")
                        .addCDATA(formatString("" + meetMember.getAppointment().getAppointmentId()));
            } else {
                meetingmember.addElement("appointment").addCDATA("0");
            }
            meetingmember.addElement("firstname").addCDATA(formatString("" + meetMember.getFirstname()));
            meetingmember.addElement("lastname").addCDATA(formatString("" + meetMember.getLastname()));
            meetingmember.addElement("memberStatus").addCDATA(formatString("" + meetMember.getMemberStatus()));
            meetingmember.addElement("appointmentStatus")
                    .addCDATA(formatString("" + meetMember.getAppointmentStatus()));
            meetingmember.addElement("email").addCDATA(formatString("" + meetMember.getEmail()));
            meetingmember.addElement("deleted").addCDATA(formatString("" + meetMember.getDeleted()));
            meetingmember.addElement("invitor").addCDATA(formatString("" + meetMember.getInvitor()));

        }

        return document;
    }

    private Document createLdapConfigDocument(List<LdapConfig> ldapConfigList) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element ldapconfigs = root.addElement("ldapconfigs");

        for (Iterator<LdapConfig> it = ldapConfigList.iterator(); it.hasNext();) {

            LdapConfig ldapC = it.next();

            Element ldapconfig = ldapconfigs.addElement("ldapconfig");

            ldapconfig.addElement("name").addCDATA(formatString("" + ldapC.getName()));
            ldapconfig.addElement("configFileName").addCDATA(formatString("" + ldapC.getConfigFileName()));
            ldapconfig.addElement("addDomainToUserName")
                    .addCDATA(formatString("" + ldapC.getAddDomainToUserName()));
            ldapconfig.addElement("domain").addCDATA(formatString("" + ldapC.getDomain()));
            ldapconfig.addElement("isActive").addCDATA(formatString("" + ldapC.getIsActive()));

        }

        return document;
    }

    public Document createDocument(List<Users> uList) throws Exception {
        Document document = getDocument();

        Element root = document.addElement("root");

        Element users = root.addElement("users");

        for (Users u : uList) {
            Element user = users.addElement("user");

            user.addElement("user_id").addCDATA(formatString("" + u.getUser_id()));
            user.addElement("deleted").addCDATA(formatString("" + u.getDeleted()));
            user.addElement("age").addCDATA(formatString("" + CalendarPatterns.getDateByMiliSeconds(u.getAge())));
            if (u.getAvailible() != null) {
                user.addElement("availible").addCDATA(formatString("" + u.getAvailible().toString()));
            } else {
                user.addElement("availible").addCDATA("0");
            }
            user.addElement("deleted").addCDATA(formatString("" + u.getDeleted()));
            user.addElement("firstname").addCDATA(formatString("" + u.getFirstname()));
            user.addElement("lastname").addCDATA(formatString("" + u.getLastname()));
            user.addElement("login").addCDATA(formatString("" + u.getLogin()));
            user.addElement("pass").addCDATA(formatString("" + u.getPassword()));

            if (u.getActivatehash() != null) {
                user.addElement("activatehash").addCDATA(formatString("" + u.getActivatehash()));
            } else {
                user.addElement("activatehash").addCDATA(formatString(""));
            }
            if (u.getExternalUserType() != null) {
                user.addElement("externalUserType").addCDATA(formatString("" + u.getExternalUserType()));
            } else {
                user.addElement("externalUserType").addCDATA(formatString(""));
            }

            if (u.getExternalUserId() != null) {
                user.addElement("externalUserId").addCDATA(formatString("" + u.getExternalUserId()));
            } else {
                user.addElement("externalUserId").addCDATA(formatString(""));
            }

            if (u.getResethash() != null) {
                user.addElement("resethash").addCDATA(formatString("" + u.getResethash()));
            } else {
                user.addElement("resethash").addCDATA(formatString(""));
            }

            if (u.getUserOffers() != null) {
                user.addElement("userOffers").addCDATA(formatString("" + u.getUserOffers()));
            } else {
                user.addElement("userOffers").addCDATA(formatString(""));
            }

            if (u.getUserSearchs() != null) {
                user.addElement("userSearchs").addCDATA(formatString("" + u.getUserSearchs()));
            } else {
                user.addElement("userSearchs").addCDATA(formatString(""));
            }

            if (u.getForceTimeZoneCheck() != null) {
                user.addElement("forceTimeZoneCheck").addCDATA(formatString("" + u.getForceTimeZoneCheck()));
            } else {
                user.addElement("ForceTimeZoneCheck").addCDATA(formatString(""));
            }

            if (u.getLasttrans() != null) {
                user.addElement("lasttrans").addCDATA(formatString("" + u.getLasttrans()));
            } else {
                user.addElement("lasttrans").addCDATA(formatString(""));
            }

            if (u.getShowContactData() != null) {
                user.addElement("showContactData").addCDATA(formatString("" + u.getShowContactData()));
            } else {
                user.addElement("showContactData").addCDATA(formatString(""));
            }

            if (u.getShowContactDataToContacts() != null) {
                user.addElement("showContactDataToContacts")
                        .addCDATA(formatString("" + u.getShowContactDataToContacts()));
            } else {
                user.addElement("showContactDataToContacts").addCDATA(formatString(""));
            }

            String pictureuri = u.getPictureuri();
            if (pictureuri != null)
                user.addElement("pictureuri").addCDATA(pictureuri);
            else
                user.addElement("pictureuri").addCDATA(formatString(""));

            if (u.getLanguage_id() != null)
                user.addElement("language_id").addCDATA(formatString(u.getLanguage_id().toString()));
            else
                user.addElement("language_id").addCDATA(formatString(""));

            if (u.getStatus() != null) {
                user.addElement("status").addCDATA(formatString("" + u.getStatus().toString()));
            } else {
                user.addElement("status").addCDATA("0");
            }
            user.addElement("regdate").addCDATA(formatString("" + CalendarPatterns.getExportDate(u.getRegdate())));

            if (u.getTitle_id() != null) {
                user.addElement("title_id").addCDATA(formatString("" + u.getTitle_id().toString()));
                user.addElement("level_id").addCDATA(formatString("" + u.getLevel_id().toString()));
            } else {
                user.addElement("title_id").addCDATA("1");
                user.addElement("level_id").addCDATA("1");
            }

            if (u.getOmTimeZone() != null) {
                user.addElement("omTimeZone").addCDATA(formatString("" + u.getOmTimeZone().getJname()));
            } else {
                user.addElement("omTimeZone").addCDATA(formatString(""));
            }

            if (u.getAdresses() != null) {
                user.addElement("additionalname").addCDATA(formatString("" + u.getAdresses().getAdditionalname()));
                user.addElement("comment").addCDATA(formatString("" + u.getAdresses().getComment()));
                // A User can not have a deleted Adress, you cannot delete the
                // Adress of an User
                // String deleted = u.getAdresses().getDeleted()
                // Phone Number not done yet
                user.addElement("fax").addCDATA(formatString("" + u.getAdresses().getFax()));
                if (u.getAdresses().getStates() != null) {
                    user.addElement("state_id")
                            .addCDATA(formatString("" + u.getAdresses().getStates().getState_id().toString()));
                } else {
                    user.addElement("state_id").addCDATA("1");
                }
                user.addElement("street").addCDATA(formatString("" + u.getAdresses().getStreet()));
                user.addElement("town").addCDATA(formatString("" + u.getAdresses().getTown()));
                user.addElement("zip").addCDATA(formatString("" + u.getAdresses().getZip()));

                // Email and Phone
                user.addElement("mail").addCDATA(formatString("" + u.getAdresses().getEmail()));
                user.addElement("phone").addCDATA(formatString("" + u.getAdresses().getPhone()));
            } else {
                user.addElement("additionalname").addCDATA(formatString(""));
                user.addElement("comment").addCDATA(formatString(""));
                // A User can not have a deleted address, you cannot delete the
                // address of an User without deleting the user
                // only SOAP users might have a null
                // String deleted = u.getAdresses().getDeleted()
                // Phone Number not done yet
                user.addElement("fax").addCDATA(formatString(""));
                user.addElement("state_id").addCDATA("1");
                user.addElement("street").addCDATA(formatString(""));
                user.addElement("town").addCDATA(formatString(""));
                user.addElement("zip").addCDATA(formatString(""));
                user.addElement("mail").addCDATA(formatString(""));
                user.addElement("phone").addCDATA(formatString(""));

            }

            if (u.getUserSipData() != null) {
                user.addElement("sip_username").addCDATA(formatString("" + u.getUserSipData().getUsername()));
                user.addElement("sip_userpass").addCDATA(formatString("" + u.getUserSipData().getUserpass()));
                user.addElement("sip_authid").addCDATA(formatString("" + u.getUserSipData().getAuthId()));
            }

            Element user_organisations = user.addElement("organisations");
            // List<String> organisations = new LinkedList();
            for (Iterator<Organisation_Users> iterObj = u.getOrganisation_users().iterator(); iterObj.hasNext();) {

                Element user_organisation = user_organisations.addElement("user_organisation");

                Organisation_Users orgUsers = iterObj.next();
                if (orgUsers.getOrganisation() != null) {
                    user_organisation.addElement("organisation_id").addCDATA(
                            formatString("" + orgUsers.getOrganisation().getOrganisation_id().toString()));
                } else {
                    user_organisation.addElement("organisation_id").addCDATA("0");
                }

                user_organisation.addElement("deleted").addCDATA(formatString("" + orgUsers.getDeleted()));
                user_organisation.addElement("isModerator").addCDATA(formatString("" + orgUsers.getIsModerator()));

            }

            // Not need at the moment
            // Element user_groups = user.addElement("groups");

        }

        return document;
    }

    private Document getDocument() {
        Document document = DocumentHelper.createDocument();
        document.setXMLEncoding("UTF-8");
        document.addComment(BACKUP_COMMENT);

        return document;
    }

    private Document createConfigDocument(List<Configuration> configs) {
        Document document = getDocument();
        Element root = document.addElement("root");
        Element configsElem = root.addElement("configs");

        for (Configuration cfg : configs) {
            Element cfgElem = configsElem.addElement("config");
            cfgElem.addElement("id").addCDATA(formatString("" + cfg.getConfiguration_id()));
            cfgElem.addElement("comment").addCDATA(formatString("" + cfg.getComment()));
            cfgElem.addElement("key").addCDATA(formatString(cfg.getConf_key()));
            cfgElem.addElement("value").addCDATA(formatString(cfg.getConf_value()));
            cfgElem.addElement("deleted").addCDATA(formatString(cfg.getDeleted()));
            cfgElem.addElement("created")
                    .addCDATA(formatString(CalendarPatterns.getExportDate(cfg.getStarttime())));
            cfgElem.addElement("updated")
                    .addCDATA(formatString(CalendarPatterns.getExportDate(cfg.getUpdatetime())));
            cfgElem.addElement("user_id").addCDATA(formatString("" + cfg.getUser_id()));
        }
        return document;
    }

    private Document createAsteriskSipUsersDocument(List<AsteriskSipUsers> asteriskSipUsers) {

        Document document = getDocument();
        Element root = document.addElement("root");
        Element astusersElem = root.addElement("asterisksipusers");

        for (AsteriskSipUsers asteriskSipUser : asteriskSipUsers) {
            Element astuserElem = astusersElem.addElement("asterisksipuser");
            astuserElem.addElement("id").addCDATA(formatString("" + asteriskSipUser.getId()));
            astuserElem.addElement("accountcode").addCDATA(formatString("" + asteriskSipUser.getAccountcode()));
            astuserElem.addElement("disallow").addCDATA(formatString("" + asteriskSipUser.getDisallow()));
            astuserElem.addElement("allow").addCDATA(formatString("" + asteriskSipUser.getAllow()));
            astuserElem.addElement("allowoverlap").addCDATA(formatString("" + asteriskSipUser.getAllowoverlap()));
            astuserElem.addElement("allowsubscribe")
                    .addCDATA(formatString("" + asteriskSipUser.getAllowsubscribe()));
            astuserElem.addElement("allowtransfer").addCDATA(formatString("" + asteriskSipUser.getAllowtransfer()));
            astuserElem.addElement("amaflags").addCDATA(formatString("" + asteriskSipUser.getAmaflags()));
            astuserElem.addElement("autoframing").addCDATA(formatString("" + asteriskSipUser.getAutoframing()));
            astuserElem.addElement("auth").addCDATA(formatString("" + asteriskSipUser.getAuth()));
            astuserElem.addElement("buggymwi").addCDATA(formatString("" + asteriskSipUser.getBuggymwi()));
            astuserElem.addElement("callgroup").addCDATA(formatString("" + asteriskSipUser.getCallgroup()));
            astuserElem.addElement("callerid").addCDATA(formatString("" + asteriskSipUser.getCallerid()));
            astuserElem.addElement("cid_number").addCDATA(formatString("" + asteriskSipUser.getCid_number()));
            astuserElem.addElement("fullname").addCDATA(formatString("" + asteriskSipUser.getFullname()));
            astuserElem.addElement("callingpres").addCDATA(formatString("" + asteriskSipUser.getCallingpres()));
            astuserElem.addElement("canreinvite").addCDATA(formatString("" + asteriskSipUser.getCanreinvite()));
            astuserElem.addElement("context").addCDATA(formatString("" + asteriskSipUser.getContext()));
            astuserElem.addElement("defaultip").addCDATA(formatString("" + asteriskSipUser.getDefaultip()));
            astuserElem.addElement("dtmfmode").addCDATA(formatString("" + asteriskSipUser.getDtmfmode()));
            astuserElem.addElement("fromuser").addCDATA(formatString("" + asteriskSipUser.getFromuser()));
            astuserElem.addElement("fromdomain").addCDATA(formatString("" + asteriskSipUser.getFromdomain()));
            astuserElem.addElement("fullcontact").addCDATA(formatString("" + asteriskSipUser.getFullcontact()));
            astuserElem.addElement("g726nonstandard")
                    .addCDATA(formatString("" + asteriskSipUser.getG726nonstandard()));
            astuserElem.addElement("host").addCDATA(formatString("" + asteriskSipUser.getHost()));
            astuserElem.addElement("insecure").addCDATA(formatString("" + asteriskSipUser.getInsecure()));
            astuserElem.addElement("ipaddr").addCDATA(formatString("" + asteriskSipUser.getIpaddr()));
            astuserElem.addElement("language").addCDATA(formatString("" + asteriskSipUser.getLanguage()));
            astuserElem.addElement("lastms").addCDATA(formatString("" + asteriskSipUser.getLastms()));
            astuserElem.addElement("mailbox").addCDATA(formatString("" + asteriskSipUser.getMailbox()));
            astuserElem.addElement("maxcallbitrate")
                    .addCDATA(formatString("" + asteriskSipUser.getMaxcallbitrate()));
            astuserElem.addElement("mohsuggest").addCDATA(formatString("" + asteriskSipUser.getMohsuggest()));
            astuserElem.addElement("md5secret").addCDATA(formatString("" + asteriskSipUser.getMd5secret()));
            astuserElem.addElement("musiconhold").addCDATA(formatString("" + asteriskSipUser.getMusiconhold()));
            astuserElem.addElement("name").addCDATA(formatString("" + asteriskSipUser.getName()));
            astuserElem.addElement("nat").addCDATA(formatString("" + asteriskSipUser.getNat()));
            astuserElem.addElement("outboundproxy").addCDATA(formatString("" + asteriskSipUser.getOutboundproxy()));
            astuserElem.addElement("deny").addCDATA(formatString("" + asteriskSipUser.getDeny()));
            astuserElem.addElement("permit").addCDATA(formatString("" + asteriskSipUser.getPermit()));
            astuserElem.addElement("pickupgroup").addCDATA(formatString("" + asteriskSipUser.getPickupgroup()));
            astuserElem.addElement("port").addCDATA(formatString("" + asteriskSipUser.getPort()));
            astuserElem.addElement("progressinband")
                    .addCDATA(formatString("" + asteriskSipUser.getProgressinband()));
            astuserElem.addElement("promiscredir").addCDATA(formatString("" + asteriskSipUser.getPromiscredir()));
            astuserElem.addElement("qualify").addCDATA(formatString("" + asteriskSipUser.getQualify()));
            astuserElem.addElement("regexten").addCDATA(formatString("" + asteriskSipUser.getRegexten()));
            astuserElem.addElement("regseconds").addCDATA(formatString("" + asteriskSipUser.getRegseconds()));
            astuserElem.addElement("rfc2833compensate")
                    .addCDATA(formatString("" + asteriskSipUser.getRfc2833compensate()));
            astuserElem.addElement("rtptimeout").addCDATA(formatString("" + asteriskSipUser.getRtptimeout()));
            astuserElem.addElement("rtpholdtimeout")
                    .addCDATA(formatString("" + asteriskSipUser.getRtpholdtimeout()));
            astuserElem.addElement("secret").addCDATA(formatString("" + asteriskSipUser.getSecret()));
            astuserElem.addElement("sendrpid").addCDATA(formatString("" + asteriskSipUser.getSendrpid()));
            astuserElem.addElement("setvar").addCDATA(formatString("" + asteriskSipUser.getSetvar()));
            astuserElem.addElement("subscribecontext")
                    .addCDATA(formatString("" + asteriskSipUser.getSubscribecontext()));
            astuserElem.addElement("subscribemwi").addCDATA(formatString("" + asteriskSipUser.getSubscribemwi()));
            astuserElem.addElement("t38pt_udptl").addCDATA(formatString("" + asteriskSipUser.getId()));
            astuserElem.addElement("trustrpid").addCDATA(formatString("" + asteriskSipUser.getTrustrpid()));
            astuserElem.addElement("type").addCDATA(formatString("" + asteriskSipUser.getType()));
            astuserElem.addElement("useclientcode").addCDATA(formatString("" + asteriskSipUser.getUseclientcode()));
            astuserElem.addElement("username").addCDATA(formatString("" + asteriskSipUser.getUsername()));
            astuserElem.addElement("usereqphone").addCDATA(formatString("" + asteriskSipUser.getUsereqphone()));
            astuserElem.addElement("videosupport").addCDATA(formatString("" + asteriskSipUser.getVideosupport()));
            astuserElem.addElement("vmexten").addCDATA(formatString("" + asteriskSipUser.getVmexten()));

        }
        return document;

    }

    private Document createExtensionsDocument(List<Extensions> extensions) {
        Document document = getDocument();
        Element root = document.addElement("root");
        Element extensionsElem = root.addElement("extensions");

        for (Extensions extension : extensions) {
            Element extensionElem = extensionsElem.addElement("extension");
            extensionElem.addElement("id").addCDATA(formatString("" + extension.getId()));
            extensionElem.addElement("exten").addCDATA(formatString("" + extension.getExten()));
            extensionElem.addElement("priority").addCDATA(formatString("" + extension.getPriority()));
            extensionElem.addElement("app").addCDATA(formatString("" + extension.getApp()));
            extensionElem.addElement("appdata").addCDATA(formatString("" + extension.getAppdata()));
        }
        return document;
    }

    private Document createMembersDocument(List<MeetMe> members) {
        Document document = getDocument();
        Element root = document.addElement("root");
        Element membersElem = root.addElement("members");

        for (MeetMe member : members) {
            Element memberElem = membersElem.addElement("member");
            memberElem.addElement("confno").addCDATA(formatString("" + member.getConfno()));
            memberElem.addElement("pin").addCDATA(formatString("" + member.getPin()));
            memberElem.addElement("adminpin").addCDATA(formatString("" + member.getAdminpin()));
            memberElem.addElement("members").addCDATA(formatString("" + member.getMembers()));
        }
        return document;
    }

    public void serializetoXML(OutputStream out, String aEncodingScheme, Document doc) throws Exception {
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setXHTML(true);
        outformat.setEncoding(aEncodingScheme);
        XMLWriter writer = new XMLWriter(out, outformat);
        writer.write(doc);
        writer.flush();
        writer.close();
        out.close();
    }

}