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();
}
}
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);
}
}
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();
}
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);
}
}
}
}
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);
}
}
Aggregations