@Override protected Object getTargetRepository(RepositoryInformation metadata) { entityClass = metadata.getDomainType(); @SuppressWarnings("rawtypes") ReflectionJdbcRepository repository = getTargetRepositoryViaReflection(metadata, entityClass); repository.setDataSource(datasource); repository.afterPropertiesSet(); this.repository = repository; generator = SqlGeneratorFactory.getInstance().getGenerator(datasource); template = new NamedParameterJdbcTemplate((JdbcOperations) extractRepositoryField(repository, FIELD_JDBC_OPS)); rowMapper = extractRepositoryField(repository, FIELD_ROWMAPPER); tableDescription = extractRepositoryField(repository, FIELD_TABLE_DESCRIPTION); return repository; }
@Test public void should_return_external_executor_runner() throws Exception { QueueConsumer queueConsumer = mock(QueueConsumer.class); QueueSettings settings = QueueSettings.builder().withBetweenTaskTimeout(Duration.ZERO).withNoTaskTimeout(Duration.ZERO) .withProcessingMode(ProcessingMode.USE_EXTERNAL_EXECUTOR).build(); QueueLocation location = QueueLocation.builder().withTableName("testTable") .withQueueId(new QueueId("testQueue")).build(); when(queueConsumer.getQueueConfig()).thenReturn(new QueueConfig(location, settings)); QueueRunner queueRunner = QueueRunner.Factory.createQueueRunner(queueConsumer, new QueueDao(new QueueShardId("s1"), mock(JdbcOperations.class), mock(TransactionOperations.class)), mock(TaskLifecycleListener.class), mock(Executor.class)); assertThat(queueRunner, CoreMatchers.instanceOf(QueueRunnerInExternalExecutor.class)); }
@Test public void should_return_separate_transactions_runner() throws Exception { QueueConsumer queueConsumer = mock(QueueConsumer.class); QueueSettings settings = QueueSettings.builder().withBetweenTaskTimeout(Duration.ZERO).withNoTaskTimeout(Duration.ZERO) .withProcessingMode(ProcessingMode.SEPARATE_TRANSACTIONS).build(); QueueLocation location = QueueLocation.builder().withTableName("testTable") .withQueueId(new QueueId("testQueue")).build(); when(queueConsumer.getQueueConfig()).thenReturn(new QueueConfig(location, settings)); QueueRunner queueRunner = QueueRunner.Factory.createQueueRunner(queueConsumer, new QueueDao(new QueueShardId("s1"), mock(JdbcOperations.class), mock(TransactionOperations.class)), mock(TaskLifecycleListener.class), null); assertThat(queueRunner, CoreMatchers.instanceOf(QueueRunnerInSeparateTransactions.class)); }
@Test public void should_return_wrap_in_transaction_runner() throws Exception { QueueConsumer queueConsumer = mock(QueueConsumer.class); QueueSettings settings = QueueSettings.builder().withBetweenTaskTimeout(Duration.ZERO).withNoTaskTimeout(Duration.ZERO) .withProcessingMode(ProcessingMode.WRAP_IN_TRANSACTION).build(); QueueLocation location = QueueLocation.builder().withTableName("testTable") .withQueueId(new QueueId("testQueue")).build(); when(queueConsumer.getQueueConfig()).thenReturn(new QueueConfig(location, settings)); QueueRunner queueRunner = QueueRunner.Factory.createQueueRunner(queueConsumer, new QueueDao(new QueueShardId("s1"), mock(JdbcOperations.class), mock(TransactionOperations.class)), mock(TaskLifecycleListener.class), null); assertThat(queueRunner, CoreMatchers.instanceOf(QueueRunnerInTransaction.class)); }
public static int[] executeBatchUpdateWithNamedParameters(final ParsedSql parsedSql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) { if (batchArgs.length <= 0) { return new int[] {0}; } String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]); return jdbcOperations.batchUpdate( sqlToUse, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null); int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]); setStatementParameters(values, ps, columnTypes); } @Override public int getBatchSize() { return batchArgs.length; } }); }
/** * return a map of the objects, divided into their transactions indexes. * * @param jdbcOperations * the jdbc operations to use. * @param sqlKey * the sql key to use. * @param sqlArgsBaList * the byte arrays to use for SQL arguments. * @param mapToObject * the mapToObject to use. * @param <T> * the object type to use. * @return a map of the objects, divided into their transactions indexes. */ private <T> Map<Integer, List<T>> getMapList(final JdbcOperations jdbcOperations, final String sqlKey, final AbstractMapToObject<T> mapToObject, final byte[]... sqlArgsBaList) { final String sql = getSql(sqlKey); final List<Map<String, Object>> mapList = jdbcOperations.queryForList(sql, (Object[]) sqlArgsBaList); final Map<Integer, List<T>> tMapList = new TreeMap<>(); for (final Map<String, Object> map : mapList) { final byte[] transactionIndexBa = (byte[]) map.get(TRANSACTION_INDEX); final T t = mapToObject.toObject(map); final int transactionIndex = getTransactionIndex(transactionIndexBa); if (!tMapList.containsKey(transactionIndex)) { tMapList.put(transactionIndex, new ArrayList<>()); } tMapList.get(transactionIndex).add(t); } return tMapList; }
/** * gets the inputs for each transaction in the block, and adds them to the * transaction. * * @param block * the block to use * @param jdbcOperations * the jdbc operations to use. * @param blockIndexBa * the block index to use. */ private void getTransactionInputsWithIndex(final Block block, final JdbcOperations jdbcOperations, final byte[] blockIndexBa) { final Map<Integer, List<CoinReference>> inputsMap = getMapList(jdbcOperations, "getTransactionInputsWithBlockIndex", new CoinReferenceMapToObject(), blockIndexBa); for (final int txIx : inputsMap.keySet()) { final List<CoinReference> inputs = inputsMap.get(txIx); if (txIx >= block.getTransactionList().size()) { throw new RuntimeException( "txIx \"" + txIx + "\" exceeds txList.size \"" + block.getTransactionList().size() + "\" for block index \"" + block.getIndexAsLong() + "\" hash \"" + block.hash + "\""); } else { block.getTransactionList().get(txIx).inputs.addAll(inputs); } } }
@Autowired public DefaultCalendarService(final EventDao eventDao, final CalendarUserDao userDao, final JdbcOperations jdbcOperations, final PasswordEncoder passwordEncoder) { if (eventDao == null) { throw new IllegalArgumentException("eventDao cannot be null"); } if (userDao == null) { throw new IllegalArgumentException("userDao cannot be null"); } if (jdbcOperations == null) { throw new IllegalArgumentException("jdbcOperations cannot be null"); } if (passwordEncoder == null) { throw new IllegalArgumentException("passwordEncoder cannot be null"); } this.eventDao = eventDao; this.userDao = userDao; this.jdbcOperations = jdbcOperations; this.passwordEncoder = passwordEncoder; }
@Autowired public DefaultCalendarService(final EventDao eventDao, final CalendarUserDao userDao, final JdbcOperations jdbcOperations) { if (eventDao == null) { throw new IllegalArgumentException("eventDao cannot be null"); } if (userDao == null) { throw new IllegalArgumentException("userDao cannot be null"); } if (jdbcOperations == null) { throw new IllegalArgumentException("jdbcOperations cannot be null"); } this.eventDao = eventDao; this.userDao = userDao; this.jdbcOperations = jdbcOperations; }
/** * Add the given versions to the given project * * @param dataSource * The data source of JIRA database. * @param jira * the JIRA project identifier. * @param versions * the version names to add * @return the {@link Map} where value is the created version identifier. */ public Map<String, Integer> addVersions(final DataSource dataSource, final int jira, final Collection<String> versions) { final Map<String, Integer> result = new HashMap<>(); final JdbcOperations jdbcTemplate = new JdbcTemplate(dataSource); int nextId = prepareForNextId(dataSource, VERSION_NODE, versions.size()); int nextSequence = getNextVersionSequence(jira, jdbcTemplate); for (final String version : versions) { jdbcTemplate.update("INSERT INTO projectversion (ID,PROJECT,vname,SEQUENCE) values(?,?,?,?)", nextId, jira, version, nextSequence); result.put(version, nextId); nextId++; nextSequence++; } return result; }
/** * Return current sequence. */ protected int getCurrentSequence(final String sequenceName, final JdbcOperations jdbcTemplate) { final Long currentSequence; final List<Long> sequence = jdbcTemplate .queryForList("SELECT SEQ_ID FROM SEQUENCE_VALUE_ITEM WHERE SEQ_NAME = ?", Long.class, sequenceName); if (sequence.isEmpty()) { // New sequence, start from an arbitrary sequence to be sure JIRA // does not fill it currentSequence = 10000L; jdbcTemplate.update("INSERT INTO SEQUENCE_VALUE_ITEM (SEQ_NAME,SEQ_ID) values(?,?)", sequenceName, currentSequence); } else { currentSequence = sequence.get(0); } return currentSequence.intValue(); }
@Test public void testPrepareForNextIdConcurrent() throws Exception { final JiraUpdateDao dao = new JiraUpdateDao() { private boolean mock = true; @Override protected int getCurrentSequence(final String sequenceName, final JdbcOperations jdbcTemplate) { if (mock) { mock = false; return -10000; } return super.getCurrentSequence(sequenceName, jdbcTemplate); } }; Assert.assertEquals(10100, dao.prepareForNextId(datasource, "ChangeGroup", 2000)); }
/** * Indicate the installation and activation of "script runner" plug-in. * * @param dataSource * The data source of JIRA database. * @param jira * the JIRA project identifier. * @param issues * The issues to add. The ID property of each inserted issue is * also updated in these objects. * @param workflowStepMapping * the {@link Map} linking type identifier to a {@link Map} * linking status name to steps. */ public void addIssues(final DataSource dataSource, final int jira, final List<JiraIssueRow> issues, final Map<Integer, Workflow> workflowStepMapping) { final JdbcOperations jdbcTemplate = new JdbcTemplate(dataSource); int nextId = prepareForNextId(dataSource, ISSUE_NODE, issues.size()); reserveProjectCounter(dataSource, jira, issues); int nextCurrentStepId = prepareForNextId(dataSource, "OSCurrentStep", issues.size()); int nextWfEntryId = prepareForNextId(dataSource, "OSWorkflowEntry", issues.size()); int counter = 0; for (final JiraIssueRow issueRow : issues) { issueRow.setId(nextId); log.info("Inserting issue {}-{}({}) {}/{}", issueRow.getPkey(), issueRow.getIssueNum(), issueRow.getId(), counter, issues.size()); Workflow workflow = workflowStepMapping.get(issueRow.getType()); if (workflow == null) { workflow = workflowStepMapping.get(0); } addIssue(jira, jdbcTemplate, nextId, nextCurrentStepId, nextWfEntryId, issueRow, workflow); nextId++; nextWfEntryId++; nextCurrentStepId++; counter++; } }
/** * Add an issue, workflow entry, corresponding step of current status and link author to this issue (user activity * dashboard) */ private void addIssue(final int jira, final JdbcOperations jdbcTemplate, final int nextId, final int nextCurrentStepId, final int nextWfEntryId, final JiraIssueRow issueRow, final Workflow workflow) { final INamableBean<Integer> workflowStep = workflow.getStatusToSteps().get(issueRow.getStatusText()); // Insert workflow activity jdbcTemplate.update("INSERT INTO OS_WFENTRY (ID,NAME,STATE) values(?,?,?)", nextWfEntryId, workflow.getName(), 1); jdbcTemplate.update("INSERT INTO OS_CURRENTSTEP (ID,ENTRY_ID,STEP_ID,ACTION_ID,START_DATE,STATUS) values(?,?,?,?,?,?)", nextCurrentStepId, nextWfEntryId, workflowStep.getId(), 0, issueRow.getCreated(), workflowStep.getName()); // Insert issue jdbcTemplate.update( "INSERT INTO jiraissue" + " (ID,issuenum,WATCHES,VOTES,PROJECT,REPORTER,ASSIGNEE,CREATOR,issuetype,SUMMARY,DESCRIPTION,PRIORITY,RESOLUTION,RESOLUTIONDATE," + "issuestatus,CREATED,UPDATED,WORKFLOW_ID,DUEDATE) values(?,?,1,0,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", nextId, issueRow.getIssueNum(), jira, issueRow.getReporter(), issueRow.getAssignee(), issueRow.getAuthor(), issueRow.getType(), issueRow.getSummary(), issueRow.getDescription(), issueRow.getPriority(), issueRow.getResolution(), issueRow.getResolutionDate(), issueRow.getStatus(), issueRow.getCreated(), issueRow.getUpdated(), nextWfEntryId, issueRow.getDueDate()); // Add user relation jdbcTemplate.update("INSERT INTO userassociation (SOURCE_NAME,SINK_NODE_ID,SINK_NODE_ENTITY,ASSOCIATION_TYPE,CREATED) values(?,?,?,?,?)", issueRow.getAuthor(), nextId, "Issue", "WatchIssue", issueRow.getUpdated()); }
public OAuth2Configuration(ResourceLoader resourceLoader, OpenIdProviderProperties properties, ObjectProvider<JdbcOperations> jdbcOperations, ObjectProvider<AuthenticationManager> authenticationManager, ObjectProvider<HazelcastInstance> hazelcastInstance) { this.resourceLoader = resourceLoader; this.properties = properties; this.jdbcOperations = jdbcOperations.getObject(); this.authenticationManager = authenticationManager.getObject(); this.hazelcastInstance = hazelcastInstance.getObject(); }
/** * Associate an issue to a set of items; */ private void associatedItems(final JdbcOperations jdbcTemplate, final int issueId, final Collection<Integer> items, final String nodetype, final String associationType) { for (final Integer item : items) { jdbcTemplate .update("INSERT INTO nodeassociation (SOURCE_NODE_ID,SOURCE_NODE_ENTITY,SINK_NODE_ID,SINK_NODE_ENTITY,ASSOCIATION_TYPE)" + " values(?,?,?,?,?)", issueId, ISSUE_NODE, item, nodetype, associationType); } }
@Autowired public JdbcCalendarUserDao(JdbcOperations jdbcOperations) { if (jdbcOperations == null) { throw new IllegalArgumentException("jdbcOperations cannot be null"); } this.jdbcOperations = jdbcOperations; }
@Autowired public JdbcEventDao(JdbcOperations jdbcOperations) { if (jdbcOperations == null) { throw new IllegalArgumentException("jdbcOperations cannot be null"); } this.jdbcOperations = jdbcOperations; }
/** * Associate a single custom field values to a given issue. */ private int associateCustomFieldValue(final JdbcOperations jdbcTemplate, final int issueId, final int nextId, final int cfId, final String column, final Iterable<Object> values) { int nextIdl = nextId; // Persist single/multiple values for (final Object value : values) { jdbcTemplate.update("INSERT INTO customfieldvalue (ID,ISSUE,CUSTOMFIELD," + column + ") values(?,?,?,?)", nextIdl, issueId, cfId, value); nextIdl++; } return nextIdl; }
/** * Associate issue to custom values. Custom component must exist. * * @param dataSource * The data source of JIRA database. * @param issues * The issues to add. * @param customFields * Existing custom field definitions. */ public void associateCustomFieldsValues(final DataSource dataSource, final List<JiraIssueRow> issues, final Map<String, CustomFieldEditor> customFields) { final JdbcOperations jdbcTemplate = new JdbcTemplate(dataSource); // Compute total amount of custom values to create int nextId = prepareForNextId(dataSource, "CustomFieldValue", countCustomFieldValues(issues)); for (final JiraIssueRow issueRow : issues) { nextId = associateCutomFieldValues(customFields, jdbcTemplate, nextId, issueRow); } }