Package org.internna.iwebmvc.parsers

Source Code of org.internna.iwebmvc.parsers.MultiDocumentParser

/*
* Copyright 2002-2007 the original author or authors.
*
* 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 org.internna.iwebmvc.parsers;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.internna.iwebmvc.dao.DAO;
import org.internna.iwebmvc.model.Document;
import org.internna.iwebmvc.model.MultiDocument;
import org.internna.iwebmvc.model.UUID;
import org.springframework.util.CollectionUtils;

/**
* Parses @{@link MultiDocument} entities.
*
* @author Jose Noheda
* @since 2.0
*/
public final class MultiDocumentParser extends AbstractEntityParser<MultiDocument> {

    private DocumentParser documentParser;

    public MultiDocumentParser(DAO dao, DocumentParser documentParser) {
        super(MultiDocument.class, dao);
        this.documentParser = documentParser;
    }

    private void purge(MultiDocument md) {
        Set<Document> docs = md.getDocuments();
        if (!CollectionUtils.isEmpty(docs)) {
            Set<Document> keep = new HashSet<Document>();
            Set<Document> remove = new HashSet<Document>();
            for (Document doc : docs)
                if ((doc != null) && (doc.getId() != null)) {
                    if (md.getId() == null) {
                        Document kept = documentParser.parse(doc);
                        kept.setDocumentGroup(md);
                        keep.add(kept);
                    }
                } else remove.add(doc);
            md.removeDocuments(remove);
            if (md.getId() == null) md.setDocuments(keep);
        }
    }

    @Override public MultiDocument parse(MultiDocument up) {
        if (up != null) {
            purge(up);
            if (CollectionUtils.isEmpty(up.getDocuments())) {
                if (up.getId() != null) {
                    MultiDocument old = getDao().find(MultiDocument.class, up.getId());
                    old.removeAll();
                    getDao().remove(old);
                }
                return null;
            } else {
                UUID id = up.getId();
                if (id != null) {
                    MultiDocument old = getDao().find(MultiDocument.class, id);
                    old.removeAll();
                    for (Document doc : up.getDocuments()) {
                        Document parsed = documentParser.parse(doc);
                        parsed.setDocumentGroup(old);
                        old.addDocument(parsed);
                    }
                    if (old.getDocuments().size() == 0) {
                        getDao().remove(old);
                        return null;
                    }
                    return old;
                }
            }
        }
        return up;
    }

}
TOP

Related Classes of org.internna.iwebmvc.parsers.MultiDocumentParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.