Documente Academic
Documente Profesional
Documente Cultură
Part I
Chapter 12 • The Repository API
12-2
Dynamusic Repository Access
• In examples so far, all Dynamusic repository
access has used standard servlet beans and form
handlers
• "Manager" components provide specialized
access to repositories that isn't supported by the
standard components
12-3
Example: SongsManager
SongsManager
repository
userRepository
addArtistToSong()
createArtistFromUser()
UploadSong
Handler firstArtistByName()
12-4
Repository API Overview
• The ATG Repository API is a set of interfaces used for
generic data access
12-5
Retrieving a Repository Item by ID
SongsManager.java
public void addArtistToSong(String pSongid, String
pArtistid) throws RepositoryException {
Repository repository = getRepository();
try {
RepositoryItem artistItem =
repository.getItem(pArtistid,"artist");
if (ifLoggingDebug())
logDebug("adding artist: " +
artistItem.getPropertyValue("name"));
}
catch (RepositoryException e) {
if (isLoggingError()) {
logError("Unable to add artist to song",
e);
}
throw e;
}
}…
12-6
Mutable Repositories (1)
• Repository and RepositoryItem provide a
read-only view of repository elements
• MutableRepository is an extension that
allows repository elements to be:
— Created
— Updated
— Deleted
• Manipulating a MutableRepositoryItem (a
local copy of the data) only changes the
repository when an explicit call to
updateItem()or addItem()is made
12-7
Mutable Repositories (2)
• Allow an item to be “checked out” of the
repository, modified, and then resubmitted (so
that the change will be reflected in the
underlying data store)
• Provide a locking mechanism which prohibits
any access of the data store while it is being
modified
12-8
Updating an Existing Item
• Get the item using getItemForUpdate()
• Change values by calling the item's
setPropertyValue() method
• Save changes to the repository using
updateItem()
12-9
Example: Using Mutable Repositories
SongsManager.java
12-10
Creating a New Item
1. Create a MutableRepositoryItem using
MutableRepository.createItem()
method
12-11
Example: Creating a New Item
SongsManager.java
public String createArtistFromUser(String pUserid) throws
RepositoryException {
MutableRepository mutRepos = (MutableRepository)
getRepository();
RepositoryItem user =
getUserRepository().getItem(pUserid, "user");
String username =
(String)user.getPropertyValue("firstName") + " " +
user.getPropertyValue("lastName");
1 MutableRepositoryItem mutArtistItem =
mutRepos.createItem("artist");
2 mutArtistItem.setPropertyValue("name", username);
…copy all relevant properties…
3 mutRepos.addItem(mutArtistItem);
return mutArtistItem.getRepositoryId();
}
12-12
Querying the Repository
• The most straightforward way of executing
queries in a repository is using RQL using the
RqlStatement class
• You can create an RQLStatement from a string
using parseRQLStatement()
RqlStatement statement =
RqlStatement.parseRqlStatement(
rql string);
• RQL statements are executed against a
RepositoryView
12-13
Parameterized RQL
• The syntax for passing parameters to an RQL query is
different for the API than RQLQueryForEach
— ?n (where "n" is the parameter number)
if (artistList != null) {
return artistList[0];
}
return null;
}
12-15
Named Queries (1)
• You can also define a query in the SQL
repository definition file
songs.xml Example: a named
… query to return all
<item-descriptor name="song"> jazz songs in the
… repository
<named-query>
<rql-query>
<query-name>jazzSongs</query-name>
<rql>genre = "jazz"</rql>
</rql-query>
</named-query>
</item-descriptor>
…
12-16
Named Queries (2)
• Two ways to execute a named query
- programmatically (use - in JSP (use the
the NamedQueryView NamedQueryForEach
object) droplet)
SongsManager
jazzSongs
12-17
Named Queries: Programmatic Execution
SongsManager.java SongsManager
public RepositoryItem[] getJazzSongs() { jazzSongs
try {
RepositoryView songView =
getRepository().getView("song");
if(songView instanceof NamedQueryView) {
1 NamedQueryView nameView =
(NamedQueryView)songView;
Query namedQuery =
2 nameView.getNamedQuery("jazzSongs");
RepositoryItem[] results =
nameView.executeQuery(namedQuery);
}
return results;
}
…
12-18
Named Queries: JSP
jazzsongs.jsp
<dsp:droplet
name="/atg/dynamo/droplet/NamedQueryForEach">
<dsp:param name="repository"
bean="/dynamusic/SongsRepository" />
<dsp:param name="queryName" value="jazzSongs" />
<dsp:param name="itemDescriptor" value="song" />
…
<dsp:oparam name="output">
<li>
<dsp:a href="song.jsp">
<dsp:param name="itemId" param="element.id"/>
<dsp:valueof param="element.title"/>
</dsp:a>
</dsp:oparam>
…
</dsp:droplet>
12-19
Summary
In this chapter, you learned how to
• Use the Repository API to retrieve, create,
12-20
For More Information
• Repository Guide
— "Repository API"
— "Repository Queries"
• API Reference
— atg.repository package
12-21
Exercises
• Implement the deleteAlbumsByArtist
method using Repository API calls
• Testing using a provided JSP, passing in the ID
of an Artist
12-22