Search in sources :

Example 16 with PartnerDTO

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

the class FormResource method purgePartners.

private void purgePartners(ActivityDTO activity) {
    UserDatabaseDTO database = activity.getDatabase();
    if (!database.getAmOwner() && !database.isEditAllAllowed()) {
        UserPermission userPermission = userPermissionDAO.get().findUserPermissionByUserIdAndDatabaseId(getUser().getId(), database.getId());
        if (userPermission == null) {
            // user shouldn't be here if this is the case
            throw new WebApplicationException(Status.FORBIDDEN);
        } else {
            // only keep matching partner
            Partner allowedPartner = userPermission.getPartner();
            ListIterator<PartnerDTO> it = database.getPartners().listIterator();
            while (it.hasNext()) {
                PartnerDTO cur = it.next();
                if (cur.getId() != allowedPartner.getId()) {
                    it.remove();
                }
            }
        }
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) UserDatabaseDTO(org.activityinfo.shared.dto.UserDatabaseDTO) Partner(org.activityinfo.server.database.hibernate.entity.Partner) UserPermission(org.activityinfo.server.database.hibernate.entity.UserPermission)

Example 17 with PartnerDTO

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

the class GetPartnersDimensionHandler method execute.

@Override
public void execute(GetPartnersDimension cmd, ExecutionContext context, final AsyncCallback<PartnerResult> callback) {
    // if the filter doesn't contain any activity, database or indicator values, just return an empty list
    if (!cmd.getFilter().isRestricted(DimensionType.Database) && !cmd.getFilter().isRestricted(DimensionType.Activity) && !cmd.getFilter().isRestricted(DimensionType.Indicator)) {
        callback.onSuccess(new PartnerResult());
        return;
    }
    final Dimension dimension = new Dimension(DimensionType.Partner);
    PivotSites query = new PivotSites();
    query.setFilter(cmd.getFilter());
    query.setDimensions(dimension);
    query.setValueType(ValueType.DIMENSION);
    context.execute(query, new AsyncCallback<PivotSites.PivotResult>() {

        @Override
        public void onSuccess(PivotSites.PivotResult result) {
            Set<PartnerDTO> partners = new HashSet<PartnerDTO>();
            for (Bucket bucket : result.getBuckets()) {
                EntityCategory category = (EntityCategory) bucket.getCategory(dimension);
                if (category == null) {
                    Log.debug("Partner is null: " + bucket.toString());
                } else {
                    PartnerDTO partner = new PartnerDTO();
                    partner.setId(category.getId());
                    partner.setName(category.getLabel());
                    partners.add(partner);
                }
            }
            // sort partners by name (fallback on id)
            List<PartnerDTO> list = new ArrayList<PartnerDTO>(partners);
            Collections.sort(list, new Comparator<PartnerDTO>() {

                @Override
                public int compare(PartnerDTO p1, PartnerDTO p2) {
                    int result = p1.getName().compareToIgnoreCase(p2.getName());
                    if (result != 0) {
                        return result;
                    } else {
                        return ((Integer) p1.getId()).compareTo(p2.getId());
                    }
                }
            });
            callback.onSuccess(new PartnerResult(list));
        }

        @Override
        public void onFailure(Throwable caught) {
            callback.onFailure(caught);
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) Dimension(org.activityinfo.shared.report.model.Dimension) GetPartnersDimension(org.activityinfo.shared.command.GetPartnersDimension) Comparator(java.util.Comparator) PivotSites(org.activityinfo.shared.command.PivotSites) Bucket(org.activityinfo.shared.command.result.Bucket) ArrayList(java.util.ArrayList) List(java.util.List) EntityCategory(org.activityinfo.shared.report.content.EntityCategory) PartnerResult(org.activityinfo.shared.command.result.PartnerResult)

Example 18 with PartnerDTO

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

the class GetSitesHandler method toSite.

private SiteDTO toSite(SqlResultSetRow row) {
    SiteDTO model = new SiteDTO();
    model.setId(row.getInt("SiteId"));
    model.setLinked(row.getBoolean("Linked"));
    model.setActivityId(row.getInt("ActivityId"));
    model.setDate1(row.getDate("Date1"));
    model.setDate2(row.getDate("Date2"));
    model.setDateCreated(row.getDate("DateCreated"));
    model.setTimeEdited(row.getDouble("TimeEdited"));
    model.setLocationId(row.getInt("LocationId"));
    model.setLocationName(row.getString("LocationName"));
    model.setLocationAxe(row.getString("LocationAxe"));
    if (!row.isNull("x") && !row.isNull("y")) {
        model.setX(row.getDouble("x"));
        model.setY(row.getDouble("y"));
    }
    model.setComments(row.getString("Comments"));
    PartnerDTO partner = new PartnerDTO();
    partner.setId(row.getInt("PartnerId"));
    partner.setName(row.getString("PartnerName"));
    if (!row.isNull("ProjectId") && row.isNull("ProjectDateDeleted")) {
        ProjectDTO project = new ProjectDTO();
        project.setId(row.getInt("ProjectId"));
        project.setName(row.getString("ProjectName"));
        model.setProject(project);
    }
    model.setPartner(partner);
    return model;
}
Also used : ProjectDTO(org.activityinfo.shared.dto.ProjectDTO) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) SiteDTO(org.activityinfo.shared.dto.SiteDTO)

Example 19 with PartnerDTO

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

the class CreateSiteTest method testAdminBoundCreate.

@Test
@Ignore("WIP")
public void testAdminBoundCreate() throws CommandException {
    // create a new detached, client model
    SiteDTO newSite = new SiteDTO();
    newSite.setActivityId(4);
    newSite.setPartner(new PartnerDTO(1, "Foobar"));
    newSite.setDate1((new GregorianCalendar(2008, 12, 1)).getTime());
    newSite.setDate2((new GregorianCalendar(2009, 1, 3)).getTime());
    newSite.setAdminEntity(1, new AdminEntityDTO(1, 2, "Sud Kivu"));
    newSite.setAdminEntity(2, new AdminEntityDTO(2, 11, "Walungu"));
    newSite.setAdminEntity(3, null);
    newSite.setX(27.432);
    newSite.setY(1.23);
    newSite.setComments("huba huba");
    newSite.setProject(new ProjectDTO(1, "SomeProject"));
    // create command
    CreateSite cmd = new CreateSite(newSite);
    // execute the command
    setUser(1);
    newSite.setProject(new ProjectDTO(1, "SomeProject"));
    CreateResult result = execute(cmd);
    newSite.setId(result.getNewId());
    // try to retrieve what we've created
    PagingLoadResult<SiteDTO> loadResult = execute(GetSites.byId(newSite.getId()));
    Assert.assertEquals(1, loadResult.getData().size());
    SiteDTO secondRead = loadResult.getData().get(0);
    // confirm that the changes are there
    Assert.assertEquals("site.location.name", "Walungu", secondRead.getLocationName());
}
Also used : ProjectDTO(org.activityinfo.shared.dto.ProjectDTO) PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) CreateResult(org.activityinfo.shared.command.result.CreateResult) AdminEntityDTO(org.activityinfo.shared.dto.AdminEntityDTO) GregorianCalendar(java.util.GregorianCalendar) SiteDTO(org.activityinfo.shared.dto.SiteDTO) CreateSite(org.activityinfo.shared.command.CreateSite) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 20 with PartnerDTO

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

the class CreateSiteTest method unauthorized.

@Test(expected = NotAuthorizedException.class)
public void unauthorized() throws CommandException {
    // create a new detached, client model
    SiteDTO newSite = SiteDTOs.newSite();
    newSite.setPartner(new PartnerDTO(2, "Not NRC"));
    // create command
    CreateSite cmd = new CreateSite(newSite);
    // execute the command
    // bavon (only has access to his own partners in database 1)
    setUser(2);
    execute(cmd);
}
Also used : PartnerDTO(org.activityinfo.shared.dto.PartnerDTO) SiteDTO(org.activityinfo.shared.dto.SiteDTO) CreateSite(org.activityinfo.shared.command.CreateSite) Test(org.junit.Test)

Aggregations

PartnerDTO (org.activityinfo.shared.dto.PartnerDTO)23 CreateResult (org.activityinfo.shared.command.result.CreateResult)8 Test (org.junit.Test)8 ProjectDTO (org.activityinfo.shared.dto.ProjectDTO)6 SiteDTO (org.activityinfo.shared.dto.SiteDTO)6 AddPartner (org.activityinfo.shared.command.AddPartner)5 CreateSite (org.activityinfo.shared.command.CreateSite)4 DuplicateCreateResult (org.activityinfo.shared.command.result.DuplicateCreateResult)4 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)3 GregorianCalendar (java.util.GregorianCalendar)3 FormDialogCallback (org.activityinfo.client.page.common.dialog.FormDialogCallback)3 FormDialogTether (org.activityinfo.client.page.common.dialog.FormDialogTether)3 OnDataSet (org.activityinfo.server.database.OnDataSet)3 GetPartnersDimension (org.activityinfo.shared.command.GetPartnersDimension)3 PartnerResult (org.activityinfo.shared.command.result.PartnerResult)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 KeyGenerator (org.activityinfo.client.local.command.handler.KeyGenerator)2