001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Antoine Taillefer 016 */ 017 018package org.nuxeo.ecm.diff.test; 019 020import java.io.Serializable; 021import java.util.ArrayList; 022import java.util.Calendar; 023import java.util.GregorianCalendar; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027import java.util.TimeZone; 028 029import org.nuxeo.ecm.core.api.Blob; 030import org.nuxeo.ecm.core.api.Blobs; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.event.EventService; 034import org.nuxeo.ecm.core.test.DefaultRepositoryInit; 035import org.nuxeo.runtime.api.Framework; 036import org.nuxeo.runtime.transaction.TransactionHelper; 037 038/** 039 * Inits the repository for a document diff test case with 2 documents of the same type. 040 * 041 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a> 042 */ 043public class DocumentDiffRepositoryInit extends DefaultRepositoryInit { 044 045 public static String getLeftDocPath() { 046 return "/leftDoc"; 047 } 048 049 public static String getRightDocPath() { 050 return "/rightDoc"; 051 } 052 053 @Override 054 public void populate(CoreSession session) { 055 056 createLeftDoc(session); 057 createRightDoc(session); 058 059 TransactionHelper.commitOrRollbackTransaction(); 060 Framework.getService(EventService.class).waitForAsyncCompletion(); 061 TransactionHelper.startTransaction(); 062 } 063 064 /** 065 * Creates the left doc. 066 * 067 * @param session the session 068 * @return the document model 069 */ 070 protected DocumentModel createLeftDoc(CoreSession session) { 071 072 DocumentModel doc = session.createDocumentModel("/", "leftDoc", "SampleType"); 073 074 // ----------------------- 075 // dublincore 076 // ----------------------- 077 doc.setPropertyValue("dc:title", "My first sample"); 078 doc.setPropertyValue("dc:description", "description"); 079 doc.setPropertyValue("dc:created", getCalendarUTCNoMillis(2011, Calendar.DECEMBER, 29, 11, 24, 25)); 080 doc.setPropertyValue("dc:creator", "Administrator"); 081 doc.setPropertyValue("dc:modified", getCalendarUTCNoMillis(2011, Calendar.DECEMBER, 29, 11, 24, 25)); 082 doc.setPropertyValue("dc:lastContributor", "Administrator"); 083 doc.setPropertyValue("dc:contributors", new String[] { "Administrator", "joe", null }); 084 doc.setPropertyValue("dc:subjects", new String[] { "Art", "Architecture" }); 085 086 // ----------------------- 087 // file 088 // ----------------------- 089 doc.setPropertyValue("file:filename", "Joe.txt"); 090 Blob blob = Blobs.createBlob("Joe is rich."); 091 blob.setFilename("Joe.txt"); 092 doc.setPropertyValue("file:content", (Serializable) blob); 093 094 // ----------------------- 095 // files 096 // ----------------------- 097 List<Map<String, Serializable>> files = new ArrayList<Map<String, Serializable>>(); 098 099 Map<String, Serializable> file = new HashMap<String, Serializable>(); 100 file.put("filename", "first_attachement.txt"); 101 blob = Blobs.createBlob("Content of the first blob"); 102 blob.setFilename("first_attachement.txt"); 103 file.put("file", (Serializable) blob); 104 files.add(file); 105 106 file = new HashMap<String, Serializable>(); 107 file.put("filename", "second_attachement.txt"); 108 blob = Blobs.createBlob("Content of the second blob"); 109 blob.setFilename("second_attachement.txt"); 110 file.put("file", (Serializable) blob); 111 files.add(file); 112 113 file = new HashMap<String, Serializable>(); 114 file.put("filename", "third_attachement.txt"); 115 blob = Blobs.createBlob("Content of the third blob"); 116 blob.setFilename("third_attachement.txt"); 117 file.put("file", (Serializable) blob); 118 files.add(file); 119 120 file = new HashMap<String, Serializable>(); 121 file.put("filename", "fourth_attachement.txt"); 122 blob = Blobs.createBlob("Content of the fourth blob"); 123 blob.setFilename("fourth_attachement.txt"); 124 file.put("file", (Serializable) blob); 125 files.add(file); 126 127 doc.setPropertyValue("files:files", (Serializable) files); 128 129 // ----------------------- 130 // simpletypes 131 // ----------------------- 132 doc.setPropertyValue("st:string", "a string property"); 133 doc.setPropertyValue("st:textarea", "a textarea property"); 134 doc.setPropertyValue("st:boolean", true); 135 doc.setPropertyValue("st:integer", 10); 136 doc.setPropertyValue("st:date", getCalendarUTCNoMillis(2011, Calendar.DECEMBER, 28, 23, 00, 00)); 137 doc.setPropertyValue( 138 "st:htmlText", 139 "<p>html text with <strong><span style=\"text-decoration: underline;\">styles</span></strong></p>\n<ul>\n<li>and</li>\n<li>nice</li>\n<li>bullets</li>\n</ul>"); 140 doc.setPropertyValue("st:multivalued", new String[] { "monday", "tuesday", "wednesday", "thursday" }); 141 142 // ----------------------- 143 // complextypes 144 // ----------------------- 145 Map<String, Serializable> complexPropValue = new HashMap<String, Serializable>(); 146 complexPropValue.put("stringItem", "string of a complex type"); 147 complexPropValue.put("booleanItem", true); 148 complexPropValue.put("integerItem", 10); 149 doc.setPropertyValue("ct:complex", (Serializable) complexPropValue); 150 151 Map<String, Serializable> item1ComplexPropValue = new HashMap<String, Serializable>(); 152 item1ComplexPropValue.put("stringItem", "first element of a complex list"); 153 item1ComplexPropValue.put("booleanItem", true); 154 item1ComplexPropValue.put("integerItem", 12); 155 156 List<Map<String, Serializable>> complexListPropValue = new ArrayList<Map<String, Serializable>>(); 157 complexListPropValue.add(item1ComplexPropValue); 158 159 doc.setPropertyValue("ct:complexList", (Serializable) complexListPropValue); 160 161 // ----------------------- 162 // listoflists 163 // ----------------------- 164 List<Map<String, Serializable>> listOfListPropValue = new ArrayList<Map<String, Serializable>>(); 165 166 Map<String, Serializable> complexItem1 = new HashMap<String, Serializable>(); 167 complexItem1.put("stringItem", "first item"); 168 List<String> item1SubList = new ArrayList<String>(); 169 item1SubList.add("Monday"); 170 item1SubList.add("Tuesday"); 171 complexItem1.put("stringListItem", (Serializable) item1SubList); 172 listOfListPropValue.add(complexItem1); 173 174 Map<String, Serializable> complexItem2 = new HashMap<String, Serializable>(); 175 complexItem2.put("stringItem", "second item"); 176 List<String> item2SubList = new ArrayList<String>(); 177 item2SubList.add("Wednesday"); 178 item2SubList.add("Thursday"); 179 complexItem2.put("stringListItem", (Serializable) item2SubList); 180 listOfListPropValue.add(complexItem2); 181 182 doc.setPropertyValue("lol:listOfLists", (Serializable) listOfListPropValue); 183 184 return session.createDocument(doc); 185 } 186 187 /** 188 * Creates the right doc. 189 * 190 * @param session the session 191 * @return the document model 192 */ 193 protected DocumentModel createRightDoc(CoreSession session) { 194 195 DocumentModel doc = session.createDocumentModel("/", "rightDoc", "SampleType"); 196 197 // ----------------------- 198 // dublincore 199 // ----------------------- 200 doc.setPropertyValue("dc:title", "My second sample"); 201 doc.setPropertyValue("dc:created", getCalendarUTCNoMillis(2011, Calendar.DECEMBER, 29, 11, 24, 50)); 202 doc.setPropertyValue("dc:creator", "Administrator"); 203 doc.setPropertyValue("dc:modified", getCalendarUTCNoMillis(2011, Calendar.DECEMBER, 30, 12, 05, 02)); 204 doc.setPropertyValue("dc:lastContributor", " Administrator "); 205 doc.setPropertyValue("dc:contributors", new String[] { "anotherAdministrator", "joe", "jack" }); 206 doc.setPropertyValue("dc:subjects", new String[] { "Art" }); 207 208 // ----------------------- 209 // file 210 // ----------------------- 211 doc.setPropertyValue("file:filename", "Jack.txt"); 212 Blob blob = Blobs.createBlob("Joe is rich, Jack is not."); 213 blob.setFilename("Jack.txt"); 214 doc.setPropertyValue("file:content", (Serializable) blob); 215 216 // ----------------------- 217 // files 218 // ----------------------- 219 List<Map<String, Serializable>> files = new ArrayList<Map<String, Serializable>>(); 220 221 Map<String, Serializable> file = new HashMap<String, Serializable>(); 222 file.put("filename", "first_attachement.txt"); 223 blob = Blobs.createBlob("Content of the first blob"); 224 blob.setFilename("first_attachement.txt"); 225 file.put("file", (Serializable) blob); 226 files.add(file); 227 228 file = new HashMap<String, Serializable>(); 229 file.put("filename", "the_file_name_is_different.txt"); 230 blob = Blobs.createBlob("Content of the second blob"); 231 blob.setFilename("the_file_name_is_different.txt"); 232 file.put("file", (Serializable) blob); 233 files.add(file); 234 235 file = new HashMap<String, Serializable>(); 236 file.put("filename", "third_attachement.txt"); 237 blob = Blobs.createBlob("Different content of the third blob"); 238 blob.setFilename("third_attachement.txt"); 239 file.put("file", (Serializable) blob); 240 files.add(file); 241 242 doc.setPropertyValue("files:files", (Serializable) files); 243 244 // ----------------------- 245 // simpletypes 246 // ----------------------- 247 doc.setPropertyValue("st:string", "a different string property"); 248 doc.setPropertyValue("st:textarea", "a textarea property"); 249 doc.setPropertyValue("st:integer", 10); 250 doc.setPropertyValue("st:date", getCalendarUTCNoMillis(2011, Calendar.DECEMBER, 28, 23, 00, 00)); 251 doc.setPropertyValue( 252 "st:htmlText", 253 "<p>html text modified with <span style=\"text-decoration: underline;\">styles</span></p>\n<ul>\n<li>and</li>\n<li>nice</li>\n<li>bullets</li>\n</ul>\n<p>&nbsp;</p>"); 254 255 // ----------------------- 256 // complextypes 257 // ----------------------- 258 Map<String, Serializable> complexPropValue = new HashMap<String, Serializable>(); 259 complexPropValue.put("stringItem", "string of a complex type"); 260 complexPropValue.put("booleanItem", false); 261 complexPropValue.put("dateItem", getCalendarUTCNoMillis(2011, Calendar.DECEMBER, 29, 23, 00, 00)); 262 doc.setPropertyValue("ct:complex", (Serializable) complexPropValue); 263 264 Map<String, Serializable> item1ComplexPropValue = new HashMap<String, Serializable>(); 265 item1ComplexPropValue.put("stringItem", "first element of a complex list"); 266 item1ComplexPropValue.put("booleanItem", false); 267 item1ComplexPropValue.put("dateItem", getCalendarUTCNoMillis(2011, Calendar.DECEMBER, 30, 23, 00, 00)); 268 269 Map<String, Serializable> item2ComplexPropValue = new HashMap<String, Serializable>(); 270 item2ComplexPropValue.put("stringItem", "second element of a complex list"); 271 item2ComplexPropValue.put("booleanItem", false); 272 item2ComplexPropValue.put("integerItem", 20); 273 274 List<Map<String, Serializable>> complexListPropValue = new ArrayList<Map<String, Serializable>>(); 275 complexListPropValue.add(item1ComplexPropValue); 276 complexListPropValue.add(item2ComplexPropValue); 277 278 doc.setPropertyValue("ct:complexList", (Serializable) complexListPropValue); 279 280 // ----------------------- 281 // listoflists 282 // ----------------------- 283 List<Map<String, Serializable>> listOfListPropValue = new ArrayList<Map<String, Serializable>>(); 284 285 Map<String, Serializable> complexItem1 = new HashMap<String, Serializable>(); 286 complexItem1.put("stringItem", "first item"); 287 List<String> item1SubList = new ArrayList<String>(); 288 item1SubList.add("Monday"); 289 item1SubList.add("Tuesday"); 290 complexItem1.put("stringListItem", (Serializable) item1SubList); 291 listOfListPropValue.add(complexItem1); 292 293 Map<String, Serializable> complexItem2 = new HashMap<String, Serializable>(); 294 complexItem2.put("stringItem", "second item is different"); 295 List<String> item2SubList = new ArrayList<String>(); 296 item2SubList.add("Wednesday"); 297 item2SubList.add("Friday"); 298 item2SubList.add("Saturday"); 299 complexItem2.put("stringListItem", (Serializable) item2SubList); 300 listOfListPropValue.add(complexItem2); 301 302 Map<String, Serializable> complexItem3 = new HashMap<String, Serializable>(); 303 complexItem3.put("stringItem", "third item"); 304 List<String> item3SubList = new ArrayList<String>(); 305 item2SubList.add("July"); 306 item2SubList.add("August"); 307 complexItem3.put("stringListItem", (Serializable) item3SubList); 308 listOfListPropValue.add(complexItem3); 309 310 doc.setPropertyValue("lol:listOfLists", (Serializable) listOfListPropValue); 311 312 return session.createDocument(doc); 313 } 314 315 /** 316 * Gets a calendar set on the UTC time zone with 0 milliseconds. 317 * 318 * @param year the year 319 * @param month the month 320 * @param day the day 321 * @param hourOfDay the hour of day 322 * @param minute the minute 323 * @param second the second 324 * @return the calendar 325 */ 326 public static Calendar getCalendarUTCNoMillis(int year, int month, int day, int hourOfDay, int minute, int second) { 327 328 Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); 329 cal.set(year, month, day, hourOfDay, minute, second); 330 cal.set(Calendar.MILLISECOND, 0); 331 332 return cal; 333 } 334 335}