Skip to content

Commit

Permalink
rename Factory to Uploader, refactor NikePlusProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
tiejunhu committed Dec 27, 2013
1 parent 1dc2429 commit afdb263
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 65 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/oldhu/suunto2nike/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.oldhu.suunto2nike.suunto.moveslink.MovesLinkFactory;
import com.oldhu.suunto2nike.suunto.moveslink2.MovesLink2Factory;
import com.oldhu.suunto2nike.suunto.moveslink.MovesLinkUploader;
import com.oldhu.suunto2nike.suunto.moveslink2.MovesLink2Uploader;

public class App
{
Expand All @@ -30,7 +30,7 @@ public static void main(String[] args) throws Exception
private static void uploadMovesLink() throws IOException, Exception
{
log.info("Uploading MovesLink ...");
MovesLinkFactory mlf = MovesLinkFactory.getInstance();
MovesLinkUploader mlf = MovesLinkUploader.getInstance();
if (!mlf.checkIfEnvOkay()) {
return;
}
Expand All @@ -41,7 +41,7 @@ private static void uploadMovesLink() throws IOException, Exception
private static void uploadMovesLink2() throws IOException, Exception
{
log.info("Uploading MovesLink2 ...");
MovesLink2Factory ml2f = MovesLink2Factory.getInstance();
MovesLink2Uploader ml2f = MovesLink2Uploader.getInstance();
if (!ml2f.checkIfEnvOkay()) {
return;
}
Expand Down
43 changes: 36 additions & 7 deletions src/main/java/com/oldhu/suunto2nike/nike/NikePlusProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
Expand All @@ -12,22 +13,50 @@

public class NikePlusProperties
{
private static NikePlusProperties _instance = new NikePlusProperties();
private static Log log = LogFactory.getLog("NikePlusProperties");
private static Log log = LogFactory.getLog(NikePlusProperties.class);

public static final String NIKEPLUS_PASSWORD = "NIKEPLUS_PASSWORD";
public static final String NIKEPLUS_EMAIL = "NIKEPLUS_EMAIL";

public static NikePlusProperties getInstance()
private File dataFolder;

private Properties nikeProperties;

public NikePlusProperties(File folder) throws IOException
{
log.info("Checking Nike Plus properties file under " + folder.getAbsolutePath());
dataFolder = folder;
File nikeplusUser = getNikeUserPropertiesFile();
if (!nikeplusUser.exists()) {
createNikePlusUserProperties(nikeplusUser);
}
nikeProperties = getNikePlusUserProperties();
}

public String getEmail()
{
return _instance;
return nikeProperties.getProperty(NIKEPLUS_EMAIL);
}

private NikePlusProperties()
public String getPassword()
{
return nikeProperties.getProperty(NIKEPLUS_PASSWORD);

}

public void createNikePlusUserProperties(File file) throws IOException

private File getNikeUserPropertiesFile()
{
return new File(dataFolder, "nikeuser.properties");
}

private Properties getNikePlusUserProperties() throws IOException
{
Properties prop = new Properties();
prop.load(new FileInputStream(getNikeUserPropertiesFile()));
return prop;
}

private void createNikePlusUserProperties(File file) throws IOException
{
String userName;
String password;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.oldhu.suunto2nike.suunto.moveslink;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
Expand All @@ -17,17 +14,18 @@
import com.oldhu.suunto2nike.nike.NikePlusXmlGenerator;
import com.oldhu.suunto2nike.suunto.SuuntoMove;

public class MovesLinkFactory
public class MovesLinkUploader
{
private static MovesLinkFactory _factory = new MovesLinkFactory();
private static Log log = LogFactory.getLog("MovesLinkFactory");
private static MovesLinkUploader _instance = new MovesLinkUploader();
private static Log log = LogFactory.getLog(MovesLinkUploader.class);
private NikePlusProperties nikePlusProperties;

public static MovesLinkFactory getInstance()
public static MovesLinkUploader getInstance()
{
return _factory;
return _instance;
}

private MovesLinkFactory()
private MovesLinkUploader()
{

}
Expand All @@ -39,18 +37,6 @@ private File getDataFolder()
return folder;
}

private Properties getNikePlusUserProperties() throws FileNotFoundException, IOException
{
Properties prop = new Properties();
prop.load(new FileInputStream(getNikeUserPropertiesFile()));
return prop;
}

private File getNikeUserPropertiesFile()
{
return new File(getDataFolder(), "nikeuser.properties");
}

public void uploadXMLFiles() throws Exception
{
File folder = getDataFolder();
Expand Down Expand Up @@ -108,10 +94,8 @@ public void uploadXMLFiles() throws Exception
Document[] docsArray = new Document[docs.size()];
docs.toArray(docsArray);

Properties nikePlusUserProperties = getNikePlusUserProperties();

String nikeEmail = nikePlusUserProperties.getProperty(NikePlusProperties.NIKEPLUS_EMAIL);
char[] nikePassword = nikePlusUserProperties.getProperty(NikePlusProperties.NIKEPLUS_PASSWORD).toCharArray();
String nikeEmail = nikePlusProperties.getEmail();
char[] nikePassword = nikePlusProperties.getPassword().toCharArray();
NikePlus u = new NikePlus();
u.fullSync(nikeEmail, nikePassword, docsArray, null);
for (File file : pendingMovesFolder.listFiles()) {
Expand Down Expand Up @@ -153,11 +137,9 @@ public boolean checkIfEnvOkay() throws IOException
if (!folder.canWrite()) {
log.error("Cannot write to moves link data folder");
}

nikePlusProperties = new NikePlusProperties(getDataFolder());

File nikeplusUser = getNikeUserPropertiesFile();
if (!nikeplusUser.exists()) {
NikePlusProperties.getInstance().createNikePlusUserProperties(nikeplusUser);
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
import com.oldhu.suunto2nike.nike.NikePlusXmlGenerator;
import com.oldhu.suunto2nike.suunto.SuuntoMove;

public class MovesLink2Factory
public class MovesLink2Uploader
{
private static MovesLink2Factory _factory = new MovesLink2Factory();
private static Log log = LogFactory.getLog("MovesLink2Factory");
private static MovesLink2Uploader _instance = new MovesLink2Uploader();
private static Log log = LogFactory.getLog(MovesLink2Uploader.class);
private NikePlusProperties nikePlusProperties;

public static MovesLink2Factory getInstance()
public static MovesLink2Uploader getInstance()
{
return _factory;
return _instance;
}

private MovesLink2Factory()
private MovesLink2Uploader()
{

}
Expand All @@ -37,18 +38,6 @@ private File getDataFolder()
return folder;
}

private File getNikeUserPropertiesFile()
{
return new File(getDataFolder(), "nikeuser.properties");
}

private Properties getNikePlusUserProperties() throws FileNotFoundException, IOException
{
Properties prop = new Properties();
prop.load(new FileInputStream(getNikeUserPropertiesFile()));
return prop;
}

public boolean checkIfEnvOkay() throws IOException
{
File folder = getDataFolder();
Expand All @@ -60,10 +49,8 @@ public boolean checkIfEnvOkay() throws IOException
log.error("Cannot write to moveslink2 data folder");
}

File nikeplusUser = getNikeUserPropertiesFile();
if (!nikeplusUser.exists()) {
NikePlusProperties.getInstance().createNikePlusUserProperties(nikeplusUser);
}
nikePlusProperties = new NikePlusProperties(getDataFolder());

return true;
}

Expand Down Expand Up @@ -97,9 +84,8 @@ private void uploadMoveToNike(SuuntoMove suuntoMove) throws Exception
{
NikePlusXmlGenerator nikeXml = new NikePlusXmlGenerator(suuntoMove);
Document doc = nikeXml.getXML();
Properties nikePlusUserProperties = getNikePlusUserProperties();
String nikeEmail = nikePlusUserProperties.getProperty(NikePlusProperties.NIKEPLUS_EMAIL);
char[] nikePassword = nikePlusUserProperties.getProperty(NikePlusProperties.NIKEPLUS_PASSWORD).toCharArray();
String nikeEmail = nikePlusProperties.getEmail();
char[] nikePassword = nikePlusProperties.getPassword().toCharArray();
NikePlus u = new NikePlus();
u.fullSync(nikeEmail, nikePassword, new Document[] { doc } , null);
}
Expand Down

0 comments on commit afdb263

Please sign in to comment.