001/* 002 * (C) Copyright 2006-2013 Nuxeo SAS (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 * vpasquier <vpasquier@nuxeo.com> 016 */ 017package org.nuxeo.ecm.automation.core.operations.business.adapter; 018 019import org.apache.commons.logging.Log; 020import org.apache.commons.logging.LogFactory; 021import org.codehaus.jackson.annotate.JsonIgnore; 022import org.codehaus.jackson.annotate.JsonProperty; 023import org.codehaus.jackson.annotate.JsonPropertyOrder; 024import org.nuxeo.ecm.core.api.CoreSession; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.impl.SimpleDocumentModel; 027 028/** 029 * Nuxeo document model abstract adapter to extend for mapping 030 * 031 * @since 5.7 032 */ 033@JsonPropertyOrder({ "type", "id" }) 034public abstract class BusinessAdapter { 035 036 private static final Log log = LogFactory.getLog(BusinessAdapter.class); 037 038 @JsonProperty("id") 039 protected String id; 040 041 @JsonProperty("type") 042 protected String type; 043 044 protected transient DocumentModel doc; 045 046 /** 047 * Default constructor called by jackson 048 */ 049 public BusinessAdapter() { 050 doc = new SimpleDocumentModel(); 051 } 052 053 public BusinessAdapter(DocumentModel document) { 054 type = document.getType(); 055 doc = document; 056 id = doc.getId(); 057 } 058 059 public void save(CoreSession session) { 060 session.saveDocument(doc); 061 } 062 063 @JsonIgnore 064 public DocumentModel getDocument() { 065 return doc; 066 } 067 068 public String getId() { 069 try { 070 return doc.getId(); 071 } catch (UnsupportedOperationException e) { 072 return id; 073 } 074 } 075 076 public String getType() { 077 return doc.getType() == null ? type : doc.getType(); 078 } 079 080}