001/* 002 * (C) Copyright 2012 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 * Thierry Delprat 018 */ 019package org.nuxeo.template.adapters.doc; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025import java.util.Map; 026 027import org.nuxeo.ecm.core.api.DocumentModel; 028 029public class TemplateBindings extends ArrayList<TemplateBinding> { 030 031 private static final long serialVersionUID = 1L; 032 033 public static final String DEFAULT_BINDING = "default"; 034 035 public static final String BINDING_PROP_NAME = "nxts:bindings"; 036 037 public TemplateBindings(DocumentModel doc) { 038 Serializable value = doc.getPropertyValue(BINDING_PROP_NAME); 039 if (value != null) { 040 @SuppressWarnings("unchecked") 041 List<Map<String, Serializable>> bindings = (List<Map<String, Serializable>>) value; 042 for (Map<String, Serializable> binding : bindings) { 043 add(new TemplateBinding(binding)); 044 } 045 } 046 } 047 048 public String useMainContentAsTemplate() { 049 for (TemplateBinding tb : this) { 050 if (tb.isUseMainContentAsTemplate()) { 051 return tb.getName(); 052 } 053 } 054 return null; 055 } 056 057 public TemplateBinding get() { 058 return get(DEFAULT_BINDING); 059 } 060 061 public void removeByName(String templateName) { 062 Iterator<TemplateBinding> it = this.iterator(); 063 while (it.hasNext()) { 064 TemplateBinding binding = it.next(); 065 if (binding.getName().equals(templateName)) { 066 it.remove(); 067 return; 068 } 069 } 070 } 071 072 public boolean containsTemplateName(String templateName) { 073 for (TemplateBinding tb : this) { 074 if (templateName.equals(tb.getName())) { 075 return true; 076 } 077 } 078 return false; 079 } 080 081 public boolean containsTemplateId(String templateId) { 082 for (TemplateBinding tb : this) { 083 if (templateId.equals(tb.getTemplateId())) { 084 return true; 085 } 086 } 087 return false; 088 } 089 090 public TemplateBinding get(String name) { 091 for (TemplateBinding tb : this) { 092 if (name.equals(tb.getName())) { 093 return tb; 094 } 095 } 096 return null; 097 } 098 099 public void addOrUpdate(TemplateBinding tb) { 100 TemplateBinding existing = get(tb.getName()); 101 if (existing == null) { 102 add(tb); 103 } else { 104 existing.update(tb); 105 } 106 } 107 108 public List<String> getNames() { 109 110 List<String> names = new ArrayList<String>(); 111 for (TemplateBinding tb : this) { 112 names.add(tb.getName()); 113 } 114 return names; 115 } 116 117 public void save(DocumentModel doc) { 118 List<Map<String, Serializable>> bindings = new ArrayList<Map<String, Serializable>>(); 119 for (TemplateBinding tb : this) { 120 bindings.add(tb.getAsMap()); 121 } 122 doc.setPropertyValue(BINDING_PROP_NAME, (Serializable) bindings); 123 } 124}