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.portal.verify;
016    
017    import com.liferay.portal.kernel.concurrent.ThrowableAwareRunnable;
018    import com.liferay.portal.kernel.concurrent.ThrowableAwareRunnablesExecutorUtil;
019    import com.liferay.portal.kernel.dao.jdbc.AutoBatchPreparedStatementUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.model.Organization;
023    import com.liferay.portal.kernel.service.ClassNameLocalServiceUtil;
024    import com.liferay.portal.kernel.service.OrganizationLocalServiceUtil;
025    import com.liferay.portal.kernel.util.LoggingTimer;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.util.PortalInstances;
028    
029    import java.sql.PreparedStatement;
030    import java.sql.ResultSet;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Daniel Kocsis
038     */
039    public class VerifyOrganization extends VerifyProcess {
040    
041            @Override
042            protected void doVerify() throws Exception {
043                    List<ThrowableAwareRunnable> throwableAwareRunnables =
044                            new ArrayList<>();
045    
046                    throwableAwareRunnables.add(
047                            new ThrowableAwareRunnable() {
048    
049                                    @Override
050                                    protected void doRun() throws Exception {
051                                            rebuildTree();
052                                    }
053    
054                            });
055    
056                    throwableAwareRunnables.add(
057                            new ThrowableAwareRunnable() {
058    
059                                    @Override
060                                    protected void doRun() throws Exception {
061                                            updateOrganizationAssets();
062                                    }
063    
064                            });
065    
066                    throwableAwareRunnables.add(
067                            new ThrowableAwareRunnable() {
068    
069                                    @Override
070                                    protected void doRun() throws Exception {
071                                            updateOrganizationAssetEntries();
072                                    }
073    
074                            });
075    
076                    ThrowableAwareRunnablesExecutorUtil.execute(throwableAwareRunnables);
077            }
078    
079            protected void rebuildTree() throws Exception {
080                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
081                            long[] companyIds = PortalInstances.getCompanyIdsBySQL();
082    
083                            for (long companyId : companyIds) {
084                                    OrganizationLocalServiceUtil.rebuildTree(companyId);
085                            }
086                    }
087            }
088    
089            protected void updateOrganizationAssetEntries() throws Exception {
090                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
091                            StringBundler sb = new StringBundler();
092    
093                            sb.append("select distinct AssetEntry.classPK as classPK, ");
094                            sb.append("Organization_.uuid_ as uuid from ");
095                            sb.append(
096                                    "AssetEntry, Organization_ where AssetEntry.classNameId = ");
097    
098                            long classNameId = ClassNameLocalServiceUtil.getClassNameId(
099                                    Organization.class.getName());
100    
101                            sb.append(classNameId);
102    
103                            sb.append(
104                                    " and AssetEntry.classPK = Organization_.organizationId ");
105                            sb.append("and AssetEntry.classUuid is null");
106    
107                            try (PreparedStatement ps1 = connection.prepareStatement(
108                                            sb.toString());
109                                    ResultSet rs = ps1.executeQuery()) {
110    
111                                    try (PreparedStatement ps2 =
112                                                    AutoBatchPreparedStatementUtil.autoBatch(
113                                                            connection.prepareStatement(
114                                                                    "update AssetEntry set classUuid = ? where " +
115                                                                            "classPK = ? and classNameId = ?"))) {
116    
117                                            while (rs.next()) {
118                                                    long classPK = rs.getLong("classPK");
119                                                    String uuid = rs.getString("uuid");
120    
121                                                    ps2.setString(1, uuid);
122                                                    ps2.setLong(2, classPK);
123                                                    ps2.setLong(3, classNameId);
124    
125                                                    ps2.addBatch();
126                                            }
127    
128                                            ps2.executeBatch();
129                                    }
130                            }
131                    }
132            }
133    
134            protected void updateOrganizationAssets() throws Exception {
135                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
136                            List<Organization> organizations =
137                                    OrganizationLocalServiceUtil.getNoAssetOrganizations();
138    
139                            if (_log.isDebugEnabled()) {
140                                    _log.debug(
141                                            "Processing " + organizations.size() + " organizations " +
142                                                    "with no asset");
143                            }
144    
145                            for (Organization organization : organizations) {
146                                    try {
147                                            OrganizationLocalServiceUtil.updateAsset(
148                                                    organization.getUserId(), organization, null, null);
149                                    }
150                                    catch (Exception e) {
151                                            if (_log.isWarnEnabled()) {
152                                                    _log.warn(
153                                                            "Unable to update asset for organization " +
154                                                                    organization.getOrganizationId() + ": " +
155                                                                            e.getMessage());
156                                            }
157                                    }
158                            }
159    
160                            if (_log.isDebugEnabled()) {
161                                    _log.debug("Assets verified for organizations");
162                            }
163                    }
164            }
165    
166            private static final Log _log = LogFactoryUtil.getLog(
167                    VerifyOrganization.class);
168    
169    }