View Javadoc

1   package de.tivsource.page.converter;
2   
3   import java.util.Map;
4   import java.util.SortedSet;
5   import java.util.TreeSet;
6   
7   import javax.naming.Context;
8   import javax.naming.InitialContext;
9   
10  import org.apache.logging.log4j.LogManager;
11  import org.apache.logging.log4j.Logger;
12  import org.apache.struts2.util.StrutsTypeConverter;
13  
14  import com.opensymphony.xwork2.conversion.TypeConversionException;
15  
16  import de.tivsource.page.dao.contentitem.ContentItemDaoLocal;
17  import de.tivsource.page.entity.contentitem.ContentItem;
18  
19  
20  public class ContentItemSortedSetConverter extends StrutsTypeConverter {
21  
22      private static final Logger LOGGER = LogManager.getLogger(ContentItemSortedSetConverter.class);
23  
24      @SuppressWarnings("rawtypes")
25  	@Override
26      public Object convertFromString(Map context, String[] values, Class toClass) {
27          LOGGER.info("convertFromString(Map context, String[] values, Class toClass) aufgerufen.");
28          LOGGER.info("Anzahl der UUIDs: " + values.length);
29          
30          SortedSet<ContentItem> contentItemSortedSet = null;
31  
32          if(values.length<=0) {
33              throw new TypeConversionException("Kein gültigen ContentItems angegeben");
34          }
35      	
36      	for ( int i = 0; i < values.length; i++ ){
37      		
38      		if(i==0){
39      			contentItemSortedSet = new TreeSet<ContentItem>();
40      		}
41      		
42      		String contentItemUuid = values[i];
43              try {
44                  Context initialContext = new InitialContext();
45                  ContentItemDaoLocal contentItemDaoLocal = (ContentItemDaoLocal) initialContext.lookup("java:global/tiv-page/dao-0.0.1/ContentItemDao");
46                  ContentItem contentItem = contentItemDaoLocal.findByUuid(contentItemUuid);
47                  if(contentItem.getVisible()) {
48                      contentItemSortedSet.add(contentItem);
49                  }
50              }
51              catch (Exception e) {
52                  e.printStackTrace();
53                  throw new RuntimeException(e);
54              }
55      	}
56  
57          if(contentItemSortedSet.size()<=0) {
58              throw new TypeConversionException("Kein gültige ContentItem - UUID");
59          }
60  
61          return contentItemSortedSet;
62      }
63  
64      @SuppressWarnings("rawtypes")
65  	@Override
66      public String convertToString(Map context, Object o) {
67          ContentItem contentItem = (ContentItem)o;
68          return ""+contentItem.getUuid();
69      }
70  
71  }// Ende class