Commit 7c431a48 authored by Arild Trygve Aae's avatar Arild Trygve Aae

tmp versjon

parent 8f78a3da
Pipeline #23993 skipped with stages
......@@ -209,6 +209,29 @@
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
......
......@@ -3,7 +3,12 @@ import brsund_hent_data.jdbc.JdbcFellesService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.json.CDL;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
......@@ -11,8 +16,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.*;
import java.util.*;
import org.json.CDL;
import java.util.logging.Logger;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
......@@ -20,6 +28,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
......@@ -39,7 +48,7 @@ import org.springframework.web.client.RestTemplate;
hentOrganisasjonerListe();
System.exit(0);
}
void hentOrganisasjonerListe() throws Exception {
......@@ -48,50 +57,153 @@ import org.springframework.web.client.RestTemplate;
// private static final Logger logger = LoggerFactory.getLogger(JdbcFellesServiceImpl.class);
String fnr = "";
String sql = "";
String uri1 = "https://data.brreg.no/enhetsregisteret/api/enheter/";
String soek = "";
RestTemplate restTemplate = new RestTemplate();
List<Map<String, Object>> mapResult = null;
ObjectMapper objectMapper = new ObjectMapper();
//sql += " go ";
sql += "Select DISTINCT instkode, instnavn, fnr From dbo.ORG_INST WHERE (eierskap=2 or insttype = '93')";
sql += " AND gyld_til='99999'";
List<Map<String, Object>> data = null;
// System.out.println(" SQL: " + sql);
hentSelskapPrivate();
hentStudentsamskipnader();
hentOrgEidAvInst();
return;
}
private void hentSelskapPrivate() throws IOException {
String fileNameLevert = "C:\\Data\\selskap_private_levert_json.data";
String fileNameIkkeLevert = "C:\\Data\\selskap_private_Ikke_levert_json.data";
String fileNameFeil = "C:\\Data\\selskap_private_feil_json.data";
String sql = "Select DISTINCT instkode, instnavn, fnr From dbo.ORG_INST ";
sql += " WHERE eierskap=2";
sql += " AND gyld_til='99999'";
// System.out.println(" NNNNNNNNNNNNNNNNNNN 1 " + sql );
String uri1 = "https://data.brreg.no/enhetsregisteret/api/enheter/";
handleData(fileNameLevert,fileNameIkkeLevert, fileNameFeil,sql, uri1);
}
private void hentStudentsamskipnader() throws IOException {
String fileNameLevert = "C:\\Data\\selskap_studentsamskipander_levert_json.data";
String fileNameIkkeLevert = "C:\\Data\\selskap_studentsamskipander_Ikke_levert_json.data";
String fileNameFeil = "C:\\Data\\selskap_studentskip_feil.data";
String sql = "Select DISTINCT instkode, instnavn, fnr From dbo.ORG_INST ";
sql += "WHERE insttype = '93'";
sql += " AND gyld_til='99999'";
// System.out.println(" NNNNNNNNNNNNNNNNNNN 2 " + sql );
String uri1 = "https://data.brreg.no/enhetsregisteret/api/enheter/";
handleData(fileNameLevert,fileNameIkkeLevert,fileNameFeil, sql, uri1);
}
private void hentOrgEidAvInst() throws IOException {
String fileNameLevert = "C:\\Data\\selskap_org_eid_av_inst_levert_json.data";
String fileNameIkkeLevert = "C:\\Data\\selskap_org_eid_av_inst_Ikke_levert_json.data";
String fileNameFeil = "C:\\Data\\selskap_org_inst_eid_feil_json.data";
String sql = "Select DISTINCT NAMN_NO, Foretaksnr ";
sql += "From selskap..ORGEINING ";
sql += " where Foretaksnr is not null";
// System.out.println(" NNNNNNNNNNNNNNNNNNN 3 " + sql );
String uri1 = "https://data.brreg.no/enhetsregisteret/api/enheter/";
handleData(fileNameLevert,fileNameIkkeLevert,fileNameFeil, sql, uri1);
}
private void handleData(String fileNameLevert,String fileNameIkkeLevert, String fileNameFeil, String sql, String uri1) throws IOException {
String soek = "";
String fnr = "";
BufferedWriter writerLevert = new BufferedWriter(new FileWriter(fileNameLevert, false));
BufferedWriter writerIkkeLevert = new BufferedWriter(new FileWriter(fileNameIkkeLevert, false));
BufferedWriter writerFeilLogg = new BufferedWriter(new FileWriter(fileNameFeil, false));
RestTemplate restTemplate = new RestTemplate();
List<Map<String, Object>> mapResult = null;
ObjectMapper objectMapper = new ObjectMapper();
List<Map<String, Object>> data = null;
data = jdbcTemplate.queryForList(sql);
for (Map<String, Object> data1 : data) {
fnr = data1.get("fnr") .toString().trim();
if (data1.toString().contains("fnr")) {
fnr = data1.get("fnr").toString().trim();
}
if (data1.toString().contains("Foretaksnr")) {
fnr = data1.get("Foretaksnr").toString().trim();
}
fnr = fnr.replace(" ","");
soek = uri1 + fnr;
System.out.println(" Søker på: " + soek);
try {
String result = restTemplate.getForObject(soek, String.class);
String result = restTemplate.getForObject(soek, String.class); //Henter data
Map<String,String> resultMap = new HashMap<String,String>();
ObjectMapper mapperObj = new ObjectMapper();
System.out.println("Input Json: "+result);
//System.out.println("Input Json: "+result);
try {
JsonNode jsonNodeInn = objectMapper.valueToTree(result);
//
System.out.println("Output Map: "+ jsonNodeInn.asText());
} catch (Exception e) {
JSONObject resobj = new JSONObject(result);
Iterator<?> keys = resobj.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if (!key.equalsIgnoreCase("")) {
if (resobj.get(key) instanceof JSONObject) {
JSONObject xx = new JSONObject(resobj.get(key).toString());
} else {
// System.out.println(" key: " + key);
if (key.equalsIgnoreCase("sisteInnsendteAarsregnskap")) {
if (resobj.get(key).equals("2018")) {
skrivFil(writerLevert, resobj, fileNameLevert);
} else {
skrivFil(writerIkkeLevert, resobj, fileNameIkkeLevert);
}
}
}
}
}
} catch (HttpStatusCodeException exp) {
// TODO Auto-generated catch block
System.out.println (" BBBBBBBBBBBBBBBBBBBBBBBBB: " + result);
e.printStackTrace();
skrivFilException(writerFeilLogg, soek, fileNameFeil,exp);
// e.printStackTrace();
}
System.out.println ("Resultat av søk: " + result);
// System.out.println ("Resultat av søk: " + result);
} catch (Exception e) {
System.out.println(" Finner ikke: " + soek + " " + e.getMessage());
skrivFilException(writerFeilLogg, soek, fileNameFeil,e);
System.out.println(" Exception feil2: " + soek + " " + e.getMessage());
// e.printStackTrace();
}
}
}
return;
void skrivFil(BufferedWriter writer,JSONObject jsonObject, String filename) {
JSONObject output;
try {
writer.append(jsonObject.toString());
writer.newLine();
} catch (IOException e) {
System.out.println(" Feil ved skriv til fil: " + filename + " " + e.getMessage());
e.printStackTrace();
}
}
void skrivFilException(BufferedWriter writer2,String streng, String filename,Exception e) {
try {
streng = "Feil ved kontakt med Br.sund: " + streng;
System.out.println ("Hei 1 " + streng);
// writer2.append(streng);
// writer2.newLine();
FileWriter fstream = new FileWriter(filename,);
BufferedWriter out = new BufferedWriter(fstream);
PrintWriter pWriter = new PrintWriter(out, true);
e.printStackTrace(pWriter);
} catch (IOException ie) {
e.printStackTrace();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment