Return-Path: Delivered-To: apmail-sling-commits-archive@www.apache.org Received: (qmail 57645 invoked from network); 4 Nov 2010 02:28:42 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 4 Nov 2010 02:28:42 -0000 Received: (qmail 10046 invoked by uid 500); 4 Nov 2010 02:29:14 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 10013 invoked by uid 500); 4 Nov 2010 02:29:14 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 10006 invoked by uid 99); 4 Nov 2010 02:29:14 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Nov 2010 02:29:14 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 04 Nov 2010 02:29:13 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id A09B023889E1; Thu, 4 Nov 2010 02:27:59 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1030807 - in /sling/trunk/samples/slingshot: pom.xml src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java Date: Thu, 04 Nov 2010 02:27:59 -0000 To: commits@sling.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20101104022759.A09B023889E1@eris.apache.org> Author: cziegeler Date: Thu Nov 4 02:27:59 2010 New Revision: 1030807 URL: http://svn.apache.org/viewvc?rev=1030807&view=rev Log: Add simple automatic taggin service Added: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java (with props) Modified: sling/trunk/samples/slingshot/pom.xml Modified: sling/trunk/samples/slingshot/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/pom.xml?rev=1030807&r1=1030806&r2=1030807&view=diff ============================================================================== --- sling/trunk/samples/slingshot/pom.xml (original) +++ sling/trunk/samples/slingshot/pom.xml Thu Nov 4 02:27:59 2010 @@ -108,6 +108,10 @@ compile + org.osgi + org.osgi.compendium + + org.apache.sling org.apache.sling.api 2.1.0 Added: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java URL: http://svn.apache.org/viewvc/sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java?rev=1030807&view=auto ============================================================================== --- sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java (added) +++ sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java Thu Nov 4 02:27:59 2010 @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.sample.slingshot.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; + +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Properties; +import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; +import org.apache.sling.api.resource.LoginException; +import org.apache.sling.api.resource.PersistableValueMap; +import org.apache.sling.api.resource.PersistenceException; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.resource.ResourceResolver; +import org.apache.sling.api.resource.ResourceResolverFactory; +import org.apache.sling.sample.slingshot.Constants; +import org.osgi.service.event.Event; +import org.osgi.service.event.EventHandler; + +/** + * This is a samle for observation which adds tags to new resources. + */ +@Component(immediate=true) +@Service(value=EventHandler.class) +@Properties({ + @Property(name="service.description", + value="Apache Sling - Slingshot Tagging Service"), + @Property(name="event.topics", value=org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED) +}) +public class AutomaticTaggingService + implements EventHandler { + + @Reference + private ResourceResolverFactory resourceResolverFactory; + + private final Random random = new Random(System.currentTimeMillis()); + + public void handleEvent(final Event event) { + final String path = (String)event.getProperty(org.apache.sling.api.SlingConstants.PROPERTY_PATH); + if ( path != null && path.startsWith(Constants.ALBUMS_ROOT) ) { + ResourceResolver resolver = null; + try { + resolver = this.resourceResolverFactory.getAdministrativeResourceResolver(null); + final Resource r = resolver.getResource(path); + if ( r != null && r.isResourceType(Constants.RESOURCETYPE_PHOTO) ) { + // to add the mixin node type, we have to adopt to a node + final Node node = r.adaptTo(Node.class); + if ( node != null && !node.isNodeType("slingshot:Photo")) { + node.addMixin("slingshot:Photo"); + node.getSession().save(); + final PersistableValueMap pvm = r.adaptTo(PersistableValueMap.class); + if ( pvm != null ) { + final int tagsValue = this.random.nextInt(8); + final List tags = new ArrayList(); + if ( (tagsValue & 1) == 1 ) { + tags.add("ApacheCon"); + } + if ( (tagsValue & 2) == 2 ) { + tags.add("Vacation"); + } + if ( (tagsValue & 4) == 4 ) { + tags.add("Cool"); + } + pvm.put("slingshot:tags", tags.toArray(new String[tags.size()])); + try { + pvm.save(); + } catch (PersistenceException e) { + // we just ignore this for now + } + } + } + } + + } catch (final RepositoryException e) { + // this should never happen, therefore we ignore + } catch (final LoginException e) { + // this should never happen, therefore we ignore + } finally { + if ( resolver != null ) { + resolver.close(); + } + } + } + } +} Propchange: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Propchange: sling/trunk/samples/slingshot/src/main/java/org/apache/sling/sample/slingshot/impl/AutomaticTaggingService.java ------------------------------------------------------------------------------ svn:mime-type = text/plain