Return-Path: X-Original-To: apmail-cayenne-user-archive@www.apache.org Delivered-To: apmail-cayenne-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D265111D78 for ; Sun, 20 Jul 2014 06:24:47 +0000 (UTC) Received: (qmail 79709 invoked by uid 500); 20 Jul 2014 06:24:47 -0000 Delivered-To: apmail-cayenne-user-archive@cayenne.apache.org Received: (qmail 79640 invoked by uid 500); 20 Jul 2014 06:24:47 -0000 Mailing-List: contact user-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@cayenne.apache.org Delivered-To: mailing list user@cayenne.apache.org Received: (qmail 79629 invoked by uid 99); 20 Jul 2014 06:24:47 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 20 Jul 2014 06:24:47 +0000 X-ASF-Spam-Status: No, hits=2.9 required=5.0 tests=HTML_MESSAGE,MANY_SPAN_IN_TEXT,RCVD_IN_DNSWL_NONE,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [203.59.1.225] (HELO icp-osb-irony-out8.external.iinet.net.au) (203.59.1.225) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 20 Jul 2014 06:24:42 +0000 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApgBAOhfy1N8lDRu/2dsb2JhbAANTIJHgRnFYwEJh0SBHoYhiXiqQZd0EwSPR4JCD0QkgRgFomOQRw X-IronPort-AV: E=Sophos;i="5.01,694,1399996800"; d="scan'208,217";a="837396489" Received: from unknown (HELO [10.1.1.4]) ([124.148.52.110]) by icp-osb-irony-out8.iinet.net.au with ESMTP; 20 Jul 2014 14:24:09 +0800 From: D Tim Cummings Content-Type: multipart/alternative; boundary="Apple-Mail=_7CE9F7FD-BFE9-48D9-A0FF-94DFFEFCFF96" Subject: How to link to user record who made changes in audit trail Message-Id: <090A08F2-A812-4762-899F-2A4CBDA61EFF@triptera.com.au> Date: Sun, 20 Jul 2014 16:23:59 +1000 To: user@cayenne.apache.org Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) X-Mailer: Apple Mail (2.1878.6) X-Virus-Checked: Checked by ClamAV on apache.org --Apple-Mail=_7CE9F7FD-BFE9-48D9-A0FF-94DFFEFCFF96 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hi all I want to set up a simple audit trail which basically links who was the = person to create a record to that record. I am using Cayenne 3.2M1 and = Tapestry 5.3.7. I figure I need to set up a data channel filter to catch = changes to that record and then save a link to the user who made the = change.=20 The problem is, if I save the user in the data channel filter object = when someone logs in, then all created records link to the last logged = in user.=20 If I save the user in a ThreadLocal in the data channel filter object, I = get a different user each time (often blank) because the data channel = filter seem to run in its own thread which changes each time. I have been watching the excellent and now freely available podcast by = Andrus Adamchik presented to WebObjects developers "Advanced Apache = Cayenne" where he talks about lifecycle events, (callbacks, listeners), = caching, data channel filters, clustering, in cayenne 3.2M1. https://itunes.apple.com/podcast/webobjects-podcasts/id270165303?mt=3D2# In Andrus's sample code he uses AuditableProcessor, but I couldn't think = how to use it to solve this problem. Here is a copy of my listener/data channel filter with the ThreadLocal = code. Thanks=20 Tim public class AuditListener implements DataChannelFilter { =20 private static final Logger logger =3D = LoggerFactory.getLogger(AuditListener.class); =20 private ThreadLocal tlUserId; @PrePersist(entityAnnotations=3DTagCreateCancel.class) void doPrePersist(DataObject object) { if ( object instanceof AuditableCreateCancel ) { AuditableCreateCancel acc =3D (AuditableCreateCancel) object; TblPerson user =3D getTheUser(object.getObjectContext()); acc.setTblPersonCreate(user); } } =20 private TblPerson getTheUser(ObjectContext oc) { Thread t =3D Thread.currentThread(); Integer idUser =3D tlUserId.get(); if ( idUser =3D=3D null ) { logger.info("Thread " + t.getId() + " idUser =3D=3D null "); return null; } logger.info("Thread " + t.getId() + " Looking for TblPerson " + = idUser); TblPerson p =3D Cayenne.objectForPK(oc, TblPerson.class, = idUser.intValue()); return p; } @Override public void init(DataChannel channel) { tlUserId =3D new ThreadLocal(); } @Override public QueryResponse onQuery(ObjectContext originatingContext, Query = query, DataChannelFilterChain filterChain) { return filterChain.onQuery(originatingContext, query); } @Override public GraphDiff onSync(ObjectContext originatingContext, GraphDiff = changes, int syncType, DataChannelFilterChain filterChain) { try { return filterChain.onSync(originatingContext, changes, syncType); } finally { // } } =20 public void setTheUserId(int idUser) { Thread t =3D Thread.currentThread(); logger.info("Thread " + t.getId() + " setTheUserId " + idUser); tlUserId.set(Integer.valueOf(idUser)); } =20 } --Apple-Mail=_7CE9F7FD-BFE9-48D9-A0FF-94DFFEFCFF96--