Last active
December 16, 2015 20:10
-
-
Save danvalencia/5490611 to your computer and use it in GitHub Desktop.
Java version of the JCR node updating Groovy script I uploaded previously. This one does work, though.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.jackrabbit.commons.JcrUtils; | |
import javax.jcr.Node; | |
import javax.jcr.NodeIterator; | |
import javax.jcr.Repository; | |
import javax.jcr.RepositoryException; | |
import javax.jcr.Session; | |
import javax.jcr.SimpleCredentials; | |
import javax.jcr.query.Query; | |
import javax.jcr.query.QueryManager; | |
import javax.jcr.query.QueryResult; | |
/** | |
* @author Daniel Valencia (daniel@tacitknowledge.com) | |
*/ | |
public class AssetUpdater | |
{ | |
public static void main(String[] args) | |
{ | |
SessionManager sessionManager = new SessionManager("http://localhost:4502/crx/server"); | |
ResourceUpdater resourceUpdater = new ResourceUpdater(sessionManager.getSession()); | |
resourceUpdater.updateResource("foundation/components/image"); | |
} | |
} | |
class ResourceUpdater | |
{ | |
private Session session; | |
private QueryManager queryManager; | |
public ResourceUpdater(Session session) | |
{ | |
this.session = session; | |
try | |
{ | |
this.queryManager = this.session.getWorkspace().getQueryManager(); | |
} catch (RepositoryException e) | |
{ | |
throw new RuntimeException("Problem getting the workspace from the session " + session, e); | |
} | |
} | |
public void updateResource(String resourceType) | |
{ | |
System.out.println("Session is " + session); | |
System.out.println("First finding resource with type: " + resourceType); | |
updateResourcesWithType(resourceType); | |
NodeIterator nodeIterator = null; | |
try | |
{ | |
nodeIterator = findNodesWithResourceType(resourceType); | |
System.out.println("Node Iterator: $nodeIterator"); | |
System.out.println("nextNode: $nextNode"); | |
while (nodeIterator.hasNext()) | |
{ | |
System.out.println("There is a Node, haven't retrieved it yet!"); | |
Node theNode = nodeIterator.nextNode(); | |
System.out.println("There is a node! yay " + theNode.getPath()); | |
} | |
} catch (RepositoryException e) | |
{ | |
throw new RuntimeException("There was an issue searching for resources with resource type " + resourceType, e); | |
} | |
} | |
public void updateResourcesWithType(String resourceType) | |
{ | |
Node rootNode = null; | |
try | |
{ | |
rootNode = session.getRootNode(); | |
System.out.println("root node: " + rootNode.getPath()); | |
} catch (RepositoryException e) | |
{ | |
throw new RuntimeException("Problem getting root node ", e); | |
} | |
} | |
private NodeIterator findNodesWithResourceType(String resourceType) throws RepositoryException | |
{ | |
Query query = buildQuery(resourceType); | |
return executeQuery(query); | |
} | |
private Query buildQuery(String resourceType) throws RepositoryException | |
{ | |
String expression = String.format("/jcr:root/content/audiusa-retail//element(*,nt:unstructured)[@sling:resourceType='%s']", resourceType); | |
String language = "xpath"; | |
return queryManager.createQuery(expression, language); | |
} | |
private NodeIterator executeQuery(Query query) throws RepositoryException | |
{ | |
QueryResult results = query.execute(); | |
return results.getNodes(); | |
} | |
} | |
class SessionManager | |
{ | |
private String url; | |
public SessionManager(String url) | |
{ | |
this.url = url; | |
initializeJcrSession(); | |
} | |
private Session session; | |
public Session getSession() | |
{ | |
return session; | |
} | |
private Repository repository; | |
public Repository getRepository() | |
{ | |
return repository; | |
} | |
private void initializeJcrSession() | |
{ | |
SimpleCredentials creds = new SimpleCredentials("admin", "admin".toCharArray()); | |
try | |
{ | |
repository = JcrUtils.getRepository(url); | |
session = repository.login(creds, "crx.default"); | |
} catch (RepositoryException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>AssetUpdater</groupId> | |
<artifactId>AssetUpdater</artifactId> | |
<version>1.0</version> | |
<dependencies> | |
<dependency> | |
<groupId>javax.jcr</groupId> | |
<artifactId>jcr</artifactId> | |
<version>2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.jackrabbit</groupId> | |
<artifactId>jackrabbit-jcr2dav</artifactId> | |
<version>2.0.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-simple</artifactId> | |
<version>1.7.5</version> | |
</dependency> | |
</dependencies> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment