com.vnet.demo.service.NoteService.java Source code

Java tutorial

Introduction

Here is the source code for com.vnet.demo.service.NoteService.java

Source

/*
 * Copyright (c) 2015-2020, Chen Rui
 *
 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
 *
 * 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 com.vnet.demo.service;

import java.io.ByteArrayInputStream;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vnet.demo.dao.NoteDao;
import com.vnet.demo.dao.NotebookDao;
import com.vnet.demo.entity.Note;
import com.vnet.demo.entity.Notebook;
import com.vnet.demo.service.azure.storage.AzureStorageServiceFactory;
import com.vnet.demo.utils.AbstractGenerator;

@Service
public class NoteService {

    @Autowired
    NoteDao noteDao;

    @Autowired
    NotebookDao notebookDao;

    @Autowired
    AzureStorageServiceFactory storageSessionFactory;

    public List<Note> getNotes(Long bookId, Long startId, Integer number) {
        List<Note> notes = noteDao.findNotesByBookId(bookId, startId, number);
        return notes;
    }

    public Note getNote(Long noteId) {
        Note note = noteDao.findById(noteId);
        if (StringUtils.isNotEmpty(note.getContext())) {
            note.setContext(getNoteContextFromStoragge(note.getContext()));
            note.getNotebook();
        }
        note.getNotebook();
        return note;
    }

    public Long getNotesNumber(Long bookId) {
        return noteDao.findNotesNumber(bookId);
    }

    public Note updateNote(Note note) {
        Notebook notebook = notebookDao.findById(1L);
        note.setNotebook(notebook);
        AbstractGenerator generator = new AbstractGenerator(note.getContext());
        note.setSummary(generator.generate(100));
        note.setContext(this.saveNoteContextToStoragge(note.getContext()));
        noteDao.update(note);
        return note;
    }

    public Note saveNote(Note note) {
        Notebook notebook = notebookDao.findById(1L);
        note.setNotebook(notebook);
        AbstractGenerator generator = new AbstractGenerator(note.getContext());
        note.setSummary(generator.generate(100));
        note.setContext(this.saveNoteContextToStoragge(note.getContext()));
        noteDao.save(note);
        return note;
    }

    public void deleteNote(Long noteId) {
        noteDao.delete(noteId);
    }

    private String saveNoteContextToStoragge(String context) {
        String name = "test.html";
        ByteArrayInputStream stream = new ByteArrayInputStream(context.getBytes());
        String htmlPath = storageSessionFactory.uploadFileToStorage(stream, name, new Long(context.length()));
        return htmlPath;
    }

    private String getNoteContextFromStoragge(String path) {
        return storageSessionFactory.getFileContentFromStorage(path);
    }

}