001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 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 */ 016package org.kuali.rice.krad.service.impl; 017 018import java.util.List; 019 020import org.apache.commons.lang.StringUtils; 021import org.kuali.rice.core.api.criteria.OrderByField; 022import org.kuali.rice.core.api.criteria.OrderDirection; 023import org.kuali.rice.core.api.criteria.QueryByCriteria; 024import org.kuali.rice.core.api.criteria.QueryResults; 025import org.kuali.rice.core.api.mo.common.GloballyUnique; 026import org.kuali.rice.core.api.util.cache.CopiedObject; 027import org.kuali.rice.krad.bo.Attachment; 028import org.kuali.rice.krad.bo.Note; 029import org.kuali.rice.krad.data.DataObjectService; 030import org.kuali.rice.krad.service.AttachmentService; 031import org.kuali.rice.krad.service.NoteService; 032import org.springframework.beans.factory.annotation.Required; 033import org.springframework.transaction.annotation.Transactional; 034 035/** 036 * Service implementation for the Note structure 037 * 038 * @author Kuali Rice Team (rice.collab@kuali.org) 039 */ 040@Transactional 041public class NoteServiceImpl implements NoteService { 042 043 protected AttachmentService attachmentService; 044 protected DataObjectService dataObjectService; 045 046 @Override 047 public void saveNoteList(List<Note> notes) { 048 if (notes != null) { 049 for (Note note : notes) { 050 if (StringUtils.isBlank(note.getRemoteObjectIdentifier())) { 051 throw new IllegalStateException("The remote object identifier must be established on a Note before it can be saved. The following note in the given list had a null or empty remote object identifier: " + note); 052 } 053 save(note); 054 } 055 } 056 } 057 058 /** 059 * @see org.kuali.rice.krad.service.NoteService#save(org.kuali.rice.krad.bo.Note) 060 */ 061 @Override 062 public Note save(Note note) { 063 validateNoteNotNull(note); 064 if (StringUtils.isBlank(note.getRemoteObjectIdentifier())) { 065 throw new IllegalStateException("The remote object identifier must be established on a Note before it can be saved. Given note had a null or empty remote object identifier."); 066 } 067 if (note.getAttachment() != null && note.getAttachment().getAttachmentFileName() == null) { 068 note.setAttachment(null); 069 } 070 if (note != null && note.getNoteIdentifier() == null && note.getAttachment() != null) { 071 Attachment attachment = note.getAttachment(); 072 note.setAttachment(null); 073 // store without attachment 074 note = dataObjectService.save(note); 075 attachment.setNoteIdentifier(note.getNoteIdentifier()); 076 // put attachment back 077 note.setAttachment(attachment); 078 } 079 note = dataObjectService.save(note); 080 // move attachment from pending directory 081 if (note.getAttachment() != null) { 082 attachmentService.moveAttachmentWherePending(note); 083 } 084 return note; 085 } 086 087 /** 088 * @see org.kuali.rice.krad.service.NoteService#getByRemoteObjectId(java.lang.String) 089 */ 090 @Override 091 public List<Note> getByRemoteObjectId(String remoteObjectId) { 092 if (StringUtils.isBlank(remoteObjectId)) { 093 throw new IllegalArgumentException("The remoteObjectId must not be null or blank."); 094 } 095 QueryResults<Note> result = dataObjectService.findMatching(Note.class, 096 QueryByCriteria.Builder.forAttribute("remoteObjectIdentifier", remoteObjectId) 097 .setOrderByFields(OrderByField.Builder.create("notePostedTimestamp", OrderDirection.ASCENDING).build() 098 ).build()); 099 return result.getResults(); 100 } 101 102 /** 103 * @see org.kuali.rice.krad.service.NoteService#getNoteByNoteId(java.lang.Long) 104 */ 105 @Override 106 public Note getNoteByNoteId(Long noteId) { 107 if (noteId == null) { 108 throw new IllegalArgumentException("The noteId must not be null."); 109 } 110 return dataObjectService.find(Note.class, noteId); 111 } 112 113 /** 114 * @see org.kuali.rice.krad.service.NoteService#deleteNote(org.kuali.rice.krad.bo.Note) 115 */ 116 @Override 117 public void deleteNote(Note note) { 118 validateNoteNotNull(note); 119 if ( note.getAttachment() != null ) { // Not sure about this - might blow up when no attachment 120 dataObjectService.delete(note.getAttachment()); 121 } 122 dataObjectService.delete(note); 123 } 124 125 /** 126 * TODO this method seems awfully out of place in this service 127 * 128 */ 129 @Override 130 public Note createNote(Note noteToCopy, GloballyUnique bo, String authorPrincipalId) { 131 validateNoteNotNull(noteToCopy); 132 if (bo == null) { 133 throw new IllegalArgumentException("The bo must not be null."); 134 } 135 if (StringUtils.isBlank(authorPrincipalId)) { 136 throw new IllegalArgumentException("The authorPrincipalId must not be null."); 137 } 138 139 Note tmpNote = new CopiedObject<Note>(noteToCopy).getContent(); 140 tmpNote.setRemoteObjectIdentifier(bo.getObjectId()); 141 tmpNote.setAuthorUniversalIdentifier(authorPrincipalId); 142 143 return tmpNote; 144 } 145 146 @Required 147 public void setAttachmentService(AttachmentService attachmentService) { 148 this.attachmentService = attachmentService; 149 } 150 151 protected AttachmentService getAttachmentService() { 152 return this.attachmentService; 153 } 154 155 private void validateNoteNotNull(Note note) { 156 if (note == null) { 157 throw new IllegalArgumentException("Note must not be null."); 158 } 159 } 160 161 @Required 162 public void setDataObjectService(DataObjectService dataObjectService) { 163 this.dataObjectService = dataObjectService; 164 } 165 166}