Browse Source

代码质量

research/10.0
1 6 years ago
parent
commit
ee16d597d5
  1. 2
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java
  2. 50
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java
  3. 53
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java
  4. 25
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java
  5. 31
      designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java

2
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/SendDataToCloudProvider.java

@ -15,7 +15,7 @@ public interface SendDataToCloudProvider {
* @param tClass 埋点对象类型
* @throws Exception 取数过程中可能的异常
*/
<T> void getData(long currentTime, long lastTime, Class<T> tClass) throws Exception;
<T> void queryData(long currentTime, long lastTime, Class<T> tClass) throws Exception;
/**
* @param points 从swift获取的埋点数据

50
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/entity/FileEntity.java

@ -0,0 +1,50 @@
package com.fr.design.mainframe.messagecollect.entity;
/**
* @author alex sung
* @date 2019/4/8
*/
public class FileEntity {
/**
* 文件名
*/
private String fileName;
/**
* 文件的完整路径
*/
private String pathName;
/**
* 文件夹路径
*/
private String folderName;
public FileEntity(String fileName, String pathName, String folderName) {
this.fileName = fileName;
this.pathName = pathName;
this.folderName = folderName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getPathName() {
return pathName;
}
public void setPathName(String pathName) {
this.pathName = pathName;
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
}

53
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/AbstractSendDataToCloud.java

@ -1,6 +1,8 @@
package com.fr.design.mainframe.messagecollect.impl;
import com.fr.design.mainframe.messagecollect.SendDataToCloudProvider;
import com.fr.design.mainframe.messagecollect.entity.FileEntity;
import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils;
import com.fr.general.CloudCenter;
import com.fr.general.IOUtils;
import com.fr.general.http.HttpRequestType;
@ -13,6 +15,7 @@ import com.fr.stable.CommonUtils;
import com.fr.stable.EncodeConstants;
import com.fr.stable.ProductConstants;
import com.fr.stable.StableUtils;
import com.fr.stable.StringUtils;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.condition.QueryCondition;
import com.fr.stable.query.data.DataList;
@ -49,32 +52,14 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
protected String lastTime;
private static final int PAGE_SIZE = 200;
private long totalCount = -1;
protected String fileName;
protected String pathName;
protected String folderName;
private FileEntity fileEntity;
public String getFolderName() {
return folderName;
public FileEntity getFileEntity() {
return fileEntity;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getPathName() {
return pathName;
}
public void setPathName(String pathName) {
this.pathName = pathName;
public void setFileEntity(FileEntity fileEntity) {
this.fileEntity = fileEntity;
}
public String getLastTime() {
@ -99,7 +84,7 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
}
@Override
public <T> void getData(long currentTime, long lastTime, Class<T> tClass) {
public <T> void queryData(long currentTime, long lastTime, Class<T> tClass) {
queryAndSendOnePageFunctionContent(currentTime, lastTime, 0, tClass);
long page = (totalCount / PAGE_SIZE) + 1;
for (int i = 1; i < page; i++) {
@ -200,7 +185,7 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
private void generateFile(JSONObject jsonObject) {
try {
String content = jsonObject.toString();
File file = new File(pathName + ".json");
File file = new File(getFileEntity().getPathName() + ".json");
StableUtils.makesureFileExist(file);
FileOutputStream out = new FileOutputStream(file);
InputStream in = new ByteArrayInputStream(content.getBytes(EncodeConstants.ENCODING_UTF_8));
@ -213,18 +198,20 @@ public abstract class AbstractSendDataToCloud implements SendDataToCloudProvider
private static void uploadFile(File file, String keyFileName) throws IOException {
String url = generateSignedUploadUrl(keyFileName);
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
.addPart("file", new FileBody(file));
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/zip");
HttpToolbox.upload(url, builder, Charset.forName("utf-8"), headers, HttpRequestType.PUT);
if(StringUtils.isEmpty(url)){
FineLoggerFactory.getLogger().error("url is null.");
}else {
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
.addPart("file", new FileBody(file));
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/zip");
HttpToolbox.upload(url, builder, Charset.forName("utf-8"), headers, HttpRequestType.PUT);
}
}
private void deleteFileAndZipFile(File zipFile, String pathName) {
File file = new File(StableUtils.pathJoin(ProductConstants.getEnvHome(), pathName));
CommonUtils.deleteFile(file);
MessageCollectUtils.deleteDir(file);
CommonUtils.deleteFile(zipFile);
}

25
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/impl/FocusPointMessageUploader.java

@ -2,6 +2,7 @@ package com.fr.design.mainframe.messagecollect.impl;
import com.fr.config.MarketConfig;
import com.fr.design.DesignerEnvManager;
import com.fr.design.mainframe.messagecollect.entity.FileEntity;
import com.fr.design.mainframe.messagecollect.utils.MessageCollectUtils;
import com.fr.intelli.record.FocusPoint;
import com.fr.json.JSONObject;
@ -23,7 +24,7 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud {
private static final String TAG = "FocusPointMessageTag";
private static final String SEPARATOR = "_";
private static final String FOCUS_POINT_VERSION = "FocusPoint1003_";
private static final String FOCUS_POINT = "FocusPoint";
private static volatile FocusPointMessageUploader instance;
public static FocusPointMessageUploader getInstance() {
@ -49,11 +50,11 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud {
long lastTIme = MessageCollectUtils.getLastTimeMillis(lastTime);
try {
generatePath();
getData(currentTime, lastTIme, FocusPoint.class);
queryData(currentTime, lastTIme, FocusPoint.class);
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage());
}
sendZipFile(getFolderName());
sendZipFile(getFileEntity().getFolderName());
saveLastTime();
}
@ -71,16 +72,20 @@ public class FocusPointMessageUploader extends AbstractSendDataToCloud {
writer.end();
}
private void generatePath(){
private void generatePath() {
DesignerEnvManager envManager = DesignerEnvManager.getEnvManager();
String bbsUserName = MarketConfig.getInstance().getBbsUsername();
String uuid = envManager.getUUID();
//文件夹名称的格式是: "FocusPoint1003_" + uuid_bbsUserName_randomUuid,均以下划线分隔
//文件夹名称的格式是: "FocusPoint" + 大版本号 + 小版本号 + uuid + bbsUserName + randomUuid,均以下划线分隔
StringBuilder sb = new StringBuilder();
sb.append(FOCUS_POINT_VERSION).append(uuid).append(SEPARATOR).append(bbsUserName).append(SEPARATOR).append(UUID.randomUUID());
setFileName(String.valueOf(UUID.randomUUID()));
setPathName(StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString(), getFileName()));
setFolderName(StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString()));
sb.append(FOCUS_POINT).append(SEPARATOR).
append(ProductConstants.MAIN_VERSION).append(SEPARATOR).
append(ProductConstants.MINOR_VERSION).append(SEPARATOR).
append(uuid).append(SEPARATOR).append(bbsUserName).append(SEPARATOR).
append(UUID.randomUUID());
String fileName = String.valueOf(UUID.randomUUID());
String pathName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString(), fileName);
String folderName = StableUtils.pathJoin(ProductConstants.getEnvHome(), sb.toString());
setFileEntity(new FileEntity(fileName, pathName, folderName));
}
}

31
designer-realize/src/main/java/com/fr/design/mainframe/messagecollect/utils/MessageCollectUtils.java

@ -1,6 +1,5 @@
package com.fr.design.mainframe.messagecollect.utils;
import com.fr.base.FRContext;
import com.fr.general.DateUtils;
import com.fr.general.IOUtils;
import com.fr.log.FineLoggerFactory;
@ -13,13 +12,9 @@ import com.fr.third.javax.xml.stream.XMLStreamException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.util.Date;
/**
* @author alex sung
@ -27,18 +22,14 @@ import java.util.Date;
*/
public class MessageCollectUtils {
public static String getFileContent(File xmlFile) throws FileNotFoundException, UnsupportedEncodingException {
InputStream is = new FileInputStream(xmlFile);
return IOUtils.inputStream2String(is);
}
public static void readXMLFile(XMLReadable xmlReadable, File xmlFile) {
if (xmlFile == null || !xmlFile.exists()) {
return;
}
String charset = EncodeConstants.ENCODING_UTF_8;
try {
String fileContent = MessageCollectUtils.getFileContent(xmlFile);
InputStream is = new FileInputStream(xmlFile);
String fileContent = IOUtils.inputStream2String(is);
InputStream xmlInputStream = new ByteArrayInputStream(fileContent.getBytes(charset));
InputStreamReader inputStreamReader = new InputStreamReader(xmlInputStream, charset);
XMLableReader xmlReader = XMLableReader.createXMLableReader(inputStreamReader);
@ -51,11 +42,6 @@ public class MessageCollectUtils {
}
}
public static String newDateToString() {
DateFormat df = FRContext.getDefaultValues().getDateTimeFormat();
return df.format(new Date());
}
public static long getLastTimeMillis(String lastTime) {
if (StringUtils.isEmpty(lastTime)) {
return 0;
@ -66,4 +52,17 @@ public class MessageCollectUtils {
return -1;
}
}
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
}

Loading…
Cancel
Save