Deleting profiles and all associated orders

Nothing fancy, just a simple component to delete all profiles with no activity in the last 180 days. We needed this in our development and QA environments to reclaim disk space.

import javax.transaction.TransactionManager;

import atg.commerce.order.*;
import atg.commerce.pricing.*;
import atg.dtm.*;
import atg.userprofiling.ProfileTools;

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.sql.*;

import atg.adapter.gsa.*;

import atg.core.util.*;

import atg.nucleus.*;

import atg.repository.*;
import atg.repository.rql.*;

public class deleteProfile extends GenericService {

  public deleteProfile() {}

  protected ProfileTools mProfileTools = null;

  public void setProfileTools(ProfileTools pProfileTools){
    mProfileTools = pProfileTools;
  }

  public ProfileTools getProfileTools(){
    return mProfileTools;
  }

  protected OrderTools mOrderTools = null;

  public void setOrderTools(OrderTools pOrderTools) {
    mOrderTools = pOrderTools;
  }

  public OrderTools getOrderTools() {
    return mOrderTools;
  }

  String profileId;

  public void setProfileId(String profileId) {
    this.profileId = profileId;
  }

  public String getProfileId() {
    return profileId;
  }

  public void deleteProfiles () {
    try {

      InitialContext ic = new InitialContext();
      DataSource ds = (DataSource)ic.lookup("java:/atgcore_ds");
      Connection conn = ds.getConnection();
      ResultSet rst = conn.createStatement().executeQuery("select id from dps_user where lastactivity_date < sysdate - 180");
      PreparedStatement pst = conn.prepareStatement("select order_id from dcspp_order where profile_id = ?");
      GSARepository r = (GSARepository)Nucleus.getGlobalNucleus().resolveName("/atg/userprofiling/ProfileAdapterRepository");
      MutableRepository mr = (MutableRepository)r;
      OrderManager om = getOrderTools().getProfileTools().getOrderManager();
      RepositoryUtils ru = new RepositoryUtils();
      while (rst.next()) {
        pst.setString(1,rst.getString(1));
        ResultSet rstOrders = pst.executeQuery();
        while (rstOrders.next()) {
          om.removeOrder(rstOrders.getString(1));
        }
        ru.removeReferencesToItem(ru.getMutableRepositoryItem(mr, rst.getString(1), "user"));
        mr.removeItem(rst.getString(1),"user");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

...and the component properties file...

$class=deleteProfile
$scope=session
profileId=1
orderTools=/atg/commerce/order/OrderTools/
profileTools=/atg/userprofiling/ProfileTools

2 comments for “Deleting profiles and all associated orders

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.