Yoseph
Yoseph

Reputation: 41

ATG Repository API

Im trying to update multiple records via an ATG class extending GenericService. However im running against a roadblock. How do I do a multiple insert query where i can keep adding all the items / rows into the cached object and then do a single command sync with the table using item.add() ?

Sample code

the first part is to clear out the rows in the table before insertion happens (mighty helpful if anyone knows of a way to clear all rows in a table without having to loop through and delete one by one).

    MutableRepository repo = (MutableRepository) feedRepository;
    RepositoryView view = null; 

    try{
        view = getFeedRepository().getView(getFeedRepositoryFeedDataDescriptorName());
        RepositoryItem[] items = null;
        if(view != null){        
            QueryBuilder qb = view.getQueryBuilder();
            Query getFeedsQuery = qb.createUnconstrainedQuery();
            items = view.executeQuery(getFeedsQuery);
        }
        if(items != null && items.length>0){
            // remove all items in the repository
            for(RepositoryItem item :items){
                repo.removeItem(item.getRepositoryId(), getFeedRepositoryFeedDataDescriptorName());
            }
        }



        for(RSSFeedObject rfo : feedEntries){
            MutableRepositoryItem feedItem = repo.createItem(getFeedRepositoryFeedDataDescriptorName());

            feedItem.setPropertyValue(DB_COL_AUTHOR, rfo.getAuthor());
            feedItem.setPropertyValue(DB_COL_FEEDURL, rfo.getFeedUrl());
            feedItem.setPropertyValue(DB_COL_TITLE, rfo.getTitle());
            feedItem.setPropertyValue(DB_COL_FEEDURL, rfo.getPublishedDate());

            RepositoryItem item = repo.addItem(feedItem) ;
        }

Upvotes: 2

Views: 7060

Answers (2)

jsears
jsears

Reputation: 4641

ATG does not provide support for deleting multiple records in a single SQL statement. You can use transactions, as @chrisjleu suggests, but there is no way to do the equivalent of a DELETE WHERE ID IN {"1", "2", ...}. Your code looks correct.

It is possible to invoke stored procedures or execute custom SQL through an ATG Repository, but that isn't generally recommended for portability/maintenance reasons. If you did that, you would also need to flush the appropriate portions of the item/query caches manually.

Upvotes: 0

chrisjleu
chrisjleu

Reputation: 4380

The way I interpret your question is that you want to add multiple repository items to your repository but you want to do it fairly efficiently at a database level. I suggest you make use of the Java Transaction API as recommended in the ATG documentation, like so:

TransactionManager tm = ...
TransactionDemarcation td = new TransactionDemarcation ();
try {
  try {
    td.begin (tm);

    ... do repository item work ...
  }
  finally {
    td.end ();
  }
}
catch (TransactionDemarcationException exc) {
  ... handle the exception ...
}

Assuming you are using a SQL repository in your example, the SQL INSERT statements will be issued after each call to addItem but will not be committed until/if the transaction completes successfully.

Upvotes: 4

Related Questions