Search in sources :

Example 1 with LockedPeriodsResult

use of org.activityinfo.shared.command.GetLockedPeriods.LockedPeriodsResult in project activityinfo by bedatadriven.

the class GetLockedPeriodsHandler method execute.

@Override
public void execute(GetLockedPeriods command, ExecutionContext context, final AsyncCallback<LockedPeriodsResult> callback) {
    final List<Integer> projectIds = Lists.newArrayList();
    final List<Integer> activityIds = Lists.newArrayList();
    final List<LockedPeriodDTO> lockedPeriods = Lists.newArrayList();
    SqlQuery.select("ProjectId").from("project").where("DatabaseId").equalTo(command.getDatabaseId()).execute(context.getTransaction(), new RowHandler() {

        @Override
        public void handleRow(SqlResultSetRow row) {
            projectIds.add(row.getInt("ProjectId"));
        }
    });
    SqlQuery.select("ActivityId").from("activity").where("DatabaseId").equalTo(command.getDatabaseId()).execute(context.getTransaction(), new RowHandler() {

        @Override
        public void handleRow(SqlResultSetRow row) {
            activityIds.add(row.getInt("ActivityId"));
        }
    });
    // TODO(ruud): load only what is visible to user
    SqlQuery.select("fromDate", "toDate", "enabled", "name", "lockedPeriodId", "userDatabaseId", "activityId", "projectId").from("lockedperiod").where("ActivityId").in(activityIds).or().where("ProjectId").in(projectIds).or().where("UserDatabaseId").equalTo(command.getDatabaseId()).execute(context.getTransaction(), new SqlResultCallback() {

        @Override
        public void onSuccess(SqlTransaction tx, SqlResultSet results) {
            UserDatabaseDTO db = new UserDatabaseDTO();
            ActivityDTO activity = new ActivityDTO();
            ProjectDTO project = new ProjectDTO();
            for (SqlResultSetRow row : results.getRows()) {
                LockedPeriodDTO lockedPeriod = new LockedPeriodDTO();
                lockedPeriod.setFromDate(row.getDate("fromDate"));
                lockedPeriod.setToDate(row.getDate("toDate"));
                lockedPeriod.setEnabled(row.getBoolean("enabled"));
                lockedPeriod.setName(row.getString("name"));
                lockedPeriod.setId(row.getInt("lockedPeriodId"));
                boolean parentFound = false;
                if (!row.isNull("activityId")) {
                    Integer activityId = row.getInt("activityId");
                    lockedPeriod.setParentId(activityId);
                    lockedPeriod.setParentType(activity.getEntityName());
                    parentFound = true;
                }
                if (!row.isNull("userDatabaseId")) {
                    Integer databaseId = row.getInt("userDatabaseId");
                    lockedPeriod.setParentId(databaseId);
                    lockedPeriod.setParentType(db.getEntityName());
                    parentFound = true;
                }
                if (!row.isNull("projectID")) {
                    Integer projectId = row.getInt("projectId");
                    lockedPeriod.setParentId(projectId);
                    lockedPeriod.setParentType(project.getEntityName());
                    parentFound = true;
                }
                if (!parentFound) {
                    Log.debug("Orphan lockedPeriod: No parent (UserDatabase/Activity/Project) found for LockedPeriod with Id=" + lockedPeriod.getId());
                }
                lockedPeriods.add(lockedPeriod);
            }
            callback.onSuccess(new LockedPeriodsResult(lockedPeriods));
        }
    });
}
Also used : ProjectDTO(org.activityinfo.shared.dto.ProjectDTO) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlResultSetRow(com.bedatadriven.rebar.sql.client.SqlResultSetRow) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO) LockedPeriodDTO(org.activityinfo.shared.dto.LockedPeriodDTO) RowHandler(com.bedatadriven.rebar.sql.client.util.RowHandler) SqlResultSet(com.bedatadriven.rebar.sql.client.SqlResultSet) LockedPeriodsResult(org.activityinfo.shared.command.GetLockedPeriods.LockedPeriodsResult) SqlResultCallback(com.bedatadriven.rebar.sql.client.SqlResultCallback)

Aggregations

SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1 SqlResultSet (com.bedatadriven.rebar.sql.client.SqlResultSet)1 SqlResultSetRow (com.bedatadriven.rebar.sql.client.SqlResultSetRow)1 SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)1 RowHandler (com.bedatadriven.rebar.sql.client.util.RowHandler)1 LockedPeriodsResult (org.activityinfo.shared.command.GetLockedPeriods.LockedPeriodsResult)1 ActivityDTO (org.activityinfo.shared.dto.ActivityDTO)1 LockedPeriodDTO (org.activityinfo.shared.dto.LockedPeriodDTO)1 ProjectDTO (org.activityinfo.shared.dto.ProjectDTO)1 UserDatabaseDTO (org.activityinfo.shared.dto.UserDatabaseDTO)1