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