cst.adminTool.persistenceLayer
Class ProductionAdminQueries

java.lang.Object
  extended by cst.common.persistenceLayer.ProductionServiceDatabaseQueries
      extended by cst.adminTool.persistenceLayer.ProductionAdminQueries

public class ProductionAdminQueries
extends ProductionServiceDatabaseQueries

This class is used as a delegation class by ProductionAdminService to carry out almost all SQL queries used to support the production version of the data repository. ProductionAdminQueries is responsible for mapping fields in the data container classes shown in cst.common.businessLayer with table fields in a generated MySQL database schema.


 Copyright 2010 Medical Research Council Unit for Lifelong Health and Ageing
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
 Unless required by applicable law or agreed to in writing, software 
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.  
 

Version:
1.0
Author:
Kevin Garwood (kgarwood@users.sourceforge.net)

Field Summary
 
Fields inherited from class cst.common.persistenceLayer.ProductionServiceDatabaseQueries
activityChangesTableName, databaseName, log, sessionProperties, sqlDateFormat, subjectTableName, trialSubjectModelFactory, userTableName
 
Constructor Summary
ProductionAdminQueries(SessionProperties sessionProperties)
           
 
Method Summary
 void addUser(java.sql.Connection connection, User user)
           
 void clearChanges(java.sql.Connection connection)
           
 void clearDatabase(java.sql.Connection connection)
          clears all the tables in the database
 void clearUsers(java.sql.Connection connection, java.lang.String userTableName)
           
 void deleteDatabase()
           
 void deleteUser(java.sql.Connection connection, User user)
           
 java.util.ArrayList<TrialSubjectModel> getCoreData(java.sql.Connection connection)
          private void addTrialSubjectModel(Connection connection, TrialSubjectModel trialSubjectModel) throws CSTException { TextFieldModel primaryKeyFieldModel = trialSubjectModel.getPrimaryKeyFieldModel(); ArrayList otherCoreFieldModels = trialSubjectModel.getFilterFieldModels(); int completedActivities = getCompletedActivities(connection, trialSubjectModel); //eg: //REPLACE INTO trial_subject_model //(identifier, completed_activities, location...) VALUES ('CS10456', true, 'Birmingham'); //insert field names StringBuilder query = new StringBuilder(); query.append("REPLACE INTO "); query.append(subjectTableName); query.append(" ("); query.append(primaryKeyFieldModel.getDataStorageName()); query.append(", completed_activities"); for (TextFieldModel coreFieldModel : otherCoreFieldModels) { query.append(", "); query.append(coreFieldModel.getDataStorageName()); } //insert corresponding values query.append(") VALUES("); String trialSubjectIdentifier = primaryKeyFieldModel.getValue(); query.append("'"); query.append(trialSubjectIdentifier); query.append("', "); if (completedActivities == -1) { //this trial subject model doesn't exist in the database trialSubjectModel.computeCompletedActivities(); query.append(trialSubjectModel.getCompletedActivitiesCount()); } else { //this trial subject model does exist in the database //therefore we will ensure the updated record retains the //same counter as before query.append(String.valueOf(completedActivities)); } for (TextFieldModel coreFieldModel : otherCoreFieldModels) { query.append(", "); String fieldValue = coreFieldModel.getValue(); if (fieldValue == null) { query.append("NULL"); } else { query.append("'"); query.append(coreFieldModel.getValue()); query.append("'"); } } query.append(");"); Statement statement = null; try { statement = connection.prepareStatement(query.toString()); statement.executeUpdate(query.toString()); //in this part we decide if we want to also import the //trial activities for the subject model.
 java.util.ArrayList<User> getRegisteredUsers(java.sql.Connection connection, java.lang.String userTableName)
           
 void populateDatabase(java.sql.Connection connection, java.util.ArrayList<TrialSubjectModel> trialSubjectModels)
           
 void updateUser(java.sql.Connection connection, User user)
           
 boolean userExists(java.sql.Connection connection, java.lang.String userTableName, java.lang.String userID)
           
 boolean validateAdministrator(java.sql.Connection connection, java.lang.String userTableName, User user)
           
 
Methods inherited from class cst.common.persistenceLayer.ProductionServiceDatabaseQueries
checkActivityStepExists, checkTrialActivityDataStorageNameExists, validateCoreFieldDataStorageName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProductionAdminQueries

public ProductionAdminQueries(SessionProperties sessionProperties)
Method Detail

getRegisteredUsers

public java.util.ArrayList<User> getRegisteredUsers(java.sql.Connection connection,
                                                    java.lang.String userTableName)
                                             throws CSTException
Throws:
CSTException

validateAdministrator

public boolean validateAdministrator(java.sql.Connection connection,
                                     java.lang.String userTableName,
                                     User user)
                              throws CSTException
Throws:
CSTException

clearDatabase

public void clearDatabase(java.sql.Connection connection)
                   throws CSTException
clears all the tables in the database

Parameters:
connection -
Throws:
java.lang.Exception
CSTException

deleteDatabase

public void deleteDatabase()
                    throws java.lang.Exception
Throws:
java.lang.Exception

populateDatabase

public void populateDatabase(java.sql.Connection connection,
                             java.util.ArrayList<TrialSubjectModel> trialSubjectModels)
                      throws CSTException
Throws:
CSTException

getCoreData

public java.util.ArrayList<TrialSubjectModel> getCoreData(java.sql.Connection connection)
                                                   throws CSTException
private void addTrialSubjectModel(Connection connection, TrialSubjectModel trialSubjectModel) throws CSTException { TextFieldModel primaryKeyFieldModel = trialSubjectModel.getPrimaryKeyFieldModel(); ArrayList otherCoreFieldModels = trialSubjectModel.getFilterFieldModels(); int completedActivities = getCompletedActivities(connection, trialSubjectModel); //eg: //REPLACE INTO trial_subject_model //(identifier, completed_activities, location...) VALUES ('CS10456', true, 'Birmingham'); //insert field names StringBuilder query = new StringBuilder(); query.append("REPLACE INTO "); query.append(subjectTableName); query.append(" ("); query.append(primaryKeyFieldModel.getDataStorageName()); query.append(", completed_activities"); for (TextFieldModel coreFieldModel : otherCoreFieldModels) { query.append(", "); query.append(coreFieldModel.getDataStorageName()); } //insert corresponding values query.append(") VALUES("); String trialSubjectIdentifier = primaryKeyFieldModel.getValue(); query.append("'"); query.append(trialSubjectIdentifier); query.append("', "); if (completedActivities == -1) { //this trial subject model doesn't exist in the database trialSubjectModel.computeCompletedActivities(); query.append(trialSubjectModel.getCompletedActivitiesCount()); } else { //this trial subject model does exist in the database //therefore we will ensure the updated record retains the //same counter as before query.append(String.valueOf(completedActivities)); } for (TextFieldModel coreFieldModel : otherCoreFieldModels) { query.append(", "); String fieldValue = coreFieldModel.getValue(); if (fieldValue == null) { query.append("NULL"); } else { query.append("'"); query.append(coreFieldModel.getValue()); query.append("'"); } } query.append(");"); Statement statement = null; try { statement = connection.prepareStatement(query.toString()); statement.executeUpdate(query.toString()); //in this part we decide if we want to also import the //trial activities for the subject model. Bear in mind this //method appears in the admin features. When we import //new subject models, we may have also imported trial activities. if (completedActivities == -1) { //trial subject model does not exist in the database //so add all the activity model instances //create records for each trial activity ArrayList trialActivityModels = trialSubjectModel.getTrialActivityModels(); for (TrialActivityModel currentTrialActivityModel : trialActivityModels) { insertTrialActivityModel(connection, primaryKeyFieldModel, currentTrialActivityModel); } } else { //trial subject model does exist. //for importing trial activity models we DON'T want //to overwrite existing activity data //this will be added via the SQLLoggingQueries } } catch(SQLException exception) { log.logException(exception); String errorMessage = CSTMessages.getMessage("sqlDB.error.unableToImportTrialSubjectModels"); CSTException ctmException = new CSTException(CSTErrorType.SQL_UNABLE_TO_IMPORT_TRIAL_SUBJECT_MODELS, errorMessage); throw ctmException; } finally { AbstractProductionService.closeStatements(statement, null); } }

Throws:
CSTException

addUser

public void addUser(java.sql.Connection connection,
                    User user)
             throws CSTException
Throws:
CSTException

updateUser

public void updateUser(java.sql.Connection connection,
                       User user)
                throws CSTException
Throws:
CSTException

deleteUser

public void deleteUser(java.sql.Connection connection,
                       User user)
                throws CSTException
Throws:
CSTException

clearUsers

public void clearUsers(java.sql.Connection connection,
                       java.lang.String userTableName)
                throws CSTException
Throws:
CSTException

clearChanges

public void clearChanges(java.sql.Connection connection)
                  throws CSTException
Throws:
CSTException

userExists

public boolean userExists(java.sql.Connection connection,
                          java.lang.String userTableName,
                          java.lang.String userID)
                   throws CSTException
Throws:
CSTException