001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.PredicateFilter;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.service.GroupLocalServiceUtil;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntryTypeConstants;
026    import com.liferay.portlet.dynamicdatamapping.DDMStructure;
027    import com.liferay.portlet.dynamicdatamapping.DDMStructureLink;
028    import com.liferay.portlet.dynamicdatamapping.DDMStructureLinkManagerUtil;
029    import com.liferay.portlet.dynamicdatamapping.DDMStructureManagerUtil;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    import java.util.Locale;
034    
035    /**
036     * @author Alexander Chow
037     * @author Mate Thurzo
038     */
039    public class DLFileEntryTypeImpl extends DLFileEntryTypeBaseImpl {
040    
041            @Override
042            public List<DDMStructure> getDDMStructures() {
043                    List<DDMStructureLink> ddmStructureLinks =
044                            DDMStructureLinkManagerUtil.getStructureLinks(
045                                    PortalUtil.getClassNameId(DLFileEntryType.class),
046                                    getFileEntryTypeId());
047    
048                    return getDDMStructures(ddmStructureLinks);
049            }
050    
051            @Override
052            public String getName(Locale locale) {
053                    String name = super.getName(locale);
054    
055                    if (getFileEntryTypeId() ==
056                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
057    
058                            name = LanguageUtil.get(locale, name);
059                    }
060    
061                    return name;
062            }
063    
064            @Override
065            public String getUnambiguousName(
066                            List<DLFileEntryType> dlFileEntryTypes, long groupId,
067                            final Locale locale)
068                    throws PortalException {
069    
070                    if (getGroupId() == groupId ) {
071                            return getName(locale);
072                    }
073    
074                    boolean hasAmbiguousName = ListUtil.exists(
075                            dlFileEntryTypes,
076                            new PredicateFilter<DLFileEntryType>() {
077    
078                                    @Override
079                                    public boolean filter(DLFileEntryType fileEntryType) {
080                                            String name = fileEntryType.getName(locale);
081    
082                                            if (name.equals(getName(locale)) &&
083                                                    (fileEntryType.getFileEntryTypeId() !=
084                                                            getFileEntryTypeId())) {
085    
086                                                    return true;
087                                            }
088    
089                                            return false;
090                                    }
091    
092                            });
093    
094                    if (hasAmbiguousName) {
095                            Group group = GroupLocalServiceUtil.getGroup(getGroupId());
096    
097                            return group.getUnambiguousName(getName(locale), locale);
098                    }
099    
100                    return getName(locale);
101            }
102    
103            @Override
104            public boolean isExportable() {
105                    if (getFileEntryTypeId() ==
106                                    DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT) {
107    
108                            return false;
109                    }
110    
111                    return true;
112            }
113    
114            protected List<DDMStructure> getDDMStructures(
115                    List<DDMStructureLink> ddmStructureLinks) {
116    
117                    List<DDMStructure> ddmStructures = new ArrayList<>();
118    
119                    for (DDMStructureLink ddmStructureLink : ddmStructureLinks) {
120                            DDMStructure ddmStructure = DDMStructureManagerUtil.fetchStructure(
121                                    ddmStructureLink.getStructureId());
122    
123                            if (ddmStructure != null) {
124                                    ddmStructures.add(ddmStructure);
125                            }
126                    }
127    
128                    return ddmStructures;
129            }
130    
131    }