Search in sources :

Example 1 with SendEmailCommand

use of net.geoprism.registry.command.SendEmailCommand in project geoprism-registry by terraframe.

the class ChangeRequest method apply.

@Override
public void apply() {
    // We aren't using 'isNew' here because isNew will be true until the transaction applies
    final boolean isApplied = this.isAppliedToDB();
    // Cache the Geo-Object label and type label on this object for sorting purposes
    this.getGeoObjectLabel().setLocaleMap(this.getGeoObjectDisplayLabel().getLocaleMap());
    this.getGeoObjectTypeLabel().setLocaleMap(this.getGeoObjectType().getLabel().getLocaleMap());
    super.apply();
    // Send an email to RMs telling them about this new CR
    try {
        if (!isApplied) {
            SingleActor createdBy = this.getCreatedBy();
            if (createdBy instanceof GeoprismUser) {
                // Get all RM's for the GOT and Org
                String rmRoleName = this.getGeoObjectType().getMaintainerRoleName();
                RoleDAOIF role = RoleDAO.findRole(rmRoleName);
                Set<SingleActorDAOIF> actors = role.assignedActors();
                List<String> toAddresses = new ArrayList<String>();
                for (SingleActorDAOIF actor : actors) {
                    if (actor.getType().equals(GeoprismUser.CLASS)) {
                        GeoprismUser geoprismUser = GeoprismUser.get(actor.getOid());
                        String email = geoprismUser.getEmail();
                        if (email != null && email.length() > 0 && !email.contains("@noreply")) {
                            toAddresses.add(email);
                        }
                    }
                }
                if (toAddresses.size() > 0) {
                    String subject = LocalizationFacade.getFromBundles("change.request.email.submit.subject");
                    String body = LocalizationFacade.getFromBundles("change.request.email.submit.body");
                    body = body.replaceAll("\\\\n", "\n");
                    body = body.replaceAll("\\{user\\}", ((GeoprismUser) createdBy).getUsername());
                    body = body.replaceAll("\\{geoobject\\}", this.getGeoObjectDisplayLabel().getValue());
                    String link = GeoregistryProperties.getRemoteServerUrl() + "cgr/manage#/registry/change-requests/" + this.getOid();
                    body = body.replaceAll("\\{link\\}", link);
                    // Aspects will weave in here and this will happen at the end of the transaction
                    new SendEmailCommand(subject, body, toAddresses.toArray(new String[toAddresses.size()])).doIt();
                }
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) GeoprismUser(net.geoprism.GeoprismUser) SingleActorDAOIF(com.runwaysdk.business.rbac.SingleActorDAOIF) RoleDAOIF(com.runwaysdk.business.rbac.RoleDAOIF) SendEmailCommand(net.geoprism.registry.command.SendEmailCommand) SingleActor(com.runwaysdk.system.SingleActor)

Aggregations

RoleDAOIF (com.runwaysdk.business.rbac.RoleDAOIF)1 SingleActorDAOIF (com.runwaysdk.business.rbac.SingleActorDAOIF)1 SingleActor (com.runwaysdk.system.SingleActor)1 ArrayList (java.util.ArrayList)1 GeoprismUser (net.geoprism.GeoprismUser)1 SendEmailCommand (net.geoprism.registry.command.SendEmailCommand)1