Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0af21a6c7fb7c06122c2f39b3a596dd486af948b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
 * Copyright (c) 2012 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.db.internal.callable;

import java.io.InputStream;
import java.net.URI;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.osee.database.schema.DatabaseCallable;
import org.eclipse.osee.database.schema.SchemaResource;
import org.eclipse.osee.database.schema.SchemaResourceProvider;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.core.SystemPreferences;
import org.eclipse.osee.orcs.core.ds.DataStoreConstants;
import org.eclipse.osee.orcs.core.ds.DataStoreInfo;
import org.eclipse.osee.orcs.db.internal.resource.ResourceConstants;
import org.eclipse.osee.orcs.db.internal.util.DataStoreInfoImpl;

/**
 * @author Roberto E. Escobar
 */
public class FetchDatastoreInfoCallable extends DatabaseCallable<DataStoreInfo> {

   private final SchemaResourceProvider schemaProvider;
   private final SystemPreferences preferences;

   public FetchDatastoreInfoCallable(Log logger, IOseeDatabaseService dbService, SchemaResourceProvider schemaProvider, SystemPreferences preferences) {
      super(logger, dbService);
      this.schemaProvider = schemaProvider;
      this.preferences = preferences;
   }

   @Override
   public DataStoreInfo call() throws Exception {
      DataStoreInfoImpl dataStoreInfo = new DataStoreInfoImpl();

      Map<String, String> props = new HashMap<String, String>();
      addInfoProperties(props);
      addDbMetaData(props);

      dataStoreInfo.setProperties(props);

      List<IResource> configResources = new ArrayList<IResource>();
      dataStoreInfo.setConfigurationResources(configResources);

      for (SchemaResource resource : schemaProvider.getSchemaResources()) {
         configResources.add(asResource(resource));
      }
      return dataStoreInfo;
   }

   private void addInfoProperties(Map<String, String> props) throws OseeCoreException {
      for (String key : preferences.getKeys()) {
         String value = preferences.getValue(key);
         if (!Strings.isValid(value)) {
            value = "";
         }
         props.put(key, value);
      }
      props.put(DataStoreConstants.DATASTORE_ID_KEY, preferences.getSystemUuid());
      props.put("ds.binary.data.path", ResourceConstants.getBinaryDataPath(preferences));
      props.put("ds.attribute.data.path", ResourceConstants.getAttributeDataPath(preferences));
      props.put("ds.exchange.data.path", ResourceConstants.getExchangeDataPath(preferences));
   }

   private void addDbMetaData(Map<String, String> props) throws OseeCoreException {
      OseeConnection connection = getDatabaseService().getConnection();
      try {
         DatabaseMetaData meta = connection.getMetaData();
         props.put("db.connection.url", meta.getURL());
         props.put("db.connection.username", meta.getUserName());

         props.put("db.product.name", meta.getDatabaseProductName());
         props.put("db.product.version", meta.getDatabaseProductVersion());

         props.put("db.driver.name", meta.getDriverName());
         props.put("db.driver.version", meta.getDriverVersion());

      } catch (SQLException ex) {
         OseeExceptions.wrapAndThrow(ex);
      } finally {
         connection.close();
      }
   }

   private IResource asResource(SchemaResource resource) {
      return new SchemaResourceAdaptor(getLogger(), resource);
   }

   private static final class SchemaResourceAdaptor implements IResource {

      private final Log logger;
      private final SchemaResource resource;

      public SchemaResourceAdaptor(Log logger, SchemaResource resource) {
         super();
         this.logger = logger;
         this.resource = resource;
      }

      @Override
      public InputStream getContent() throws OseeCoreException {
         return resource.getContent();
      }

      @Override
      public URI getLocation() {
         URI toReturn = null;
         try {
            toReturn = resource.getLocation();
         } catch (OseeCoreException ex) {
            logger.error(ex, "Error finding resource");
         }
         return toReturn;
      }

      @Override
      public String getName() {
         return resource.getClass().getSimpleName();
      }

      @Override
      public boolean isCompressed() {
         return false;
      }

   }

}

Back to the top