001package org.nuxeo.project.sample;
002
003import java.util.ArrayList;
004import java.util.List;
005
006import org.nuxeo.runtime.model.ComponentInstance;
007import org.nuxeo.runtime.model.DefaultComponent;
008
009public class BookTitleServiceImpl extends DefaultComponent implements BookTitleService {
010
011    private List<BookTitleDescriptor> config = new ArrayList<BookTitleDescriptor>();
012
013    public String correctTitle(String title) {
014        boolean addInitialCap = false;
015        boolean removeExtension = false;
016        String addComment = null;
017
018        for (BookTitleDescriptor d : config) {
019            if (d.addInitialCap) {
020                addInitialCap = true;
021            }
022            if (d.getRemoveExtension()) {
023                removeExtension = true;
024            }
025            if (d.getAddComment() != null) {
026                addComment = d.getAddComment();
027            }
028        }
029
030        if (removeExtension) {
031            int dot = title.lastIndexOf('.');
032            if (dot > title.length() - 5) {
033                title = title.substring(0, dot);
034            }
035        }
036        if (addInitialCap) {
037            title = title.toUpperCase();
038        }
039        if (addComment != null) {
040            title += ' ' + addComment;
041        }
042        return title;
043    }
044
045    @Override
046    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
047        config.add((BookTitleDescriptor) contribution);
048    }
049
050    @Override
051    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
052        config.remove(contribution);
053    }
054
055}