Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
RdbmsCentralRepoFactory.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.centralrepository.datamodel;
20 
21 import java.sql.Connection;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.util.List;
27 import java.util.logging.Level;
30 import static org.sleuthkit.autopsy.centralrepository.datamodel.RdbmsCentralRepo.SOFTWARE_CR_DB_SCHEMA_VERSION;
32 import org.sleuthkit.datamodel.Account;
33 
39 
40  private final static Logger LOGGER = Logger.getLogger(RdbmsCentralRepoFactory.class.getName());
41 
42 
46 
47 
48  // SQLite pragmas
49  private final static String PRAGMA_SYNC_OFF = "PRAGMA synchronous = OFF";
50  private final static String PRAGMA_JOURNAL_WAL = "PRAGMA journal_mode = WAL";
51  private final static String PRAGMA_READ_UNCOMMITTED_TRUE = "PRAGMA read_uncommitted = True";
52  private final static String PRAGMA_ENCODING_UTF8 = "PRAGMA encoding = 'UTF-8'";
53  private final static String PRAGMA_PAGE_SIZE_4096 = "PRAGMA page_size = 4096";
54  private final static String PRAGMA_FOREIGN_KEYS_ON = "PRAGMA foreign_keys = ON";
55 
56 
57 
59  this.selectedPlatform = selectedPlatform;
60  this.sqliteCentralRepoSettings = repoSettings;
61  this.postgresCentralRepoSettings = null;
62 
63  }
64 
66  this.selectedPlatform = selectedPlatform;
67  this.postgresCentralRepoSettings = repoSettings;
68  this.sqliteCentralRepoSettings = null;
69  }
70 
71 
83  public boolean initializeDatabaseSchema() {
84 
85  String createArtifactInstancesTableTemplate = getCreateArtifactInstancesTableTemplate(selectedPlatform);
86  String createAccountInstancesTableTemplate = getCreateAccountInstancesTableTemplate(selectedPlatform);
87 
88  String instancesCaseIdIdx = getAddCaseIdIndexTemplate();
89  String instancesDatasourceIdIdx = getAddDataSourceIdIndexTemplate();
90  String instancesValueIdx = getAddValueIndexTemplate();
91  String instancesKnownStatusIdx = getAddKnownStatusIndexTemplate();
92  String instancesObjectIdIdx = getAddObjectIdIndexTemplate();
93 
94  // NOTE: the db_info table currenly only has 1 row, so having an index
95  // provides no benefit.
96  try (Connection conn = this.getEphemeralConnection();) {
97 
98  if (null == conn) {
99  LOGGER.log(Level.SEVERE, "Cannot initialize CR database, don't have a valid connection."); // NON-NLS
100  return false;
101  }
102 
103  try (Statement stmt = conn.createStatement();) {
104 
105  // these setting PRAGMAs are SQLIte spcific
106  if (selectedPlatform == CentralRepoPlatforms.SQLITE) {
107  stmt.execute(PRAGMA_JOURNAL_WAL);
108  stmt.execute(PRAGMA_SYNC_OFF);
109  stmt.execute(PRAGMA_READ_UNCOMMITTED_TRUE);
110  stmt.execute(PRAGMA_ENCODING_UTF8);
111  stmt.execute(PRAGMA_PAGE_SIZE_4096);
112  stmt.execute(PRAGMA_FOREIGN_KEYS_ON);
113  }
114 
115  // Create Organizations table
116  stmt.execute(getCreateOrganizationsTableStatement(selectedPlatform));
117 
118  // Create Cases table and indexes
119  stmt.execute(getCreateCasesTableStatement(selectedPlatform));
120  stmt.execute(getCasesOrgIdIndexStatement());
121  stmt.execute(getCasesCaseUidIndexStatement());
122 
123  stmt.execute(getCreateDataSourcesTableStatement(selectedPlatform));
124  stmt.execute(getAddDataSourcesNameIndexStatement());
125  stmt.execute(getAddDataSourcesObjectIdIndexStatement());
126 
127  stmt.execute(getCreateReferenceSetsTableStatement(selectedPlatform));
128  stmt.execute(getReferenceSetsOrgIdIndexTemplate());
129 
130  stmt.execute(getCreateCorrelationTypesTableStatement(selectedPlatform));
131 
132  stmt.execute(getCreateDbInfoTableStatement(selectedPlatform));
133  stmt.execute("INSERT INTO db_info (name, value) VALUES ('" + RdbmsCentralRepo.SCHEMA_MAJOR_VERSION_KEY + "', '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMajor() + "')");
134  stmt.execute("INSERT INTO db_info (name, value) VALUES ('" + RdbmsCentralRepo.SCHEMA_MINOR_VERSION_KEY + "', '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMinor() + "')");
135  stmt.execute("INSERT INTO db_info (name, value) VALUES ('" + RdbmsCentralRepo.CREATION_SCHEMA_MAJOR_VERSION_KEY + "', '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMajor() + "')");
136  stmt.execute("INSERT INTO db_info (name, value) VALUES ('" + RdbmsCentralRepo.CREATION_SCHEMA_MINOR_VERSION_KEY + "', '" + SOFTWARE_CR_DB_SCHEMA_VERSION.getMinor() + "')");
137 
138  // Create account_types and accounts tables which are referred by X_instances tables
139  stmt.execute(getCreateAccountTypesTableStatement(selectedPlatform));
140  stmt.execute(getCreateAccountsTableStatement(selectedPlatform));
141 
142  // Create a separate instance and reference table for each artifact type
144 
145  String reference_type_dbname;
146  String instance_type_dbname;
147  for (CorrelationAttributeInstance.Type type : defaultCorrelationTypes) {
148  reference_type_dbname = CentralRepoDbUtil.correlationTypeToReferenceTableName(type);
149  instance_type_dbname = CentralRepoDbUtil.correlationTypeToInstanceTableName(type);
150 
151  // use the correct create table template, based on whether the attribute type represents an account or not.
152  String createTableTemplate = (CentralRepoDbUtil.correlationAttribHasAnAccount(type))
153  ? createAccountInstancesTableTemplate
154  : createArtifactInstancesTableTemplate;
155 
156  stmt.execute(String.format(createTableTemplate, instance_type_dbname, instance_type_dbname));
157 
158  stmt.execute(String.format(instancesCaseIdIdx, instance_type_dbname, instance_type_dbname));
159  stmt.execute(String.format(instancesDatasourceIdIdx, instance_type_dbname, instance_type_dbname));
160  stmt.execute(String.format(instancesValueIdx, instance_type_dbname, instance_type_dbname));
161  stmt.execute(String.format(instancesKnownStatusIdx, instance_type_dbname, instance_type_dbname));
162  stmt.execute(String.format(instancesObjectIdIdx, instance_type_dbname, instance_type_dbname));
163 
164  // FUTURE: allow more than the FILES type
165  if (type.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
166  stmt.execute(String.format(getReferenceTypesTableTemplate(selectedPlatform), reference_type_dbname, reference_type_dbname));
167  stmt.execute(String.format(getReferenceTypeValueIndexTemplate(), reference_type_dbname, reference_type_dbname));
168  stmt.execute(String.format(getReferenceTypeValueKnownstatusIndexTemplate(), reference_type_dbname, reference_type_dbname));
169  }
170  }
171  // @TODO: uncomment this when ready to create Persona tables.
172  //createPersonaTables(stmt);
173  } catch (SQLException ex) {
174  LOGGER.log(Level.SEVERE, "Error initializing db schema.", ex); // NON-NLS
175  return false;
176  } catch (CentralRepoException ex) {
177  LOGGER.log(Level.SEVERE, "Error getting default correlation types. Likely due to one or more Type's with an invalid db table name."); // NON-NLS
178  return false;
179  }
180  } catch (SQLException ex) {
181  LOGGER.log(Level.SEVERE, "Error connecting to database.", ex); // NON-NLS
182  return false;
183  }
184 
185  return true;
186  }
187 
193  public boolean insertDefaultDatabaseContent() {
194 
195  boolean result;
196  try (Connection conn = this.getEphemeralConnection();) {
197  if (null == conn) {
198  return false;
199  }
200 
202  && CentralRepoDbUtil.insertDefaultOrganization(conn) &&
203  RdbmsCentralRepoFactory.insertDefaultAccountsTablesContent(conn, selectedPlatform );
204  // @TODO: uncomment when ready to create/populate persona tables
205  // && insertDefaultPersonaTablesContent(conn);
206 
207  } catch (SQLException ex) {
208  LOGGER.log(Level.SEVERE, String.format("Failed to populate default data in CR tables."), ex);
209  return false;
210  }
211 
212  return result;
213  }
214 
215  private static String getCreateDbInfoTableStatement(CentralRepoPlatforms selectedPlatform) {
216  /*
217  * Note that the essentially useless id column in the following
218  * table is required for backwards compatibility. Otherwise, the
219  * name column could be the primary key.
220  */
221 
222  return "CREATE TABLE db_info ("
223  + getNumericPrimaryKeyClause("id", selectedPlatform)
224  + "name TEXT UNIQUE NOT NULL,"
225  + "value TEXT NOT NULL "
226  + ")";
227 
228  }
236  private static String getCreateOrganizationsTableStatement(CentralRepoPlatforms selectedPlatform) {
237  // The "id" column is an alias for the built-in 64-bit int "rowid" column.
238  // It is autoincrementing by default and must be of type "integer primary key".
239  // We've omitted the autoincrement argument because we are not currently
240  // using the id value to search for specific rows, so we do not care
241  // if a rowid is re-used after an existing rows was previously deleted.
242 
243  return "CREATE TABLE IF NOT EXISTS organizations ("
244  + getNumericPrimaryKeyClause("id", selectedPlatform)
245  + "org_name text NOT NULL,"
246  + "poc_name text NOT NULL,"
247  + "poc_email text NOT NULL,"
248  + "poc_phone text NOT NULL,"
249  + "CONSTRAINT org_name_unique UNIQUE (org_name)"
250  + ")";
251  }
252 
260  private static String getCreateCasesTableStatement(CentralRepoPlatforms selectedPlatform) {
261 
262  return ("CREATE TABLE IF NOT EXISTS cases (")
263  + getNumericPrimaryKeyClause("id", selectedPlatform)
264  + "case_uid text NOT NULL,"
265  + "org_id integer,"
266  + "case_name text NOT NULL,"
267  + "creation_date text NOT NULL,"
268  + "case_number text,"
269  + "examiner_name text,"
270  + "examiner_email text,"
271  + "examiner_phone text,"
272  + "notes text,"
273  + "foreign key (org_id) references organizations(id) ON UPDATE SET NULL ON DELETE SET NULL,"
274  + "CONSTRAINT case_uid_unique UNIQUE(case_uid)" + getOnConflictIgnoreClause(selectedPlatform)
275  + ")";
276  }
277 
278  private static String getCasesOrgIdIndexStatement() {
279  return "CREATE INDEX IF NOT EXISTS cases_org_id ON cases (org_id)";
280  }
281 
282  private static String getCasesCaseUidIndexStatement() {
283  return "CREATE INDEX IF NOT EXISTS cases_case_uid ON cases (case_uid)";
284  }
285 
286  private static String getCreateReferenceSetsTableStatement(CentralRepoPlatforms selectedPlatform) {
287 
288  return "CREATE TABLE IF NOT EXISTS reference_sets ("
289  + getNumericPrimaryKeyClause("id", selectedPlatform)
290  + "org_id integer NOT NULL,"
291  + "set_name text NOT NULL,"
292  + "version text NOT NULL,"
293  + "known_status integer NOT NULL,"
294  + "read_only boolean NOT NULL,"
295  + "type integer NOT NULL,"
296  + "import_date text NOT NULL,"
297  + "foreign key (org_id) references organizations(id) ON UPDATE SET NULL ON DELETE SET NULL,"
298  + "CONSTRAINT hash_set_unique UNIQUE (set_name, version)"
299  + ")";
300 
301  }
302 
307  private static String getReferenceSetsOrgIdIndexTemplate() {
308  return "CREATE INDEX IF NOT EXISTS reference_sets_org_id ON reference_sets (org_id)";
309  }
310 
318  private static String getReferenceTypesTableTemplate(CentralRepoPlatforms selectedPlatform) {
319  // Each "%s" will be replaced with the relevant reference_TYPE table name.
320 
321  return "CREATE TABLE IF NOT EXISTS %s ("
322  + getNumericPrimaryKeyClause("id", selectedPlatform)
323  + "reference_set_id integer,"
324  + "value text NOT NULL,"
325  + "known_status integer NOT NULL,"
326  + "comment text,"
327  + "CONSTRAINT %s_multi_unique UNIQUE(reference_set_id, value)" + getOnConflictIgnoreClause(selectedPlatform) + ","
328  + "foreign key (reference_set_id) references reference_sets(id) ON UPDATE SET NULL ON DELETE SET NULL"
329  + ")";
330  }
331 
336  private static String getReferenceTypeValueIndexTemplate() {
337  return "CREATE INDEX IF NOT EXISTS %s_value ON %s (value)";
338  }
339 
345  return "CREATE INDEX IF NOT EXISTS %s_value_known_status ON %s (value, known_status)";
346  }
347 
355  private static String getCreateCorrelationTypesTableStatement(CentralRepoPlatforms selectedPlatform) {
356 
357  return "CREATE TABLE IF NOT EXISTS correlation_types ("
358  + getNumericPrimaryKeyClause("id", selectedPlatform)
359  + "display_name text NOT NULL,"
360  + "db_table_name text NOT NULL,"
361  + "supported integer NOT NULL,"
362  + "enabled integer NOT NULL,"
363  + "CONSTRAINT correlation_types_names UNIQUE (display_name, db_table_name)"
364  + ")";
365  }
373  static String getCreateArtifactInstancesTableTemplate(CentralRepoPlatforms selectedPlatform) {
374  // Each "%s" will be replaced with the relevant TYPE_instances table name.
375 
376  return "CREATE TABLE IF NOT EXISTS %s ("
377  + getNumericPrimaryKeyClause("id", selectedPlatform)
378  + "case_id integer NOT NULL,"
379  + "data_source_id integer NOT NULL,"
380  + "value text NOT NULL,"
381  + "file_path text NOT NULL,"
382  + "known_status integer NOT NULL,"
383  + "comment text,"
384  + "file_obj_id " + getBigIntType(selectedPlatform) + " ,"
385  + "CONSTRAINT %s_multi_unique UNIQUE(data_source_id, value, file_path)" + getOnConflictIgnoreClause(selectedPlatform) + ","
386  + "foreign key (case_id) references cases(id) ON UPDATE SET NULL ON DELETE SET NULL,"
387  + "foreign key (data_source_id) references data_sources(id) ON UPDATE SET NULL ON DELETE SET NULL)";
388  }
389 
397  static String getCreateAccountInstancesTableTemplate(CentralRepoPlatforms selectedPlatform) {
398  // Each "%s" will be replaced with the relevant TYPE_instances table name.
399 
400  return "CREATE TABLE IF NOT EXISTS %s ("
401  + getNumericPrimaryKeyClause("id", selectedPlatform)
402  + "case_id integer NOT NULL,"
403  + "data_source_id integer NOT NULL,"
404  + "account_id " + getBigIntType(selectedPlatform) + " DEFAULT NULL,"
405  + "value text NOT NULL,"
406  + "file_path text NOT NULL,"
407  + "known_status integer NOT NULL,"
408  + "comment text,"
409  + "file_obj_id " + getBigIntType(selectedPlatform) + " ,"
410  + "CONSTRAINT %s_multi_unique UNIQUE(data_source_id, value, file_path)" + getOnConflictIgnoreClause(selectedPlatform) + ","
411  + "foreign key (account_id) references accounts(id),"
412  + "foreign key (case_id) references cases(id) ON UPDATE SET NULL ON DELETE SET NULL,"
413  + "foreign key (data_source_id) references data_sources(id) ON UPDATE SET NULL ON DELETE SET NULL)";
414  }
415 
423  static String getCreateDataSourcesTableStatement(CentralRepoPlatforms selectedPlatform) {
424  return "CREATE TABLE IF NOT EXISTS data_sources ("
425  + getNumericPrimaryKeyClause("id", selectedPlatform)
426  + "case_id integer NOT NULL,"
427  + "device_id text NOT NULL,"
428  + "name text NOT NULL,"
429  + "datasource_obj_id " + getBigIntType(selectedPlatform) + " ,"
430  + "md5 text DEFAULT NULL,"
431  + "sha1 text DEFAULT NULL,"
432  + "sha256 text DEFAULT NULL,"
433  + "foreign key (case_id) references cases(id) ON UPDATE SET NULL ON DELETE SET NULL,"
434  + "CONSTRAINT datasource_unique UNIQUE (case_id, datasource_obj_id))";
435  }
436 
445  static String getAddCaseIdIndexTemplate() {
446  // Each "%s" will be replaced with the relevant TYPE_instances table name.
447  return "CREATE INDEX IF NOT EXISTS %s_case_id ON %s (case_id)";
448  }
449 
458  static String getAddDataSourceIdIndexTemplate() {
459  // Each "%s" will be replaced with the relevant TYPE_instances table name.
460  return "CREATE INDEX IF NOT EXISTS %s_data_source_id ON %s (data_source_id)";
461  }
462 
471  static String getAddValueIndexTemplate() {
472  // Each "%s" will be replaced with the relevant TYPE_instances table name.
473  return "CREATE INDEX IF NOT EXISTS %s_value ON %s (value)";
474  }
475 
484  static String getAddKnownStatusIndexTemplate() {
485  // Each "%s" will be replaced with the relevant TYPE_instances table name.
486  return "CREATE INDEX IF NOT EXISTS %s_value_known_status ON %s (value, known_status)";
487  }
488 
497  static String getAddObjectIdIndexTemplate() {
498  // Each "%s" will be replaced with the relevant TYPE_instances table name.
499  return "CREATE INDEX IF NOT EXISTS %s_file_obj_id ON %s (file_obj_id)";
500  }
501 
509  static String getAddDataSourcesNameIndexStatement() {
510  return "CREATE INDEX IF NOT EXISTS data_sources_name ON data_sources (name)";
511  }
512 
520  static String getAddDataSourcesObjectIdIndexStatement() {
521  return "CREATE INDEX IF NOT EXISTS data_sources_object_id ON data_sources (datasource_obj_id)";
522  }
523 
533  private static String getNumericPrimaryKeyClause(String pkName, CentralRepoPlatforms selectedPlatform) {
534  switch (selectedPlatform) {
535  case POSTGRESQL:
536  return String.format(" %s SERIAL PRIMARY KEY, ", pkName);
537  case SQLITE:
538  return String.format(" %s integer primary key autoincrement NOT NULL ,", pkName);
539  default:
540  return "";
541  }
542 
543  }
544 
551  private static String getOnConflictIgnoreClause(CentralRepoPlatforms selectedPlatform) {
552  switch (selectedPlatform) {
553  case POSTGRESQL:
554  return "";
555  case SQLITE:
556  return " ON CONFLICT IGNORE ";
557  default:
558  return "";
559  }
560  }
561 
568  static String getBigIntType(CentralRepoPlatforms selectedPlatform) {
569  switch (selectedPlatform) {
570  case POSTGRESQL:
571  return " BIGINT ";
572  case SQLITE:
573  return " INTEGER ";
574  default:
575  return "";
576  }
577  }
578 
579  private static String getOnConflictDoNothingClause(CentralRepoPlatforms selectedPlatform) {
580  switch (selectedPlatform) {
581  case POSTGRESQL:
582  return "ON CONFLICT DO NOTHING";
583  case SQLITE:
584  return "";
585  default:
586  return "";
587  }
588  }
594  private Connection getEphemeralConnection() {
595  switch (selectedPlatform) {
596  case POSTGRESQL:
597  return this.postgresCentralRepoSettings.getEphemeralConnection(false);
598  case SQLITE:
599  return this.sqliteCentralRepoSettings.getEphemeralConnection();
600  default:
601  return null;
602  }
603  }
604 
610  private boolean createPersonaTables(Statement stmt) throws SQLException {
611 
612  stmt.execute(getCreateConfidenceTableStatement(selectedPlatform));
613  stmt.execute(getCreateExaminersTableStatement(selectedPlatform));
614  stmt.execute(getCreatePersonaStatusTableStatement(selectedPlatform));
615 
616  stmt.execute(getCreatePersonasTableStatement(selectedPlatform));
617  stmt.execute(getCreatePersonaAliasTableStatement(selectedPlatform));
618  stmt.execute(getCreatePersonaMetadataTableStatement(selectedPlatform));
619  stmt.execute(getCreatePersonaAccountsTableStatement(selectedPlatform));
620 
621  return true;
622  }
623 
624 
631  static String getCreateAccountTypesTableStatement(CentralRepoPlatforms selectedPlatform) {
632 
633  return "CREATE TABLE IF NOT EXISTS account_types ("
634  + getNumericPrimaryKeyClause("id", selectedPlatform)
635  + "type_name TEXT NOT NULL,"
636  + "display_name TEXT NOT NULL,"
637  + "correlation_type_id " + getBigIntType(selectedPlatform) + " ,"
638  + "CONSTRAINT type_name_unique UNIQUE (type_name),"
639  + "FOREIGN KEY (correlation_type_id) REFERENCES correlation_types(id)"
640  + ")";
641  }
642 
649  static String getCreateConfidenceTableStatement(CentralRepoPlatforms selectedPlatform) {
650 
651  return "CREATE TABLE IF NOT EXISTS confidence ("
652  + getNumericPrimaryKeyClause("id", selectedPlatform)
653  + "confidence_id integer NOT NULL,"
654  + "description TEXT,"
655  + "CONSTRAINT level_unique UNIQUE (confidence_id)"
656  + ")";
657  }
658 
665  static String getCreateExaminersTableStatement(CentralRepoPlatforms selectedPlatform) {
666 
667  return "CREATE TABLE IF NOT EXISTS examiners ("
668  + getNumericPrimaryKeyClause("id", selectedPlatform)
669  + "login_name TEXT NOT NULL,"
670  + "display_name TEXT,"
671  + "CONSTRAINT login_name_unique UNIQUE(login_name)"
672  + ")";
673  }
674 
681  static String getCreatePersonaStatusTableStatement(CentralRepoPlatforms selectedPlatform) {
682 
683  return "CREATE TABLE IF NOT EXISTS persona_status ("
684  + getNumericPrimaryKeyClause("id", selectedPlatform)
685  + "status_id integer NOT NULL,"
686  + "status TEXT NOT NULL,"
687  + "CONSTRAINT status_unique UNIQUE(status_id)"
688  + ")";
689  }
690 
691 
698  static String getCreateAccountsTableStatement(CentralRepoPlatforms selectedPlatform) {
699 
700  return "CREATE TABLE IF NOT EXISTS accounts ("
701  + getNumericPrimaryKeyClause("id", selectedPlatform)
702  + "account_type_id integer NOT NULL,"
703  + "account_unique_identifier TEXT NOT NULL,"
704  + "CONSTRAINT account_unique UNIQUE(account_type_id, account_unique_identifier),"
705  + "FOREIGN KEY (account_type_id) REFERENCES account_types(id)"
706  + ")";
707  }
708 
715  static String getCreatePersonasTableStatement(CentralRepoPlatforms selectedPlatform) {
716 
717  return "CREATE TABLE IF NOT EXISTS personas ("
718  + getNumericPrimaryKeyClause("id", selectedPlatform)
719  + "uuid TEXT NOT NULL,"
720  + "comment TEXT NOT NULL,"
721  + "name TEXT NOT NULL,"
722  + "created_date " + getBigIntType(selectedPlatform) + " ,"
723  + "modified_date " + getBigIntType(selectedPlatform) + " ,"
724  + "status_id integer NOT NULL,"
725  + "examiner_id integer NOT NULL,"
726  + "CONSTRAINT uuid_unique UNIQUE(uuid),"
727  + "FOREIGN KEY (status_id) REFERENCES persona_status(status_id), "
728  + "FOREIGN KEY (examiner_id) REFERENCES examiners(id)"
729  + ")";
730  }
731 
738  static String getCreatePersonaAliasTableStatement(CentralRepoPlatforms selectedPlatform) {
739 
740  return "CREATE TABLE IF NOT EXISTS persona_alias ("
741  + getNumericPrimaryKeyClause("id", selectedPlatform)
742  + "persona_id " + getBigIntType(selectedPlatform) + " ,"
743  + "alias TEXT NOT NULL, "
744  + "justification TEXT NOT NULL,"
745  + "confidence_id integer NOT NULL,"
746  + "date_added " + getBigIntType(selectedPlatform) + " ,"
747  + "examiner_id integer NOT NULL,"
748  + "FOREIGN KEY (persona_id) REFERENCES personas(id),"
749  + "FOREIGN KEY (confidence_id) REFERENCES confidence(confidence_id),"
750  + "FOREIGN KEY (examiner_id) REFERENCES examiners(id)"
751  + ")";
752  }
753 
760  static String getCreatePersonaMetadataTableStatement(CentralRepoPlatforms selectedPlatform) {
761 
762  return "CREATE TABLE IF NOT EXISTS persona_metadata ("
763  + getNumericPrimaryKeyClause("id", selectedPlatform)
764  + "persona_id " + getBigIntType(selectedPlatform) + " ,"
765  + "name TEXT NOT NULL,"
766  + "value TEXT NOT NULL,"
767  + "justification TEXT NOT NULL,"
768  + "confidence_id integer NOT NULL,"
769  + "date_added " + getBigIntType(selectedPlatform) + " ,"
770  + "examiner_id integer NOT NULL,"
771  + "CONSTRAINT unique_metadata UNIQUE(persona_id, name),"
772  + "FOREIGN KEY (persona_id) REFERENCES personas(id),"
773  + "FOREIGN KEY (confidence_id) REFERENCES confidence(confidence_id),"
774  + "FOREIGN KEY (examiner_id) REFERENCES examiners(id)"
775  + ")";
776  }
777 
784  static String getCreatePersonaAccountsTableStatement(CentralRepoPlatforms selectedPlatform) {
785 
786  return "CREATE TABLE IF NOT EXISTS persona_accounts ("
787  + getNumericPrimaryKeyClause("id", selectedPlatform)
788  + "persona_id " + getBigIntType(selectedPlatform) + " ,"
789  + "account_id " + getBigIntType(selectedPlatform) + " ,"
790  + "justification TEXT NOT NULL,"
791  + "confidence_id integer NOT NULL,"
792  + "date_added " + getBigIntType(selectedPlatform) + " ,"
793  + "examiner_id integer NOT NULL,"
794  + "FOREIGN KEY (persona_id) REFERENCES personas(id),"
795  + "FOREIGN KEY (account_id) REFERENCES accounts(id),"
796  + "FOREIGN KEY (confidence_id) REFERENCES confidence(confidence_id),"
797  + "FOREIGN KEY (examiner_id) REFERENCES examiners(id)"
798  + ")";
799  }
800 
801 
810  private static boolean insertDefaultPersonaTablesContent(Connection conn, CentralRepoPlatforms selectedPlatform) {
811 
812  try (Statement stmt = conn.createStatement()) {
813  // populate the confidence table
814  for (Confidence confidence : Persona.Confidence.values()) {
815  String sqlString = "INSERT INTO confidence (confidence_id, description) VALUES ( " + confidence.getLevel() + ", '" + confidence.toString() + "')" //NON-NLS
816  + getOnConflictDoNothingClause(selectedPlatform);
817  stmt.execute(sqlString);
818  }
819 
820  // populate the persona_status table
821  for (PersonaStatus status : Persona.PersonaStatus.values()) {
822  String sqlString = "INSERT INTO persona_status (status_id, status) VALUES ( " + status.getStatus() + ", '" + status.toString() + "')" //NON-NLS
823  + getOnConflictDoNothingClause(selectedPlatform);
824  stmt.execute(sqlString);
825  }
826 
827  } catch (SQLException ex) {
828  LOGGER.log(Level.SEVERE, String.format("Failed to populate default data in Persona tables."), ex);
829  return false;
830  }
831 
832  return true;
833  }
834 
842  static boolean insertDefaultAccountsTablesContent(Connection conn, CentralRepoPlatforms selectedPlatform) {
843 
844  try (Statement stmt = conn.createStatement();) {
845 
846  // Populate the account_types table
847  for (Account.Type type : Account.Type.PREDEFINED_ACCOUNT_TYPES) {
848  if (type != Account.Type.DEVICE) {
849  int correlationTypeId = getCorrelationTypeIdForAccountType(conn, type);
850  if (correlationTypeId > 0) {
851  String sqlString = String.format("INSERT INTO account_types (type_name, display_name, correlation_type_id) VALUES ('%s', '%s', %d)" + getOnConflictDoNothingClause(selectedPlatform),
852  type.getTypeName(), type.getDisplayName(), correlationTypeId);
853  stmt.execute(sqlString);
854  }
855  }
856  }
857 
858  } catch (SQLException ex) {
859  LOGGER.log(Level.SEVERE, String.format("Failed to populate default data in account_types table."), ex);
860  return false;
861  }
862 
863  return true;
864  }
865 
875  static int getCorrelationTypeIdForAccountType(Connection conn, Account.Type accountType) {
876 
877  int typeId = -1;
878  if (accountType == Account.Type.EMAIL) {
879  typeId = CorrelationAttributeInstance.EMAIL_TYPE_ID;
880  } else if (accountType == Account.Type.PHONE) {
881  typeId = CorrelationAttributeInstance.PHONE_TYPE_ID;
882  } else {
883  String querySql = "SELECT * FROM correlation_types WHERE display_name=?";
884  try ( PreparedStatement preparedStatementQuery = conn.prepareStatement(querySql)) {
885  preparedStatementQuery.setString(1, accountType.getDisplayName());
886  try (ResultSet resultSet = preparedStatementQuery.executeQuery();) {
887  if (resultSet.next()) {
888  typeId = resultSet.getInt("id");
889  }
890  }
891  } catch (SQLException ex) {
892  LOGGER.log(Level.SEVERE, String.format("Failed to get correlation typeId for account type %s.", accountType.getTypeName()), ex);
893  }
894  }
895 
896  return typeId;
897  }
898 }
static boolean insertDefaultPersonaTablesContent(Connection conn, CentralRepoPlatforms selectedPlatform)
static String correlationTypeToReferenceTableName(CorrelationAttributeInstance.Type type)
static String getCreateCasesTableStatement(CentralRepoPlatforms selectedPlatform)
static String getCreateReferenceSetsTableStatement(CentralRepoPlatforms selectedPlatform)
static String getCreateCorrelationTypesTableStatement(CentralRepoPlatforms selectedPlatform)
static String correlationTypeToInstanceTableName(CorrelationAttributeInstance.Type type)
static String getNumericPrimaryKeyClause(String pkName, CentralRepoPlatforms selectedPlatform)
RdbmsCentralRepoFactory(CentralRepoPlatforms selectedPlatform, PostgresCentralRepoSettings repoSettings)
RdbmsCentralRepoFactory(CentralRepoPlatforms selectedPlatform, SqliteCentralRepoSettings repoSettings)
static String getCreateDbInfoTableStatement(CentralRepoPlatforms selectedPlatform)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static String getOnConflictIgnoreClause(CentralRepoPlatforms selectedPlatform)
static String getReferenceTypesTableTemplate(CentralRepoPlatforms selectedPlatform)
static String getOnConflictDoNothingClause(CentralRepoPlatforms selectedPlatform)
static String getCreateOrganizationsTableStatement(CentralRepoPlatforms selectedPlatform)

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.