// Path: RESTfulShell/src/main/java/com/elminster/psn/restful/dao/IAdminDao.java
// public interface IAdminDao {
// 
//   public void dumpData() throws Exception;
//   
//   public void restoreData() throws Exception;
// }
// 
// Path: RESTfulShell/src/main/java/com/elminster/psn/restful/data/ExitCode.java
// public enum ExitCode {
// 
//   NORMAL(0),
//   COOKIE_SAVE_FAILED(0x01),
//   DB_DUMP_FAILED(0x02);
//   
//   private final int code;
//   
//   ExitCode(int code) {
//     this.code = code;
//   }
//   
//   public int getCode() {
//     return code;
//   }
// }
// 
// Path: PSN-API/src/main/java/com/elminster/retrieve/psn/util/SystemSetting.java
// public class SystemSetting extends CommonConfiguration {
// 
//   /** the system properties. */
//   private static final String SYSTEM_PROPERTIES = "system.properties";
//   /** the property name: last api called time. */
//   private static final String LAST_API_CALLED_TIME = "LAST_API_CALLED_TIME";
//   /** the property name: PSN username. */
//   private static final String PSN_USERNAME = "PSN_USERNAME";
//   /** the property name: PSN password. */
//   private static final String PSN_PASSWORD = "PSN_PASSWORD";
//   
//   /** the singleton instance. */
//   public static final SystemSetting INSTANCE = new SystemSetting();
//   /** the logger. */
//   private static final Log logger = LogFactory.getLog(SystemSetting.class);
//   
//   /**
//    * Constructor.
//    */
//   private SystemSetting() {
//     super();
//   }
// 
//   /**
//    * Load the resource files.
//    */
//   protected void loadResources() {
//     // load the system properties, if not exist create it.
//     if (!FileUtil.isFileExist(SYSTEM_PROPERTIES)) {
//       try {
//         FileUtil.createNewFile(SYSTEM_PROPERTIES);
//       } catch (IOException e) {
//         if (logger.isWarnEnabled()) {
//           logger.warn("failed to create system properties.");
//         }
//       }
//     }
//     InputStream is = null;
//     try {
//       is = new FileInputStream(SYSTEM_PROPERTIES);
//       properties.load(is);
//     } catch (IOException e) {
//       throw new IllegalStateException("Cannot initialize the system setting: " + e);
//     } finally {
//       if (null != is) {
//         try {
//           is.close();
//         } catch (IOException e) {
//           if (logger.isDebugEnabled()) {
//             logger.debug(e);
//           }
//         }
//       }
//     }
//   }
// 
//   /**
//    * @see com.elminster.common.config.CommonConfiguration#persist()
//    */
//   @Override
//   public void persist() throws IOException {
//     super.persist();
//     String comments = "updated on " + DateUtil.getCurrentDateStr();
//     OutputStream out = null;
//     try {
//       out = new FileOutputStream(SYSTEM_PROPERTIES);
//       properties.store(out, comments);
//     } finally {
//       if (null != out) {
//         out.close();
//       }
//     }
//   }
//   
//   /**
//    * Get the PSN username.
//    * @return the PSN username
//    */
//   public String getPSNUsername() {
//     return this.getStringProperty(PSN_USERNAME);
//   }
//   
//   /**
//    * Get the PSN password.
//    * @return the PSN password
//    */
//   public String getPSNPassword() {
//     return this.getStringProperty(PSN_PASSWORD);
//   }
//   
//   //////////// FIXME The API stats should be stored in the db. For convenience, using properties instead. ////////////
//   /**
//    * Update the last API called time.
//    */
//   public void updateLastApiCalledTime() {
//     setProperty(LAST_API_CALLED_TIME, System.currentTimeMillis());
//   }
//   
//   /**
//    * Get the last API called time.
//    * @return the last api called time
//    */
//   public long getLastApiCalledTime() {
//     return this.getLongProperty(LAST_API_CALLED_TIME, 0L);
//   }
// }


import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elminster.common.util.ExceptionUtil;
import com.elminster.psn.restful.dao.IAdminDao;
import com.elminster.psn.restful.data.ExitCode;
import com.elminster.retrieve.psn.util.SystemSetting;
package com.elminster.psn.restful.service;





/**
 * The admin service.
 * 
 * @author jgu
 * @version 1.0
 */
@Service
public class AdminServiceImpl implements IAdminService {

  /** the logger. **/
  private static final Log logger = LogFactory.getLog(AdminServiceImpl.class);
  
  private final IAdminDao adminDao;
  private EntityManager em;
  
  @Autowired
  public AdminServiceImpl(IAdminDao adminDao) {
    this.adminDao = adminDao;
  }
  
  @Autowired
  public void setEntityManager(EntityManager em) {
    this.em = em;
  }
  
