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