RemoveAFriend.java from Texai at Krugle
Show RemoveAFriend.java syntax highlighted
/*
* RemoveAFriend.java
*
* Created on December 21, 2006, 12:47 PM
*
* Description: Removes a Friend object.
*
* Copyright (C) 2006 Stephen L. Reed.
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.texai.kb.persistence.sample;
import java.io.File;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;
import net.sf.ehcache.CacheManager;
import org.apache.log4j.Logger;
import org.openrdf.model.URI;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.sail.nativerdf.NativeStore;
import org.texai.kb.persistence.RDFEntityManager;
import org.texai.util.TexaiException;
/**
*
* @author reed
*/
public final class RemoveAFriend {
/** the log4j logger */
private static final Logger LOGGER = Logger.getLogger(RemoveAFriend.class.getName());
/** the RDF entity manager */
private RDFEntityManager rdfEntityManager;
/** the friend entity id */
private String friendId;
/**
* Creates a new instance of RemoveAFriend.
*/
public RemoveAFriend() {
super();
}
/** Initializes this application. */
public void initialize() {
getClass().getClassLoader().setDefaultAssertionStatus(true); // optional
final File dataDirectory = new File(System.getProperties().getProperty("user.home"));
LOGGER.info("accessing the Sesame2 repository in " + dataDirectory.toString());
final String indices = "spoc,posc";
final Repository repository = new SailRepository(new NativeStore(dataDirectory, indices));
try {
repository.initialize();
} catch (final RepositoryException ex) {
throw new TexaiException(ex);
}
rdfEntityManager = new RDFEntityManager(repository);
LOGGER.info("clearing the Sesame2 repository");
rdfEntityManager.clear();
}
/** Creates and persists a Friend instance. */
public void createAndPersistAFriend() {
rdfEntityManager.setAutoCommit(false);
// populate the friend
final String name = "Stephen Reed";
final String birthday = "09-20";
final String gender = "male";
final Set<Object> thingsMade = new HashSet<Object>();
thingsMade.add(rdfEntityManager.getValueFactory().createURI("http://texai.org"));
final String firstName = "Stephen";
final String familyName = "Reed";
final Date dateOfBirth = (new GregorianCalendar(1951, 9, 20, 0, 0, 0)).getTime();
final Friend friend = new Friend(
name,
birthday,
gender,
thingsMade,
firstName,
familyName,
dateOfBirth);
// persist the friend and commit the transaction
rdfEntityManager.persist(friend);
friendId = friend.getAgentId();
rdfEntityManager.commit();
}
/** Removes the friend. */
public void removeFriend() {
// load the friend
final URI friendURI = rdfEntityManager.getValueFactory().createURI(friendId);
Friend friend = (Friend) rdfEntityManager.find(Friend.class, friendURI);
LOGGER.info("loaded via URI: " + friendId + " --> " + friend);
friend.setName("Stephen L. Reed");
// remove the friend and commit the transaction
friendId = friend.getAgentId();
rdfEntityManager.remove(friend);
rdfEntityManager.commit();
// attempt to load the friend
friend = (Friend) rdfEntityManager.find(Friend.class, friendURI);
if (friend == null) {
LOGGER.info("as expected, cannot load a removed entity via URI: " + friendId);
} else {
LOGGER.info("error, loaded a removed entity via URI: " + friendId + " --> " + friend);
}
}
/** Finalizes this application. */
public void finalization() {
CacheManager.getInstance().shutdown();
rdfEntityManager.close();
LOGGER.info("LoadAFriend completed");
}
/** Executes this application.
*
* @param args the command line arguments, which are not used
*/
public static void main(final String[] args) {
final RemoveAFriend deleteAFriend = new RemoveAFriend();
deleteAFriend.initialize();
deleteAFriend.createAndPersistAFriend();
deleteAFriend.removeFriend();
deleteAFriend.finalization();
}
}
See more files for this project here