Search in sources :

Example 21 with Database

use of org.wso2.carbon.humantask.core.db.Database in project product-iots by wso2.

the class DeviceTypeDAOImpl method getAllDevices.

public List<Device> getAllDevices() throws DeviceMgtPluginException {
    Connection conn = null;
    PreparedStatement stmt = null;
    ResultSet resultSet = null;
    Device device;
    List<Device> iotDevices = new ArrayList<>();
    try {
        conn = DeviceTypeDAO.getConnection();
        String selectDBQuery = "SELECT sampledevice_DEVICE_ID, DEVICE_NAME " + "FROM sampledevice_DEVICE";
        stmt = conn.prepareStatement(selectDBQuery);
        resultSet = stmt.executeQuery();
        while (resultSet.next()) {
            device = new Device();
            device.setDeviceIdentifier(resultSet.getString(DeviceTypeConstants.DEVICE_PLUGIN_DEVICE_ID));
            device.setName(resultSet.getString(DeviceTypeConstants.DEVICE_PLUGIN_DEVICE_NAME));
            List<Device.Property> propertyList = new ArrayList<>();
            device.setProperties(propertyList);
        }
        if (log.isDebugEnabled()) {
            log.debug("All sampledevice device details have fetched from sampledevice database.");
        }
        return iotDevices;
    } catch (SQLException e) {
        String msg = "Error occurred while fetching all sampledevice device data'";
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, resultSet);
        DeviceTypeDAO.closeConnection();
    }
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Device(org.wso2.carbon.device.mgt.common.Device) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 22 with Database

use of org.wso2.carbon.humantask.core.db.Database in project product-iots by wso2.

the class DeviceTypeUtils method setupDeviceManagementSchema.

public static void setupDeviceManagementSchema() throws DeviceMgtPluginException {
    try {
        Context ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup(DeviceTypeConstants.DATA_SOURCE_NAME);
        DeviceSchemaInitializer initializer = new DeviceSchemaInitializer(dataSource);
        log.info("Initializing device management repository database schema");
        initializer.createRegistryDatabase();
    } catch (NamingException e) {
        log.error("Error while looking up the data source: " + DeviceTypeConstants.DATA_SOURCE_NAME);
    } catch (Exception e) {
        throw new DeviceMgtPluginException("Error occurred while initializing Iot Device " + "Management database schema", e);
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Example 23 with Database

use of org.wso2.carbon.humantask.core.db.Database in project jaggery by wso2.

the class ShellUtilityService method destroyUtilityServices.

/**
     * Destroy utility services for command line client
     *
     */
protected static void destroyUtilityServices() {
    //H2 database
    final H2DatabaseManager databaseManager = H2DatabaseManager.getInstance();
    databaseManager.terminate();
}
Also used : H2DatabaseManager(org.wso2.carbon.h2.osgi.console.H2DatabaseManager)

Example 24 with Database

use of org.wso2.carbon.humantask.core.db.Database in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getSubscriptionPolicyById.

/**
 * Retrieves {@link SubscriptionPolicy} with policy uuid <code>uuid</code>
 * <p>This will retrieve complete details about the ApplicationPolicy with all pipelins and conditions.</p>
 *
 * @param uuid uuid of the policy to retrieve from the database
 * @return {@link SubscriptionPolicy}
 */
private SubscriptionPolicy getSubscriptionPolicyById(String uuid) throws SQLException, APIMgtDAOException {
    final String query = "SELECT NAME, UUID, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, CUSTOM_ATTRIBUTES, IS_DEPLOYED, RATE_LIMIT_COUNT, RATE_LIMIT_TIME_UNIT, " + "STOP_ON_QUOTA_REACH, BILLING_PLAN FROM AM_SUBSCRIPTION_POLICY WHERE UUID = ?";
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement statement = conn.prepareStatement(query)) {
        statement.setString(1, uuid);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            if (rs.next()) {
                return createSubscriptionPolicyFromResultSet(uuid, rs);
            } else {
                // not found
                String msg = "Subscription Policy not found for id: " + uuid;
                log.warn(msg);
                throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
            }
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 25 with Database

use of org.wso2.carbon.humantask.core.db.Database in project carbon-apimgt by wso2.

the class MssqlSQLStatements method prepareAttributeSearchStatementForStore.

/**
 * @see ApiDAOVendorSpecificStatements#prepareAttributeSearchStatementForStore(Connection connection, List, List,
 * Map, int, int)
 */
@Override
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE" })
public PreparedStatement prepareAttributeSearchStatementForStore(Connection connection, List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
    StringBuilder roleListBuilder = new StringBuilder();
    roleListBuilder.append("?");
    for (int i = 0; i < roles.size() - 1; i++) {
        roleListBuilder.append(",?");
    }
    StringBuilder searchQuery = new StringBuilder();
    Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        searchQuery.append("LOWER(");
        if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
            searchQuery.append(APIMgtConstants.TAG_NAME_COLUMN);
        } else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
            searchQuery.append(APIMgtConstants.URL_PATTERN_COLUMN);
        } else {
            searchQuery.append(entry.getKey());
        }
        searchQuery.append(") LIKE ?");
        if (entries.hasNext()) {
            searchQuery.append(" AND ");
        }
    }
    // retrieve the attribute applicable for the search
    String searchAttribute = attributeMap.entrySet().iterator().next().getKey();
    // get the corresponding implementation based on the attribute to be searched
    String query = searchMap.get(searchAttribute).getStoreAttributeSearchQuery(roleListBuilder, searchQuery, offset, limit);
    query = "Select * from ( " + query + " )  A" + getStoreAPIsByLabelJoinQuery(labels);
    try {
        int queryIndex = 1;
        PreparedStatement statement = connection.prepareStatement(query);
        // include the attribute in the query (for APIs with public visibility)
        for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
            statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
            queryIndex++;
        }
        // include user roles in the query
        for (String role : roles) {
            statement.setString(queryIndex, role);
            queryIndex++;
        }
        // include the attribute in the query (for APIs with restricted visibility)
        for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
            statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
            queryIndex++;
        }
        for (String label : labels) {
            statement.setString(queryIndex, label);
            queryIndex++;
        }
        statement.setInt(queryIndex, limit);
        // setting 0 as the default offset based on store-api.yaml and MSSQL specifications
        statement.setInt(++queryIndex, (offset < 0) ? 0 : offset);
        return statement;
    } catch (SQLException e) {
        String errorMsg = "Error occurred while searching APIs for attributes in the database.";
        log.error(errorMsg, e);
        throw new APIMgtDAOException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

SQLException (java.sql.SQLException)29 PreparedStatement (java.sql.PreparedStatement)28 Connection (java.sql.Connection)24 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)23 ResultSet (java.sql.ResultSet)16 ArrayList (java.util.ArrayList)13 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)12 Map (java.util.Map)6 DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)6 HashMap (java.util.HashMap)5 BpelDatabase (org.apache.ode.bpel.engine.BpelDatabase)5 File (java.io.File)4 InstanceFilter (org.apache.ode.bpel.common.InstanceFilter)4 BpelDAOConnection (org.apache.ode.bpel.dao.BpelDAOConnection)4 InstanceManagementException (org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.InstanceManagementException)4 Device (org.wso2.carbon.device.mgt.common.Device)4 IOException (java.io.IOException)3 Collection (java.util.Collection)3 DataSource (javax.sql.DataSource)3 ProcessInstanceDAO (org.apache.ode.bpel.dao.ProcessInstanceDAO)3