Search in sources :

Example 6 with UserDatabaseDTO

use of org.activityinfo.shared.dto.UserDatabaseDTO 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)

Example 7 with UserDatabaseDTO

use of org.activityinfo.shared.dto.UserDatabaseDTO in project activityinfo by bedatadriven.

the class ExportSitesServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Set<Integer> activities = new HashSet<Integer>();
    if (req.getParameterValues("a") != null) {
        for (String activity : req.getParameterValues("a")) {
            activities.add(Integer.parseInt(activity));
        }
    }
    Filter filter = FilterUrlSerializer.fromQueryParameter(req.getParameter("filter"));
    SchemaDTO schema = dispatcher.execute(new GetSchema());
    SiteExporter export = new SiteExporter(dispatcher);
    for (UserDatabaseDTO db : schema.getDatabases()) {
        for (ActivityDTO activity : db.getActivities()) {
            if (!filter.isRestricted(DimensionType.Activity) || filter.getRestrictions(DimensionType.Activity).contains(activity.getId())) {
                export.export(activity, filter);
            }
        }
    }
    export.done();
    resp.setContentType("application/vnd.ms-excel");
    if (req.getHeader("User-Agent").indexOf("MSIE") != -1) {
        resp.addHeader("Content-Disposition", "attachment; filename=ActivityInfo.xls");
    } else {
        resp.addHeader("Content-Disposition", "attachment; filename=" + ("ActivityInfo Export " + new Date().toString() + ".xls").replace(" ", "_"));
    }
    OutputStream os = resp.getOutputStream();
    export.getBook().write(os);
}
Also used : Filter(org.activityinfo.shared.command.Filter) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) OutputStream(java.io.OutputStream) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) GetSchema(org.activityinfo.shared.command.GetSchema) Date(java.util.Date) HashSet(java.util.HashSet)

Example 8 with UserDatabaseDTO

use of org.activityinfo.shared.dto.UserDatabaseDTO in project activityinfo by bedatadriven.

the class RootResource method getDatabaseSchemaCsv.

@GET
@Path("/database/{id}/schema.csv")
public Response getDatabaseSchemaCsv(@PathParam("id") int id) {
    UserDatabaseDTO db = getDatabaseSchema(id);
    SchemaCsvWriter writer = new SchemaCsvWriter();
    writer.write(db);
    return Response.ok().type("text/css").header("Content-Disposition", "attachment; filename=schema_" + id + ".csv").entity(writer.toString()).build();
}
Also used : SchemaCsvWriter(org.activityinfo.shared.dto.SchemaCsvWriter) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 9 with UserDatabaseDTO

use of org.activityinfo.shared.dto.UserDatabaseDTO in project activityinfo by bedatadriven.

the class SiteChangeServlet method sendNotifications.

@VisibleForTesting
void sendNotifications(int editorUserId, int siteId, ChangeType type) {
    User user = entityManager.get().find(User.class, editorUserId);
    /*
         * For our purposes, the user who initiated the change will be
         * considered the authenticated user for this thread
         */
    authProvider.set(user);
    SiteResult siteResult = dispatcher.execute(GetSites.byId(siteId));
    SiteDTO siteDTO = siteResult.getData().get(0);
    SchemaDTO schemaDTO = dispatcher.execute(new GetSchema());
    ActivityDTO activityDTO = schemaDTO.getActivityById(siteDTO.getActivityId());
    UserDatabaseDTO userDatabaseDTO = activityDTO.getDatabase();
    Date date = new Date();
    List<User> recipients = findRecipients(userDatabaseDTO.getId());
    for (User recipient : recipients) {
        try {
            // themselves!
            if (recipient.getId() != editorUserId) {
                LOGGER.info("sending sitechange notification email to " + recipient.getEmail());
                UpdateMessageBuilder message = new UpdateMessageBuilder();
                message.setDate(date);
                message.setEditor(user);
                message.setRecipient(recipient);
                message.setUserDatabaseDTO(userDatabaseDTO);
                message.setSiteDTO(siteDTO);
                message.setActivityDTO(activityDTO);
                message.setChangeType(type);
                mailSender.get().send(message.build());
            }
        } catch (Throwable t) {
            LOGGER.warning("failed sending notification email to " + recipient.getName() + " <" + recipient.getEmail() + ">: " + t.getMessage());
            t.printStackTrace();
        }
    }
}
Also used : User(org.activityinfo.server.database.hibernate.entity.User) SiteResult(org.activityinfo.shared.command.result.SiteResult) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) SiteDTO(org.activityinfo.shared.dto.SiteDTO) ActivityDTO(org.activityinfo.shared.dto.ActivityDTO) SchemaDTO(org.activityinfo.shared.dto.SchemaDTO) GetSchema(org.activityinfo.shared.command.GetSchema) Date(java.util.Date) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 10 with UserDatabaseDTO

use of org.activityinfo.shared.dto.UserDatabaseDTO in project activityinfo by bedatadriven.

the class DbListPresenterTest method commandShouldBePreparedProperly.

@Test
public void commandShouldBePreparedProperly() {
    Capture<CreateEntity> cmd = new Capture<CreateEntity>();
    expectDispatch(new GetSchema(), schema);
    captureDispatch(cmd);
    replay(dispatcher);
    UserDatabaseDTO newDb = new UserDatabaseDTO();
    newDb.setCountry(new CountryDTO(31, "Haiti"));
    newDb.setName("My Db");
    createPresenter();
    presenter.save(newDb, niceFormDialogMock());
    assertTrue("command was dispatched", cmd.hasCaptured());
    assertThat((Integer) cmd.getValue().getProperties().get("countryId"), is(equalTo(31)));
}
Also used : CreateEntity(org.activityinfo.shared.command.CreateEntity) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) CountryDTO(org.activityinfo.shared.dto.CountryDTO) GetSchema(org.activityinfo.shared.command.GetSchema) Capture(org.easymock.Capture) Test(org.junit.Test)

Aggregations

UserDatabaseDTO (org.activityinfo.shared.dto.UserDatabaseDTO)25 ActivityDTO (org.activityinfo.shared.dto.ActivityDTO)13 GetSchema (org.activityinfo.shared.command.GetSchema)10 SchemaDTO (org.activityinfo.shared.dto.SchemaDTO)10 Test (org.junit.Test)8 CreateResult (org.activityinfo.shared.command.result.CreateResult)4 OnDataSet (org.activityinfo.server.database.OnDataSet)3 CreateEntity (org.activityinfo.shared.command.CreateEntity)3 Filter (org.activityinfo.shared.command.Filter)3 AttributeGroupDTO (org.activityinfo.shared.dto.AttributeGroupDTO)3 CountryDTO (org.activityinfo.shared.dto.CountryDTO)3 Date (java.util.Date)2 User (org.activityinfo.server.database.hibernate.entity.User)2 SiteResult (org.activityinfo.shared.command.result.SiteResult)2 IndicatorDTO (org.activityinfo.shared.dto.IndicatorDTO)2 LocationTypeDTO (org.activityinfo.shared.dto.LocationTypeDTO)2 LockedPeriodDTO (org.activityinfo.shared.dto.LockedPeriodDTO)2 ProjectDTO (org.activityinfo.shared.dto.ProjectDTO)2 SiteDTO (org.activityinfo.shared.dto.SiteDTO)2 SqlResultCallback (com.bedatadriven.rebar.sql.client.SqlResultCallback)1