Skip to content

Commit b51a1cc

Browse files
committed
- Fix for unzipping files
- Change the table name from random to time-based - Fix for process command - Check whether the necessary files for the import are available after unzipping
1 parent e15a8cd commit b51a1cc

1 file changed

Lines changed: 34 additions & 16 deletions

File tree

src/main/webapp/java/de/htwb/shpImport/ShapeImporter.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import javax.servlet.http.Part;
44
import java.io.*;
5-
import java.nio.file.Files;
6-
import java.nio.file.Paths;
7-
import java.sql.SQLException;
5+
import java.time.LocalDateTime;
6+
import java.time.format.DateTimeFormatter;
87
import java.util.ArrayList;
8+
import java.util.List;
99
import java.util.Random;
1010
import java.util.zip.ZipEntry;
1111
import java.util.zip.ZipInputStream;
@@ -34,10 +34,10 @@ public ShapeImporter()
3434

3535
public String importFile(File zipFile, String userName) throws Exception
3636
{
37-
unzipFile(zipFile);
37+
List unzipFiles = unzipFile(zipFile);
38+
String shapeFilesPath = checkShapeFilesAndReturnFilepath(unzipFiles);
3839

39-
40-
File sqlFile = createSQLImportFile(zipFile);
40+
File sqlFile = createSQLImportFile(shapeFilesPath);
4141
if(sqlFile == null)
4242
return null;
4343

@@ -87,16 +87,12 @@ public File saveUploadedPartToFile(Part part) throws IOException
8787
return targetFile;
8888
}
8989

90-
private File createSQLImportFile(File zipFile) throws IOException
90+
private File createSQLImportFile(String path) throws IOException
9191
{
92-
long milliseconds = System.currentTimeMillis();
93-
Random rnd = new Random();
94-
int rndNumber = Math.abs(rnd.nextInt());
92+
String tableName = DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss").format(LocalDateTime.now());
9593

96-
String tableName = String.format("%d%d", rndNumber, milliseconds);
97-
String shpImportCmd = String.format("\"%s\" \"%s\" \"temp\".\"%s\"", SHP_TO_PGSQL_FILE_PATH, zipFile.getAbsolutePath(), tableName);
94+
Process pr = new ProcessBuilder(SHP_TO_PGSQL_FILE_PATH, path, SCHEME_TEMP +"."+tableName).start();
9895

99-
Process pr = new ProcessBuilder(shpImportCmd).start();
10096
File importFile;
10197

10298
try(BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream())))
@@ -133,8 +129,7 @@ private File createSQLImportFile(File zipFile) throws IOException
133129

134130
private void importShapeIntoDb(File importFile) throws Exception
135131
{
136-
String sqlImportCommand = String.format("\"%s\" -d \"%s\" -U \"%s\" -h \"%s\" -f \"%s\"", PGSQL_FILE_PATH, "ohdm_test", "geoserver", "ohdm.f4.htw-berlin.de", importFile.getAbsolutePath());
137-
Process pr = new ProcessBuilder(sqlImportCommand).start();
132+
Process pr = new ProcessBuilder(PGSQL_FILE_PATH, "-d", DB_NAME,"-U", DB_USER, "-h", DB_HOST, "-f", importFile.getAbsolutePath()).start();
138133

139134
pr.waitFor();
140135
try(BufferedReader brErr = new BufferedReader(new InputStreamReader(pr.getErrorStream())))
@@ -147,14 +142,16 @@ private void importShapeIntoDb(File importFile) throws Exception
147142
}
148143
}
149144

150-
private void unzipFile(File zipFile) throws IOException
145+
private List unzipFile(File zipFile) throws IOException
151146
{
147+
List<File> unzippedFiles = new ArrayList<>();
152148
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
153149
ZipEntry zipEntry;
154150
byte[] buffer = new byte[BUFFER_SIZE];
155151
while((zipEntry = zis.getNextEntry()) != null)
156152
{
157153
File newFile = new File(zipFile.getParent(), zipEntry.getName());
154+
unzippedFiles.add(newFile);
158155
try(FileOutputStream fops = new FileOutputStream(newFile))
159156
{
160157
int len;
@@ -167,6 +164,7 @@ private void unzipFile(File zipFile) throws IOException
167164

168165
zis.closeEntry();
169166
zis.close();
167+
return unzippedFiles;
170168
}
171169

172170
private void cleanUp(File zipFile, File sqlFile) throws Exception
@@ -176,4 +174,24 @@ private void cleanUp(File zipFile, File sqlFile) throws Exception
176174
FileUtils.forceDelete(sqlFile);
177175
dbRepos.disconnect();
178176
}
177+
178+
private String checkShapeFilesAndReturnFilepath(List<File> files) throws Exception {
179+
180+
List<String> filenames = new ArrayList<>();
181+
182+
for(File file : files){
183+
if(FilenameUtils.getExtension(file.getName()).equals("shp")){
184+
filenames.add(file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')));
185+
} else if(FilenameUtils.getExtension(file.getName()).equals("shx")) {
186+
filenames.add(file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')));
187+
} else if (FilenameUtils.getExtension(file.getName()).equals("dbf")) {
188+
filenames.add(file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')));
189+
}
190+
}
191+
if (filenames.size() == 3 && filenames.stream().distinct().count() == 1) {
192+
return filenames.get(0);
193+
} else {
194+
throw new Exception("The zip file does not contain the necessary shape files (.shp, .shx, .dbf), or they do not have the same name!");
195+
}
196+
}
179197
}

0 commit comments

Comments
 (0)