Mercurial > hg > openmind
annotate src/main/java/org/mpi/openmind/scripts/DivaImport.java @ 76:f04bd29d284d
renamed replaceSourceRelation() to replaceUniqueSourceRelation() to make intent clearer.
author | casties |
---|---|
date | Fri, 24 Feb 2017 20:22:23 +0100 |
parents | 615d27dce9b3 |
children |
rev | line source |
---|---|
1 | 1 package org.mpi.openmind.scripts; |
2 | |
3 import java.sql.DriverManager; | |
4 import java.sql.Connection; | |
5 import java.sql.ResultSet; | |
6 import java.sql.SQLException; | |
7 import java.sql.Statement; | |
8 import java.util.ArrayList; | |
9 import java.util.List; | |
10 | |
11 import org.apache.log4j.ConsoleAppender; | |
12 import org.apache.log4j.Level; | |
13 import org.apache.log4j.Logger; | |
14 import org.apache.log4j.PatternLayout; | |
15 import org.hibernate.mapping.Array; | |
16 import org.mpi.openmind.cache.WrapperService; | |
17 import org.mpi.openmind.repository.bo.Attribute; | |
18 import org.mpi.openmind.repository.bo.Entity; | |
19 import org.mpi.openmind.repository.bo.Node; | |
20 import org.mpi.openmind.repository.bo.Relation; | |
21 import org.mpi.openmind.repository.services.ServiceRegistry; | |
22 | |
23 import cl.maps.utils.AttKey; | |
24 | |
25 | |
26 public class DivaImport { | |
27 | |
28 static{ | |
29 ConsoleAppender console = new ConsoleAppender(); //create appender | |
30 //configure the appender | |
31 String PATTERN = "%d [%p|%c|%C{1}] %m%n"; | |
32 console.setLayout(new PatternLayout(PATTERN)); | |
33 console.setThreshold(Level.INFO); | |
34 console.activateOptions(); | |
35 //add appender to any Logger (here is root) | |
36 Logger.getRootLogger().addAppender(console); | |
37 } | |
38 | |
39 public static String DIGITALIZATION = "DIGITALIZATION"; | |
40 public static String userName = "diva-import"; | |
41 | |
42 public static void execute(){ | |
43 ServiceRegistry services = new ServiceRegistry(); | |
44 createDataModel(services.getWrapper()); | |
45 importData(services.getWrapper()); | |
46 } | |
47 | |
48 private static void createDataModel(WrapperService ontology){ | |
49 | |
50 try { | |
51 | |
52 Entity digi = new Entity(Node.TYPE_TBOX, Node.TYPE_TBOX, false); | |
53 digi.setOwnValue(DIGITALIZATION); | |
54 | |
55 digi = ontology.saveLWDefinition(digi, userName); | |
56 | |
57 Attribute attName = new Attribute(Node.TYPE_TBOX, "text", "name"); | |
58 attName.setSourceId(digi.getId()); | |
59 attName.setSourceObjectClass(Node.TYPE_TBOX); | |
60 attName.setSourceModif(digi.getModificationTime()); | |
61 attName.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
62 | |
63 ontology.saveDefAttribute(attName, userName); | |
64 | |
65 Attribute num_files = new Attribute(Node.TYPE_TBOX, "text", "num_files"); | |
66 num_files.setSourceId(digi.getId()); | |
67 num_files.setSourceObjectClass(Node.TYPE_TBOX); | |
68 num_files.setSourceModif(digi.getModificationTime()); | |
69 num_files.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
70 | |
71 ontology.saveDefAttribute(num_files, userName); | |
72 | |
73 | |
74 //DIGI is_digitalization_of CODEX | |
75 Entity codex = ontology.getDefinition("CODEX"); | |
76 Relation rel = new Relation(digi, codex, "is_digitalization_of"); | |
77 | |
78 ontology.saveDefRelation(rel, userName); | |
79 | |
80 //----------- | |
81 Entity witness = ontology.getDefinition("WITNESS"); | |
82 | |
83 Attribute end_page = new Attribute(Node.TYPE_TBOX, "text", "end_page"); | |
84 end_page.setSourceId(witness.getId()); | |
85 end_page.setSourceObjectClass(Node.TYPE_TBOX); | |
86 end_page.setSourceModif(witness.getModificationTime()); | |
87 end_page.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
88 | |
89 ontology.saveDefAttribute(end_page, userName); | |
90 | |
91 Attribute start_page = new Attribute(Node.TYPE_TBOX, "text", "start_page"); | |
92 start_page.setSourceId(witness.getId()); | |
93 start_page.setSourceObjectClass(Node.TYPE_TBOX); | |
94 start_page.setSourceModif(witness.getModificationTime()); | |
95 start_page.setSystemStatus(Node.SYS_STATUS_CURRENT_VERSION); | |
96 | |
97 ontology.saveDefAttribute(start_page, userName); | |
98 | |
99 | |
100 } catch (Exception e) { | |
101 e.printStackTrace(); | |
102 } | |
103 | |
104 | |
105 } | |
106 | |
107 private static void importData(WrapperService ontology){ | |
108 try { | |
109 | |
110 Class.forName("org.postgresql.Driver"); | |
111 Connection conn = null; | |
112 conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/diva","jurzua", "221082"); | |
113 | |
114 String sql = "SELECT * FROM imageserve_manuscript"; | |
115 | |
116 Statement stmt = conn.createStatement(); | |
117 ResultSet rs = stmt.executeQuery(sql); | |
118 int countNoCodex = 0; | |
119 int countYesCodex = 0; | |
120 int countIdCodex = 0; | |
121 List<String> noCodexList = new ArrayList<String>(); | |
122 | |
123 List<Entity> list = new ArrayList<Entity>(); | |
124 while(rs.next()){ | |
125 String directory = rs.getString("directory"); | |
126 Long ismi_id = rs.getLong("ismi_id"); | |
127 Integer numFiles = rs.getInt("num_files"); | |
128 Boolean hasFolioNums = rs.getBoolean("has_folio_nums"); | |
129 String folioPgs = rs.getString("folio_pgs"); | |
130 | |
131 String ov = directory.replace("/data7/srv/images/", ""); | |
132 | |
133 Entity digi = new Entity(Node.TYPE_ABOX, DIGITALIZATION, false); | |
134 digi.setOwnValue(ov); | |
135 | |
136 digi.addAttribute(new Attribute("name", "text", ov)); | |
137 digi.addAttribute(new Attribute("num_files", "text", numFiles + "")); | |
138 | |
139 | |
140 if(ismi_id != null && ismi_id != 0){ | |
141 Entity codex = ontology.getEntityByIdWithContent(ismi_id); | |
76
f04bd29d284d
renamed replaceSourceRelation() to replaceUniqueSourceRelation() to make intent clearer.
casties
parents:
1
diff
changeset
|
142 digi.replaceUniqueSourceRelation(codex, "CODEX", "is_digitalization_of"); |
1 | 143 countYesCodex++; |
144 }else{ | |
145 List<Entity> list0 = ontology.getEntityByDefAndOW("CODEX", ov, 1); | |
146 if(list0.size() > 0){ | |
147 Entity codex = ontology.getEntityByIdWithContent(list0.get(0).getId()); | |
76
f04bd29d284d
renamed replaceSourceRelation() to replaceUniqueSourceRelation() to make intent clearer.
casties
parents:
1
diff
changeset
|
148 digi.replaceUniqueSourceRelation(codex, "CODEX", "is_digitalization_of"); |
1 | 149 countIdCodex++; |
150 }else{ | |
151 noCodexList.add(ov); | |
152 countNoCodex++; | |
153 } | |
154 } | |
155 | |
156 list.add(digi); | |
157 } | |
158 | |
159 | |
160 conn.close(); | |
161 | |
162 ontology.saveEntityList(list, userName); | |
163 | |
164 System.out.println("countNoCodex: " + countNoCodex); | |
165 System.out.println("countYesCodex: " + countYesCodex); | |
166 System.out.println("countIdCodex: " + countIdCodex); | |
167 System.out.println("list.size(): " + list.size()); | |
168 System.out.println("END"); | |
169 | |
170 } catch (Exception e) { | |
171 e.printStackTrace(); | |
172 } | |
173 } | |
174 | |
175 | |
176 public static void main(String[] args){ | |
177 execute(); | |
178 System.exit(0); | |
179 } | |
180 } |