You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Scott Koenig <sc...@gmail.com> on 2011/01/12 22:43:48 UTC

using a custom Converter for elements of a Collection

Hello,

In my company's internal CMS, we are implementing two new asset types,
Videos and Playlists (collections of Videos). In the Video UI, the user
designates which Playlists are to be associated with a given Video, and
these values are posted on save via an array called "playlists" which
contains the UIDs of each Playlist. These are represented as an
ArrayList<Playlist> in the Video POJO.

We have implemented a PlaylistConverter to convert each String UID to a
Playlist instance (and back), and going off of
http://struts.apache.org/2.1.8/docs/type-conversion.html#TypeConversion-CollectionandMapSupport,
added Element_playlists=[converterpackage].PlaylistConverter to
Video-conversion.properties.

Save was not committing and there seemed to be some conversion error, so I
stepped through the conversion process to try and determine what was going
wrong. Everything seems to be fine at the Collection level; it properly
determines that the toType of the collection is List and the memberType is
Playlist. However, when XWorkConverter.getConverter() tries to find the
converter for the individual members, even though Element_playlists=class
[converterpackage].PlaylistConverter is present in the mappings, the
property String passed in is still "playlists" instead of
"Element_playlists" and thus it does not hook up the playlist ID with the
converter and falls back to the default converter (which cannot handle it
and is what is raising the error).

Any guidance as to what could be going wrong here would be greatly
appreciated.

Thank you very much,
~~Scott


PS--If it's useful, our system depends on the following libraries:

ognl-3.0.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar

Re: using a custom Converter for elements of a Collection

Posted by Scott Koenig <sc...@gmail.com>.
I was able to find a solution by creating a second converter,
PlaylistsConverter, which returns a List of Playlists instead:

public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
        if (arg1.length > 0 && arg1[0].trim() != "") {
            List<Playlist> playlists = new ArrayList<Playlist>();
            try {
                for (int i = 0; i < arg1.length; i++) {
                    if (arg1[i] != null && !arg1[i].trim().equals("")) {
                        playlists.add((Playlist)
repositoryAppService.retrieveContentById(Long.valueOf(arg1[i])));
                    }
                }
                return playlists;
            } catch (Exception e) {
                throw new TypeConversionException(e);
            }
        } else {
            return null;
        }
    }

This serves my purposes, but I would still prefer to reuse
PlaylistConverter, which is used by Playlists themselves to handle what we
call source Playlists:

@Override
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
        if (arg1.length > 0 && arg1[0].trim() != "") {
            List<Playlist> playlists = new ArrayList<Playlist>();
            try {
                for (int i = 0; i < arg1.length; i++) {
                    if (arg1[i] != null && !arg1[i].trim().equals("")) {
                        playlists.add((Playlist)
repositoryAppService.retrieveContentById(Long.valueOf(arg1[i])));
                    }
                }
                return playlists;
            } catch (Exception e) {
                throw new TypeConversionException(e);
            }
        } else {
            return null;
        }
    }

I'm happy to provide additional code, but I don't want to inundate you with
chaff... can you give me some idea of the wheat you are looking for?

Thanks,
~~Scott

On Wed, Jan 12, 2011 at 7:08 PM, <st...@gmail.com> wrote:

> Can we see your related files?
>
> On Wed, Jan 12, 2011 at 3:43 PM, Scott Koenig <scott.l.koenig@gmail.com
> >wrote:
>
> > Hello,
> >
> > In my company's internal CMS, we are implementing two new asset types,
> > Videos and Playlists (collections of Videos). In the Video UI, the user
> > designates which Playlists are to be associated with a given Video, and
> > these values are posted on save via an array called "playlists" which
> > contains the UIDs of each Playlist. These are represented as an
> > ArrayList<Playlist> in the Video POJO.
> >
> > We have implemented a PlaylistConverter to convert each String UID to a
> > Playlist instance (and back), and going off of
> >
> >
> http://struts.apache.org/2.1.8/docs/type-conversion.html#TypeConversion-CollectionandMapSupport
> > ,
> > added Element_playlists=[converterpackage].PlaylistConverter to
> > Video-conversion.properties.
> >
> > Save was not committing and there seemed to be some conversion error, so
> I
> > stepped through the conversion process to try and determine what was
> going
> > wrong. Everything seems to be fine at the Collection level; it properly
> > determines that the toType of the collection is List and the memberType
> is
> > Playlist. However, when XWorkConverter.getConverter() tries to find the
> > converter for the individual members, even though Element_playlists=class
> > [converterpackage].PlaylistConverter is present in the mappings, the
> > property String passed in is still "playlists" instead of
> > "Element_playlists" and thus it does not hook up the playlist ID with the
> > converter and falls back to the default converter (which cannot handle it
> > and is what is raising the error).
> >
> > Any guidance as to what could be going wrong here would be greatly
> > appreciated.
> >
> > Thank you very much,
> > ~~Scott
> >
> >
> > PS--If it's useful, our system depends on the following libraries:
> >
> > ognl-3.0.jar
> > struts2-core-2.1.8.1.jar
> > xwork-core-2.1.6.jar
> >
>

Re: using a custom Converter for elements of a Collection

Posted by st...@gmail.com.
Can we see your related files?

On Wed, Jan 12, 2011 at 3:43 PM, Scott Koenig <sc...@gmail.com>wrote:

> Hello,
>
> In my company's internal CMS, we are implementing two new asset types,
> Videos and Playlists (collections of Videos). In the Video UI, the user
> designates which Playlists are to be associated with a given Video, and
> these values are posted on save via an array called "playlists" which
> contains the UIDs of each Playlist. These are represented as an
> ArrayList<Playlist> in the Video POJO.
>
> We have implemented a PlaylistConverter to convert each String UID to a
> Playlist instance (and back), and going off of
>
> http://struts.apache.org/2.1.8/docs/type-conversion.html#TypeConversion-CollectionandMapSupport
> ,
> added Element_playlists=[converterpackage].PlaylistConverter to
> Video-conversion.properties.
>
> Save was not committing and there seemed to be some conversion error, so I
> stepped through the conversion process to try and determine what was going
> wrong. Everything seems to be fine at the Collection level; it properly
> determines that the toType of the collection is List and the memberType is
> Playlist. However, when XWorkConverter.getConverter() tries to find the
> converter for the individual members, even though Element_playlists=class
> [converterpackage].PlaylistConverter is present in the mappings, the
> property String passed in is still "playlists" instead of
> "Element_playlists" and thus it does not hook up the playlist ID with the
> converter and falls back to the default converter (which cannot handle it
> and is what is raising the error).
>
> Any guidance as to what could be going wrong here would be greatly
> appreciated.
>
> Thank you very much,
> ~~Scott
>
>
> PS--If it's useful, our system depends on the following libraries:
>
> ognl-3.0.jar
> struts2-core-2.1.8.1.jar
> xwork-core-2.1.6.jar
>