Clover coverage report -
Coverage timestamp: Fri Nov 19 2004 13:41:51 PST
file stats: LOC: 181   Methods: 7
NCLOC: 154   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DocumentFactory.java 81.8% 90.8% 100% 88.2%
coverage coverage
 1    package photospace.search;
 2   
 3    import java.beans.*;
 4    import java.text.*;
 5    import java.util.*;
 6    import org.apache.commons.lang.*;
 7    import org.apache.commons.logging.*;
 8    import org.apache.lucene.document.*;
 9    import org.springframework.beans.*;
 10    import org.springframework.beans.propertyeditors.*;
 11    import photospace.meta.*;
 12    import photospace.space.*;
 13   
 14    public class DocumentFactory
 15    {
 16    private static final Log log = LogFactory.getLog(DocumentFactory.class);
 17   
 18    // dummy field so that we can find all documents
 19    // see http://nagoya.apache.org/eyebrowse/ReadMsg?msgId=716778
 20    public static final String ALL_FIELD = "ALL";
 21    public static final String ALL_VALUE = "1";
 22   
 23    public static final String CLASS_FIELD = "CLASS";
 24    public static final String TEXT_FIELD = "TEXT";
 25    public static final String SORT_FIELD = "created-datetime";
 26    public static final String PARENT_FIELD = "parent";
 27    public static final String LATITUDE_FIELD = "position.latitude";
 28    public static final String LONGITUDE_FIELD = "position.longitude";
 29   
 30    public static final String TYPE_FIELD = "TYPE";
 31    public static final String FOLDER_TYPE = "FOLDER";
 32    public static final String PHOTO_TYPE = "PHOTO";
 33    public static final String MOVIE_TYPE = "MOVIE";
 34    public static final String AUDIO_TYPE = "AUDIO";
 35   
 36    private static final NumberFormat numberFormat = NumberFormat.getInstance();
 37   
 38  56 public Meta createMeta(Document doc)
 39    {
 40  56 Meta meta = null;
 41  56 try
 42    {
 43  56 meta = (Meta) Class.forName(doc.getField(CLASS_FIELD).stringValue()).newInstance();
 44  56 BeanWrapper bean = new BeanWrapperImpl(meta);
 45  56 registerCustomEditors(bean);
 46  56 for (Enumeration fields = doc.fields(); fields.hasMoreElements();)
 47    {
 48  561 Field field = (Field) fields.nextElement();
 49  230 if (!bean.isReadableProperty(field.name())) continue;
 50  0 if (!bean.isWritableProperty(field.name())) continue;
 51   
 52  331 Class klass = bean.getPropertyType(field.name());
 53   
 54  331 if (isPrimitive(klass))
 55    {
 56  229 bean.setPropertyValue(field.name(), field.stringValue());
 57    }
 58  102 else if (Date.class.isAssignableFrom(klass))
 59    {
 60  30 bean.setPropertyValue(field.name(), DateField.stringToDate(field.stringValue()));
 61    }
 62  72 else if (String[].class.isAssignableFrom(klass))
 63    {
 64  22 List values = new ArrayList(Arrays.asList((String[]) bean.getPropertyValue(field.name())));
 65  22 values.add(field.stringValue());
 66  22 bean.setPropertyValue(field.name(), values.toArray(new String[] {}));
 67    }
 68    }
 69    }
 70    catch (Exception e)
 71    {
 72  0 log.error("Exception trying to create meta instance for document of type " + doc.getField(CLASS_FIELD).stringValue());
 73    }
 74  56 return meta;
 75    }
 76   
 77  56 private void registerCustomEditors(BeanWrapper bean)
 78    {
 79  56 bean.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, numberFormat, false));
 80    }
 81   
 82   
 83  45 public Document createDocument(Meta meta)
 84    {
 85  0 if (log.isDebugEnabled()) log.debug("Creating document for " + meta);
 86   
 87  45 Document doc = new Document();
 88  45 doc.add(Field.Keyword(CLASS_FIELD, meta.getClass().getName()));
 89  45 doc.add(Field.Keyword(ALL_FIELD, ALL_VALUE));
 90   
 91  11 if (meta instanceof FolderMeta) doc.add(Field.Keyword(TYPE_FIELD, FOLDER_TYPE));
 92  34 else if (meta instanceof PhotoMeta) doc.add(Field.Keyword(TYPE_FIELD, PHOTO_TYPE));
 93   
 94  45 doc.add(getSearchField(meta));
 95   
 96  45 addProperties(doc, meta, null);
 97  40 if (meta.getParentPath() != null) doc.add(Field.Keyword(PARENT_FIELD, meta.getParentPath()));
 98   
 99  45 return doc;
 100    }
 101   
 102  45 private Field getSearchField(Meta meta)
 103    {
 104  45 Set text = new HashSet();
 105  45 text.add(meta.getPath());
 106  45 text.add(meta.getTitle());
 107  45 text.add(meta.getName());
 108  45 text.add(meta.getDescription());
 109  45 text.add(meta.getCreator());
 110  45 text.addAll(Arrays.asList(meta.getLabels()));
 111  45 return Field.UnStored(TEXT_FIELD, StringUtils.join(text.iterator(), " "));
 112    }
 113   
 114  90 private void addProperties(Document doc, Object obj, String prefix)
 115    {
 116  90 BeanWrapper bean = new BeanWrapperImpl(obj);
 117  90 PropertyDescriptor[] properties = bean.getPropertyDescriptors();
 118   
 119  90 for (int i = 0; i < properties.length; i++)
 120    {
 121  1362 String name = properties[i].getName();
 122  0 if (!bean.isReadableProperty(name)) continue;
 123  269 if (!bean.isWritableProperty(name)) continue;
 124  806 if (bean.getPropertyValue(name) == null) continue;
 125   
 126  287 Class klass = bean.getPropertyType(name);
 127  287 String property = prefix != null ? prefix + "." + name : name;
 128   
 129  0 if (log.isDebugEnabled()) log.debug("Adding " + property + " (" + klass.getName() + ")" + ": " + bean.getPropertyValue(name).toString());
 130   
 131  287 if (isPrimitive(klass))
 132    {
 133  180 doc.add(Field.Keyword(property, bean.getPropertyValue(name).toString()));
 134    }
 135  107 else if (Date.class.isAssignableFrom(klass))
 136    {
 137  17 addDateField(doc, bean, property, name);
 138    }
 139  90 else if (String[].class.isAssignableFrom(klass))
 140    {
 141  45 String[] array = (String[]) bean.getPropertyValue(name);
 142  45 for (int j = 0; j < array.length; j++)
 143    {
 144  15 doc.add(Field.Keyword(property, array[j]));
 145    }
 146    }
 147  45 else if (Position.class.isAssignableFrom(klass))
 148    {
 149  45 addProperties(doc, bean.getPropertyValue(name), property);
 150    }
 151    else
 152    {
 153  0 if (log.isDebugEnabled()) log.debug("Not adding " + klass + " " + name);
 154    }
 155    }
 156    }
 157   
 158    private static DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
 159    private static DateFormat datetimeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
 160  17 private void addDateField(Document doc, BeanWrapper bean, String property, String name)
 161    {
 162  17 Date date = (Date) bean.getPropertyValue(name);
 163  17 doc.add(Field.Keyword(property, date));
 164  17 doc.add(Field.Keyword(property + "-date", dateFormat.format(date)));
 165  17 doc.add(Field.Keyword(property + "-datetime", datetimeFormat.format(date)));
 166    }
 167   
 168  618 private boolean isPrimitive(Class klass)
 169    {
 170  618 return Number.class.isAssignableFrom(klass)
 171    || klass == String.class
 172    || klass == java.lang.Boolean.TYPE
 173    || klass == java.lang.Character.TYPE
 174    || klass == java.lang.Byte.TYPE
 175    || klass == java.lang.Short.TYPE
 176    || klass == java.lang.Integer.TYPE
 177    || klass == java.lang.Long.TYPE
 178    || klass == java.lang.Float.TYPE
 179    || klass == java.lang.Double.TYPE;
 180    }
 181    }