  /**
   * dump the data.
   */
  @Override
  public int savepoint() {
    int ec = ExitCode.NORMAL.getCode();
    // save cookies
    try {
      SystemSetting.INSTANCE.persist();
// Path: RESTfulShell/src/main/java/com/elminster/psn/restful/dao/IAdminDao.java
// public interface IAdminDao {
// 
//   public void dumpData() throws Exception;
//   
//   public void restoreData() throws Exception;
// }
// 
// Path: RESTfulShell/src/main/java/com/elminster/psn/restful/data/ExitCode.java
// public enum ExitCode {
// 
//   NORMAL(0),
//   COOKIE_SAVE_FAILED(0x01),
//   DB_DUMP_FAILED(0x02);
//   
//   private final int code;
//   
//   ExitCode(int code) {
//     this.code = code;
//   }
//   
//   public int getCode() {
//     return code;
//   }
// }
// 
// Path: PSN-API/src/main/java/com/elminster/retrieve/psn/util/SystemSetting.java
// public class SystemSetting extends CommonConfiguration {
// 
//   /** the system properties. */
//   private static final String SYSTEM_PROPERTIES = "system.properties";
//   /** the property name: last api called time. */
//   private static final String LAST_API_CALLED_TIME = "LAST_API_CALLED_TIME";
//   /** the property name: PSN username. */
//   private static final String PSN_USERNAME = "PSN_USERNAME";
//   /** the property name: PSN password. */
//   private static final String PSN_PASSWORD = "PSN_PASSWORD";
//   
//   /** the singleton instance. */
//   public static final SystemSetting INSTANCE = new SystemSetting();
//   /** the logger. */
//   private static final Log logger = LogFactory.getLog(SystemSetting.class);
//   
//   /**
//    * Constructor.
//    */
//   private SystemSetting() {
//     super();
//   }
// 
//   /**
//    * Load the resource files.
//    */
//   protected void loadResources() {
//     // load the system properties, if not exist create it.
//     if (!FileUtil.isFileExist(SYSTEM_PROPERTIES)) {
//       try {
//         FileUtil.createNewFile(SYSTEM_PROPERTIES);
//       } catch (IOException e) {
//         if (logger.isWarnEnabled()) {
//           logger.warn("failed to create system properties.");
//         }
//       }
//     }
//     InputStream is = null;
//     try {
//       is = new FileInputStream(SYSTEM_PROPERTIES);
//       properties.load(is);
//     } catch (IOException e) {
//       throw new IllegalStateException("Cannot initialize the system setting: " + e);
//     } finally {
//       if (null != is) {
//         try {
//           is.close();
//         } catch (IOException e) {
//           if (logger.isDebugEnabled()) {
//             logger.debug(e);
//           }
//         }
//       }
//     }
//   }
// 
//   /**
//    * @see com.elminster.common.config.CommonConfiguration#persist()
//    */
//   @Override
//   public void persist() throws IOException {
//     super.persist();
//     String comments = "updated on " + DateUtil.getCurrentDateStr();
//     OutputStream out = null;
//     try {
//       out = new FileOutputStream(SYSTEM_PROPERTIES);
//       properties.store(out, comments);
//     } finally {
//       if (null != out) {
//         out.close();
//       }
//     }
//   }
//   
//   /**
//    * Get the PSN username.
//    * @return the PSN username
//    */
//   public String getPSNUsername() {
//     return this.getStringProperty(PSN_USERNAME);
//   }
//   
//   /**
//    * Get the PSN password.
//    * @return the PSN password
//    */
//   public String getPSNPassword() {
//     return this.getStringProperty(PSN_PASSWORD);
//   }
//   
//   //////////// FIXME The API stats should be stored in the db. For convenience, using properties instead. ////////////
//   /**
//    * Update the last API called time.
//    */
//   public void updateLastApiCalledTime() {
//     setProperty(LAST_API_CALLED_TIME, System.currentTimeMillis());
//   }
//   
//   /**
//    * Get the last API called time.
//    * @return the last api called time
//    */
//   public long getLastApiCalledTime() {
//     return this.getLongProperty(LAST_API_CALLED_TIME, 0L);
//   }
// }


// Path: RESTfulShell/src/main/java/com/elminster/psn/restful/service/AdminServiceImpl.java
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elminster.common.util.ExceptionUtil;
import com.elminster.psn.restful.dao.IAdminDao;
import com.elminster.psn.restful.data.ExitCode;
import com.elminster.retrieve.psn.util.SystemSetting;

package com.elminster.psn.restful.service;





/**
 * The admin service.
 * 
 * @author jgu
 * @version 1.0
 */
@Service
public class AdminServiceImpl implements IAdminService {

  /** the logger. **/
  private static final Log logger = LogFactory.getLog(AdminServiceImpl.class);
  
  private final IAdminDao adminDao;
  private EntityManager em;
  
  @Autowired
  public AdminServiceImpl(IAdminDao adminDao) {
    this.adminDao = adminDao;
  }
  
  @Autowired
  public void setEntityManager(EntityManager em) {
    this.em = em;
  }
  
  /**
   * dump the data.
   */
  @Override
  public int savepoint() {
    int ec = ExitCode.NORMAL.getCode();
    // save cookies
    try {
      SystemSetting.INSTANCE.persist();
    } catch (IOException e1) {