com.taobao.osceola.demo.acount.persist.service.impl.AccountPersistServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.taobao.osceola.demo.acount.persist.service.impl.AccountPersistServiceImpl.java

Source

/*
 * Copyright 2015 Alibaba.com All right reserved. This software is the
 * confidential and proprietary information of Alibaba.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Alibaba.com.
 */

package com.taobao.osceola.demo.acount.persist.service.impl;

import com.taobao.osceola.demo.acount.persist.exception.AccountPersistException;
import com.taobao.osceola.demo.acount.persist.model.Account;
import com.taobao.osceola.demo.acount.persist.service.AccountPersistService;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
 * AccountPersistServiceImpl.javaTODO 
 * 
 * @author Xee 2015-7-17 11:09:41
 */
public class AccountPersistServiceImpl implements AccountPersistService {

    private static final String ELEMENT_ROOT = "account-persist";

    private static final String ELEMENT_ACCOUNTS = "accounts";

    private static final String ELEMENT_ACCOUNT_ID = "id";
    private static final String ELE_ACCOUNT_NAME = "name";
    private static final String ELE_ACCOUNT_EMAIL = "email";
    private static final String ELEMENT_ACCOUNT_PASSWORD = "password";
    private static final String ELEMENT_ACCOUNT_ACTIVATED = "activated";

    private String file;

    private SAXReader reader = new SAXReader();

    /*
     * (non-Javadoc)
     * @see
     * com.taobao.osceola.demo.acount.persist.service.AccountPersistService#createAccount(com.taobao.osceola.demo.acount
     * .persist.model.Account)
     */
    @Override
    public Account createAccount(final Account account) {
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.taobao.osceola.demo.acount.persist.service.AccountPersistService#readAccount(java.lang.String)
     */
    @Override
    public Account readAccount(String id) throws AccountPersistException {
        Document doc = readDocument();

        Element accountsEle = doc.getRootElement().element(ELEMENT_ACCOUNTS);

        for (Element accountEle : accountsEle.elements()) {
            if (accountEle.elementText(ELEMENT_ACCOUNT_ID).equals(id)) {
                return buildAccount(accountEle);
            }
        }
        return null;
    }

    /**
     * @param accountEle
     * @return
     */
    private Account buildAccount(Element element) {
        Account account = new Account();

        account.setId(element.elementText(ELEMENT_ACCOUNT_ID));
        account.setName(element.elementText(ELE_ACCOUNT_NAME));
        account.setEmail(element.elementText(ELE_ACCOUNT_EMAIL));
        account.setPassword(element.elementText(ELEMENT_ACCOUNT_PASSWORD));
        account.setActivated(element.elementText(ELEMENT_ACCOUNT_ACTIVATED).equals("true"));

        return account;
    }

    /*
     * (non-Javadoc)
     * @see
     * com.taobao.osceola.demo.acount.persist.service.AccountPersistService#updateAccount(com.taobao.osceola.demo.acount
     * .persist.model.Account)
     */
    @Override
    public Account updateAccount(Account account) {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.taobao.osceola.demo.acount.persist.service.AccountPersistService#deleteAccount(java.lang.String)
     */
    @Override
    public void deleteAccount(String id) {
        // TODO Auto-generated method stub

    }

    private Document readDocument() throws AccountPersistException {
        File dataFile = new File(file);
        if (!dataFile.exists()) {
            dataFile.getParentFile().mkdirs();
            Document doc = DocumentFactory.getInstance().createDocument();
            Element rootEle = doc.addElement(ELEMENT_ROOT);
            rootEle.addElement(ELEMENT_ACCOUNTS);
            writeDocment(doc);
        }

        try {
            return reader.read(new File(file));
        } catch (DocumentException e) {
            throw new AccountPersistException("", e);
        }
    }

    /**
     * @param doc
     */
    private void writeDocment(Document doc) throws AccountPersistException {
        Writer out = null;
        try {
            out = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
            XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint());
            writer.write(doc);
        } catch (Exception e) {
            throw new AccountPersistException("", e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                throw new AccountPersistException("", e);
            }
        }
    }

    public void setFile(String file) {
        this.file = file;
    }
}