DingXiaoYu пре 3 година
родитељ
комит
3cb7468a71

+ 25 - 41
java_compile/src/main/java/com/example/services/FileService.java

@@ -1,6 +1,5 @@
 package com.example.services;
 
-import com.alibaba.fastjson.JSONObject;
 import com.example.analysis.FileAnalysis;
 import com.example.dao.*;
 import com.example.data.CsvAdapter;
@@ -13,14 +12,14 @@ import com.example.entity.Model;
 import com.example.services.pojo.*;
 import com.example.util.FileHelper;
 import com.example.util.Json2Object;
-import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import lombok.val;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -30,32 +29,26 @@ import java.util.stream.Collectors;
  * File-related service Created by lucas on 2016/11/14.
  */
 @Slf4j
-@Component
-@Data
+@Service
 public class FileService {
     @Autowired
-    private SasLoader  sasLoader;
+    private SasLoader sasLoader;
     @Autowired
-    private CsvLoader  csvLoader;
+    private CsvLoader csvLoader;
     @Autowired
     private CsvAdapter csvAdapter;
     @Autowired
-    private HdfsDao    hdfsDao;
-
+    private HdfsDao hdfsDao;
     @Autowired
     private FileInfoDao fileInfoDao;
-
     @Autowired
     private ConfigDao configDao;
-
     @Autowired
     private HeaderInfoDao headerInfoDao;
     @Autowired
     private FileAnalysis  fileAnalysis;
-
     @Autowired
     private ModelDao modelDao;
-
     @Autowired
     private FuncInfoDao funcInfoDao;
 
@@ -71,9 +64,8 @@ public class FileService {
         String fileType = FileHelper.getFileType(filename);
         File file = FileHelper.multipartToFile(inFile);
         log.info("server file location: " + file.getAbsolutePath());
-        int rsl = fileType.equals("csv") ? csvLoader.loadCsv(file.getAbsolutePath(), userId) :
+        int rsl = "csv".equals(fileType) ? csvLoader.loadCsv(file.getAbsolutePath(), userId) :
                 sasLoader.loadSas(file.getAbsolutePath(), userId);
-
         file.delete();
         return rsl;
     }
@@ -99,8 +91,8 @@ public class FileService {
      * @throws Exception
      */
     public List<FileNamePojo> getAllFiles(int userId) throws Exception {
-        val files = fileInfoDao.findByUserId(userId);
-        val rsl = files.stream().map((file) -> new FileNamePojo(String.valueOf(file.getId()), file.getFilename()))
+        List<FileInfo> files = fileInfoDao.findByUserId(userId);
+        List<FileNamePojo> rsl = files.stream().map((file) -> new FileNamePojo(String.valueOf(file.getId()), file.getFilename()))
                 .collect(Collectors.toList());
         return rsl;
     }
@@ -120,8 +112,8 @@ public class FileService {
      * @throws Exception
      */
     public FileDetailPojo getFileDetailInfo(int fileId) throws Exception {
-        val fileInfo = fileInfoDao.findById(fileId);
-        val headerInfos = headerInfoDao.findByFileInfoId(fileId);
+        FileInfo fileInfo = fileInfoDao.findById(fileId);
+        List<HeaderInfo> headerInfos = headerInfoDao.findByFileInfoId(fileId);
         return new FileDetailPojo(fileInfo.getFilename(), fileInfo.getUploadTime(), new Json2Object().Json2HashMap(fileInfo.getFileStrucInfo()), headerInfos);
     }
 
@@ -130,7 +122,6 @@ public class FileService {
      * @throws Exception
      */
     public void analysis(int fileId) throws Exception {
-
         try {
             fileAnalysis.fileAnalysis(fileId, (float) 0.01);
         } catch (Exception e) {
@@ -195,16 +186,14 @@ public class FileService {
      * @return
      */
     public boolean updateHeaderInfo(int headerId, String aliasName, String fieldDes) {
-
         HeaderInfo headerInfo = headerInfoDao.findById(headerId);
         if (headerInfo == null) {
             return false;
         }
         headerInfo.setFieldDes(fieldDes);
         headerInfo.setAliasName(aliasName);
-        val headerInfo_get = headerInfoDao.save(headerInfo);
-        return headerInfo_get != null;
-
+        headerInfoDao.save(headerInfo);
+        return true;
     }
 
     /**
@@ -213,22 +202,18 @@ public class FileService {
      * @return
      */
     public boolean updateAllHeaders(List<InnerHeaderInfo> innerHeaderInfos, List<Integer> headerToUpdate) {
-
         if (innerHeaderInfos == null || headerToUpdate == null) {
             return false;
         }
         int currLen = headerToUpdate.size();
         System.out.println("headerInfos = [" + innerHeaderInfos + "], headerToUpdate = [" + headerToUpdate + "]");
         System.out.println(currLen);
-        for (int i = 0; i < currLen; i++) {
-            int index = headerToUpdate.get(i);
+        for (int index : headerToUpdate) {
             InnerHeaderInfo innerheaderInfo = innerHeaderInfos.get(index);
-            if (updateHeaderInfo(Integer.parseInt(innerheaderInfo.getId()), innerheaderInfo.getAliasName(),
-                    innerheaderInfo.getFieldDes()) == false) {
+            if (!updateHeaderInfo(Integer.parseInt(innerheaderInfo.getId()), innerheaderInfo.getAliasName(),
+                    innerheaderInfo.getFieldDes())) {
                 return false;
             }
-            ;
-
         }
         return true;
     }
@@ -240,7 +225,7 @@ public class FileService {
      */
     public HashMap<String, HashMap<String, Object>> getFieldDistribution(String[] fieldIds) throws Exception {
         try {
-            val res = fileAnalysis.getFieldDistribution(fieldIds);
+            HashMap<String,HashMap<String,Object>> res = fileAnalysis.getFieldDistribution(fieldIds);
             return res;
         } catch (Exception e) {
             log.error("file analysis error");
@@ -257,7 +242,7 @@ public class FileService {
     public Object getFieldAnalysis(String[] fieldIds, int funcId) throws Exception {
         try {
 
-            if (fieldIds == null || fieldIds.length < 0 || funcId == 0) {
+            if (fieldIds == null || fieldIds.length == 0 || funcId == 0) {
                 return null;
             }
             String funcName = funcInfoDao.findById(funcId).getFuncName();
@@ -266,19 +251,19 @@ public class FileService {
             }
             switch (funcName) {
                 case "数据分布":
-                    val res_1 = fileAnalysis.getFieldDistribution(fieldIds);
+                    HashMap<String, HashMap<String, Object>> res_1 = fileAnalysis.getFieldDistribution(fieldIds);
                     return res_1;
                 case "散点图":
-                    val res_2 = fileAnalysis.getFieldScatterDiagram(fieldIds);
+                    HashMap<String,ArrayList<Object>> res_2 = fileAnalysis.getFieldScatterDiagram(fieldIds);
                     return res_2;
                 case "数据统计":
-                    val res_3 = fileAnalysis.getFieldDescription(fieldIds);
+                    HashMap<String, HashMap<String, Object>> res_3 = fileAnalysis.getFieldDescription(fieldIds);
                     return res_3;
                 case "偏度-峰度":
-                    val res_4 = fileAnalysis.getFieldSkewnessAndKurtosis(fieldIds);
+                    HashMap<String, HashMap<String, Object>> res_4 = fileAnalysis.getFieldSkewnessAndKurtosis(fieldIds);
                     return res_4;
                 case "皮尔森相关系数":
-                    val res_5 = fileAnalysis.getFieldCorr(fieldIds);
+                    HashMap<String, Object> res_5 = fileAnalysis.getFieldCorr(fieldIds);
                     return res_5;
                 default:
                     break;
@@ -292,7 +277,7 @@ public class FileService {
 
     public Object getFieldClassificationAnalysis(int fieldId, String[] fieldIds, int funcId) throws Exception {
         try {
-            if (fieldId == 0 || fieldIds == null || fieldIds.length < 0 || funcId == 0) {
+            if (fieldId == 0 || fieldIds == null || fieldIds.length == 0 || funcId == 0) {
                 return null;
             }
             String funcName = funcInfoDao.findById(funcId).getFuncName();
@@ -301,7 +286,7 @@ public class FileService {
             }
             switch (funcName) {
                 case "平均值":
-                    val res_1 = fileAnalysis.getFieldClassificationAverage(fieldId,fieldIds);
+                    HashMap<String, HashMap<String, Object>> res_1 = fileAnalysis.getFieldClassificationAverage(fieldId,fieldIds);
                     return res_1;
                 default:
                     break;
@@ -312,5 +297,4 @@ public class FileService {
             throw e;
         }
     }
-
 }

+ 20 - 39
java_compile/src/main/java/com/example/services/PictureService.java

@@ -12,16 +12,13 @@ import com.example.services.pojo.PicCategoryPojo;
 import com.example.services.pojo.PicFilePojo;
 import com.example.services.pojo.PicInfoPojo;
 import com.example.util.FileHelper;
-import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
-import lombok.val;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
 import java.util.*;
-import java.util.concurrent.locks.ReentrantLock;
 import java.util.stream.Collectors;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
@@ -30,8 +27,7 @@ import java.util.zip.ZipFile;
  * Picture-related service
  */
 @Slf4j
-@Component
-@Data
+@Service
 public class PictureService {
     @Autowired private CsvLoader csvLoader;
     @Autowired private CsvAdapter csvAdapter;
@@ -76,17 +72,11 @@ public class PictureService {
                e.printStackTrace();
                 return false;
             }
-            System.out.println(1233);
             new Thread(new MyThread(file.getAbsolutePath(), userId, picAbsolutePath)).start();
-            //if(parseZipFile(file.getAbsolutePath(), userId, picAbsolutePath))
-                //return true;
-            System.out.println(222);
-
             return true;
         }else{
             return false;
         }
-
     }
 
 
@@ -99,28 +89,31 @@ public class PictureService {
         }
         File pathFile = new File(descDir);
         if(!pathFile.exists()){
-            pathFile.mkdirs();
+            if (!pathFile.mkdirs()){
+                return false;
+            }
         }
         File zipFile = new File(descDir+filename);
         ZipFile zip = new ZipFile(zipFile);
 
         String picFile = null;
-        int countNum = 0;
         Set<String> hs = new HashSet<>();
         PicCategory picCategory = null;
         PicFile picFileId = null;
         List<PicInfo> picInfos = new ArrayList<>();
 
 
-        for(Enumeration enumeration = zip.entries(); enumeration.hasMoreElements();){
-            ZipEntry entry = (ZipEntry)enumeration.nextElement();
+        for(Enumeration<? extends ZipEntry> enumeration = zip.entries(); enumeration.hasMoreElements();){
+            ZipEntry entry = enumeration.nextElement();
             String zipEntryName = entry.getName();
             InputStream in = zip.getInputStream(entry);
             String outPath = (descDir + zipEntryName).replaceAll("\\*", "/");
             //判断路径是否存在,不存在则创建文件路径
             File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
             if(!file.exists()){
-                file.mkdirs();
+                if (!file.mkdirs()){
+                    return false;
+                }
             }
             //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
             if(new File(outPath).isDirectory()){
@@ -152,7 +145,7 @@ public class PictureService {
             in.close();
             out.close();
         }
-        picInfos.stream().forEach(picInfo -> pictureInfoDao.save(picInfo));
+        picInfos.forEach(picInfo -> pictureInfoDao.save(picInfo));
         //("******************解压完毕********************");
         long endTime=System.currentTimeMillis();
         System.out.println(endTime - startTime);
@@ -160,28 +153,22 @@ public class PictureService {
     }
 
     public List<PicFilePojo> getAllProj(int userId){
-        List<PicFilePojo> res = new ArrayList<>();
         List<PicFile> picFiles = pictureFileDao.findByUserId(userId);
-        res = picFiles.stream().map((picInfo) -> new PicFilePojo(String.valueOf(picInfo.getId()), picInfo.getFileName()))
+        List<PicFilePojo> res = picFiles.stream().map((picInfo) -> new PicFilePojo(String.valueOf(picInfo.getId()), picInfo.getFileName()))
                 .collect(Collectors.toList());
         return res;
     }
 
     public List<PicCategoryPojo> getOneProj(int picFileId){
-        List<PicCategoryPojo> res = new ArrayList<>();
-        List<PicCategory> picCategorys = pictureCategoryDao.findByPicFileId(picFileId);
-        res = picCategorys.stream().map((picInfo) -> new PicCategoryPojo(String.valueOf(picInfo.getId()), picInfo.getCategoryName()))
+        List<PicCategory> picCategories = pictureCategoryDao.findByPicFileId(picFileId);
+        List<PicCategoryPojo> res = picCategories.stream().map((picInfo) -> new PicCategoryPojo(String.valueOf(picInfo.getId()), picInfo.getCategoryName()))
                 .collect(Collectors.toList());
         return res;
-
     }
 
     public List<PicInfoPojo> getAllPicAddr(int picTag){
-        List<PicInfoPojo> res = new ArrayList<>();
         List<PicInfo> picInfos = pictureInfoDao.findByCategoryId(picTag);
-
-
-        res = picInfos.stream().map((picInfo) -> new PicInfoPojo(String.valueOf(picInfo.getId()), picInfo.getPicname(),picInfo.getLocation()))
+        List<PicInfoPojo> res = picInfos.stream().map((picInfo) -> new PicInfoPojo(String.valueOf(picInfo.getId()), picInfo.getPicname(),picInfo.getLocation()))
                 .collect(Collectors.toList());
         return res;
     }
@@ -193,24 +180,18 @@ public class PictureService {
         //modelOfGet.setModel(model);
         picInfo.setCategoryId(picCategoryId);
         picInfo.setTag(picTag);
-        val pic = pictureInfoDao.save(picInfo);
-        if(pic!=null){
-            return true;
-        }
-        return false;
+        pictureInfoDao.save(picInfo);
+        return true;
     }
 
     public class MyThread implements Runnable{
-
-        private String fileAbsolutePath;
-        private int userId;
-        private String picAbsolutePath;
+        private final String fileAbsolutePath;
+        private final int userId;
+        private final String picAbsolutePath;
         public MyThread(String fileAbsolutePath, int userId, String picAbsolutePath){
             this.fileAbsolutePath = fileAbsolutePath;
             this.userId = userId;
             this.picAbsolutePath = picAbsolutePath;
-
-
         }
         @Override
         public void run(){