<commit_before>/**
 * Copyright 2005-2011 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kew.notes.service.impl;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.kuali.rice.kew.notes.Attachment;
import org.kuali.rice.kew.notes.Note;
import org.kuali.rice.kew.notes.dao.NoteDAO;
import org.kuali.rice.kew.notes.service.AttachmentService;
import org.kuali.rice.kew.notes.service.NoteService;


public class NoteServiceImpl implements NoteService {

	private NoteDAO noteDAO;

	private AttachmentService attachmentService;

	public Note getNoteByNoteId(String noteId) {
		return getNoteDAO().getNoteByNoteId(noteId);
	}

	public List<Note> getNotesByDocumentId(String documentId) {
		return getNoteDAO().getNotesByDocumentId(documentId);
	}

	public void saveNote(Note note) {
		try {
			if (! note.getAttachments().isEmpty()){
				for (Iterator iter = note.getAttachments().iterator(); iter.hasNext();) {
					Attachment attachment = (Attachment) iter.next();
					if (attachment.getAttachedObject()!= null){
						attachmentService.persistAttachedFileAndSetAttachmentBusinessObjectValue(attachment);
					}
				}
			}
			getNoteDAO().saveNote(note);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void deleteNote(Note note) {
		try {
			for (Iterator iter = note.getAttachments().iterator(); iter.hasNext();) {
				Attachment attachment = (Attachment) iter.next();
				attachmentService.deleteAttachedFile(attachment);
			}
			getNoteDAO().deleteNote(note);
		} catch (Exception e) {
			throw new RuntimeException("caught exception deleting attachment", e);<commit_msg>KULRICE-5977 - Add null checks in NoteServiceImpl to prevent error when delete button is double clicked <commit_after>/**
 * Copyright 2005-2011 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.rice.kew.notes.service.impl;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.kuali.rice.kew.notes.Attachment;
import org.kuali.rice.kew.notes.Note;
import org.kuali.rice.kew.notes.dao.NoteDAO;
import org.kuali.rice.kew.notes.service.AttachmentService;
import org.kuali.rice.kew.notes.service.NoteService;


public class NoteServiceImpl implements NoteService {

	private NoteDAO noteDAO;

	private AttachmentService attachmentService;

	public Note getNoteByNoteId(String noteId) {
		return getNoteDAO().getNoteByNoteId(noteId);
	}

	public List<Note> getNotesByDocumentId(String documentId) {
		return getNoteDAO().getNotesByDocumentId(documentId);
	}

	public void saveNote(Note note) {
		try {
			if (! note.getAttachments().isEmpty()){
				for (Iterator iter = note.getAttachments().iterator(); iter.hasNext();) {
					Attachment attachment = (Attachment) iter.next();
					if (attachment.getAttachedObject()!= null){
						attachmentService.persistAttachedFileAndSetAttachmentBusinessObjectValue(attachment);
					}
				}
			}
			getNoteDAO().saveNote(note);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void deleteNote(Note note) {
		try {
           if (note != null && !note.getAttachments().isEmpty()){
               for (Iterator iter = note.getAttachments().iterator(); iter.hasNext();) {
                   Attachment attachment = (Attachment) iter.next();
                   attachmentService.deleteAttachedFile(attachment);
               }
           }
           if (note != null) {
               getNoteDAO().deleteNote(note);
           }
		} catch (Exception e) {
			throw new RuntimeException("caught exception deleting attachment", e);
{'fraction_non_alphanumeric': 0.1039265898420828, 'fraction_numerical': 0.005548442168160478, 'mean_word_length': 3.99680170575693, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6990175', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1351, 'n_words': 379}